Skip to main content

Multi-factor authentication

This guide shows you how to enable multi-factor authentication (MFA) for your Ory project.

To enable MFA using the Console:

  1. Log in to your Ory Console
  2. Select your workspace and project
  3. Navigate to the Authentication tab
  4. Click on Two-factor auth in the sidebar

Configure One-Time Codes

In the Two-factor auth settings, you can enable and configure One-Time Codes for multi-factor authentication:

https://console.ory.sh/projects/<id>/mfa

One-Time Codes Settings

Toggle the "Enable one-time code multi factor authentication" toggle to allow users to receive one-time codes for MFA.

What users will see

When MFA is enabled, users will see a second authentication screen after logging in:

https://console.ory.sh/projects/<id>/mfa

MFA Outcome

Check AAL

Authentication Authorization Level (AAL) is a concept that describes the strength of the authentication factor used to access a resource.

  • aal1: Password/OIDC
  • aal2: Password/OIDC and one-time code

To check the AAL of the current session, use the authenticator_assurance_level on the toSession method.

const requireAuth = async (req, res, next) => {
try {
const session = await ory.toSession({ cookie: req.header("cookie") })
if (session.authenticator_assurance_level === "aal2") {
req.session = session
next()
} else {
res.redirect(
`${process.env.ORY_SDK_URL}/self-service/login/browser?aal=aal2`,
)
}
} catch (error) {
res.redirect(`${process.env.ORY_SDK_URL}/self-service/login/browser`)
}
}

app.get("/", requireAuth, (req, res) => {
res.json(req.session.identity.traits) // { email: 'newtestuser@gmail.com' }
})

User flow

  1. The user enters their username/password or uses another primary authentication method
  2. They see the MFA challenge screen
  3. A one-time code is sent to their email
  4. After entering the valid code, they gain access to the application or protected settings