Multi-factor verification (MFA) is an added layer of security that requires users to provide a second verification factor to access an account.
One of the options that Clerk supports for MFA is Authenticator applications (also known as TOTP - Time-based One-time Password) . This guide will walk you through how to build a custom flow that allows users to manage their TOTP settings.
For your users to be able to enable MFA for their account, you need to enable MFA as an MFA authentication strategy in your Clerk application.
In the Clerk Dashboard, navigate to the Multi-factor page.
Enable Authenticator application and Backup codes .
Select Save .
This example is written for Next.js App Router but it can be adapted for any React meta framework, such as Remix.
This example consists of two pages:
The main page where users can manage their MFA settings
The page where users can add TOTP MFA.
Use the following tabs to view the code necessary for each page.
app /account /manage-mfa /page.tsx 'use client'
import * as React from 'react'
import { useUser } from '@clerk/nextjs'
import Link from 'next/link'
import { BackupCodeResource } from '@clerk/types'
// If TOTP is enabled, provide the option to disable it
const TotpEnabled = () => {
const { user } = useUser ()
const disableTOTP = async () => {
await user ?.disableTOTP ()
}
return (
< div >
< p >
TOTP via authentication app enabled - < button onClick = {() => disableTOTP ()}>Remove</ button >
</ p >
</ div >
)
}
// If TOTP is disabled, provide the option to enable it
const TotpDisabled = () => {
return (
< div >
< p >
Add TOTP via authentication app -{ ' ' }
< Link href = "/account/manage-mfa/add" >
< button >Add</ button >
</ Link >
</ p >
</ div >
)
}
// Generate and display backup codes
export function GenerateBackupCodes () {
const { user } = useUser ()
const [ backupCodes , setBackupCodes ] = React .useState < BackupCodeResource | undefined >( undefined )
const [ loading , setLoading ] = React .useState ( false )
React .useEffect (() => {
if (backupCodes) {
return
}
setLoading ( true )
void user
?.createBackupCode ()
.then ((backupCode : BackupCodeResource ) => {
setBackupCodes (backupCode)
setLoading ( false )
})
.catch ((err) => {
// See https://clerk.com/docs/custom-flows/error-handling
// for more info on error handling
console .error ( JSON .stringify (err , null , 2 ))
setLoading ( false )
})
} , [])
if (loading) {
return < p >Loading...</ p >
}
if ( ! backupCodes) {
return < p >There was a problem generating backup codes</ p >
}
return (
< ol >
{ backupCodes . codes .map ((code , index) => (
< li key = {index}>{code}</ li >
))}
</ ol >
)
}
export default function ManageMFA () {
const { isLoaded , user } = useUser ()
const [ showNewCodes , setShowNewCodes ] = React .useState ( false )
if ( ! isLoaded) return null
if ( ! user) {
return < p >You must be logged in to access this page</ p >
}
return (
<>
< h1 >User MFA Settings</ h1 >
{ /* Manage TOTP MFA */ }
{ user .totpEnabled ? < TotpEnabled /> : < TotpDisabled />}
{ /* Manage backup codes */ }
{ user .backupCodeEnabled && user .twoFactorEnabled && (
< div >
< p >
Generate new backup codes? -{ ' ' }
< button onClick = {() => setShowNewCodes ( true )}>Generate</ button >
</ p >
</ div >
)}
{showNewCodes && (
<>
< GenerateBackupCodes />
< button onClick = {() => setShowNewCodes ( false )}>Done</ button >
</>
)}
</>
)
}
app /account /manage-mfa /add /page.tsx 'use client'
import { useUser } from '@clerk/nextjs'
import { TOTPResource } from '@clerk/types'
import Link from 'next/link'
import * as React from 'react'
import { QRCodeSVG } from 'qrcode.react'
import { GenerateBackupCodes } from '../page'
type AddTotpSteps = 'add' | 'verify' | 'backupcodes' | 'success'
type DisplayFormat = 'qr' | 'uri'
function AddTotpScreen ({
setStep ,
} : {
setStep : React . Dispatch < React . SetStateAction < AddTotpSteps >>
}) {
const { user } = useUser ()
const [ totp , setTOTP ] = React .useState < TOTPResource | undefined >( undefined )
const [ displayFormat , setDisplayFormat ] = React .useState < DisplayFormat >( 'qr' )
React .useEffect (() => {
void user
?.createTOTP ()
.then ((totp : TOTPResource ) => {
setTOTP (totp)
})
.catch ((err) =>
// See https://clerk.com/docs/custom-flows/error-handling
// for more info on error handling
console .error ( JSON .stringify (err , null , 2 )) ,
)
} , [])
return (
<>
< h1 >Add TOTP MFA</ h1 >
{totp && displayFormat === 'qr' && (
<>
< div >
< QRCodeSVG value = { totp ?.uri || '' } size = { 200 } />
</ div >
< button onClick = {() => setDisplayFormat ( 'uri' )}>Use URI instead</ button >
</>
)}
{totp && displayFormat === 'uri' && (
<>
< div >
< p >{ totp .uri}</ p >
</ div >
< button onClick = {() => setDisplayFormat ( 'qr' )}>Use QR Code instead</ button >
</>
)}
< button onClick = {() => setStep ( 'add' )}>Reset</ button >
< p >Once you have set up your authentication app, verify your code</ p >
< button onClick = {() => setStep ( 'verify' )}>Verify</ button >
</>
)
}
function VerifyTotpScreen ({
setStep ,
} : {
setStep : React . Dispatch < React . SetStateAction < AddTotpSteps >>
}) {
const { user } = useUser ()
const [ code , setCode ] = React .useState ( '' )
const verifyTotp = async (e : React . FormEvent ) => {
e .preventDefault ()
try {
await user ?.verifyTOTP ({ code })
setStep ( 'backupcodes' )
} catch (err) {
console .error ( JSON .stringify (err , null , 2 ))
}
}
return (
<>
< h1 >Verify TOTP</ h1 >
< form onSubmit = {(e) => verifyTotp (e)}>
< label htmlFor = "totp-code" >Enter the code from your authentication app</ label >
< input type = "text" id = "totp-code" onChange = {(e) => setCode ( e . currentTarget .value)} />
< button type = "submit" >Verify code</ button >
< button onClick = {() => setStep ( 'add' )}>Reset</ button >
</ form >
</>
)
}
function BackupCodeScreen ({
setStep ,
} : {
setStep : React . Dispatch < React . SetStateAction < AddTotpSteps >>
}) {
return (
<>
< h1 >Verification was a success!</ h1 >
< div >
< p >
Save this list of backup codes somewhere safe in case you need to access your account in
an emergency
</ p >
< GenerateBackupCodes />
< button onClick = {() => setStep ( 'success' )}>Finish</ button >
</ div >
</>
)
}
function SuccessScreen () {
return (
<>
< h1 >Success!</ h1 >
< p >You have successfully added TOTP MFA via an authentication application.</ p >
</>
)
}
export default function AddMFaScreen () {
const [ step , setStep ] = React .useState < AddTotpSteps >( 'add' )
const { isLoaded , user } = useUser ()
if ( ! isLoaded) return null
if ( ! user) {
return < p >You must be logged in to access this page</ p >
}
return (
<>
{step === 'add' && < AddTotpScreen setStep = {setStep} />}
{step === 'verify' && < VerifyTotpScreen setStep = {setStep} />}
{step === 'backupcodes' && < BackupCodeScreen setStep = {setStep} />}
{step === 'success' && < SuccessScreen />}
< Link href = "/account/manage-mfa" >Manage MFA</ Link >
</>
)
}
While Clerk does not natively enforce MFA for all users, you can implement this functionality by using clerkMiddleware()
to check whether a user has MFA enabled.
The following example demonstrates how to force MFA for all users. It uses clerkMiddleware()
to intercept all requests and check whether a user has MFA enabled. If the user does not have MFA enabled, clerkMiddleware()
redirects them to the /mfa
page where they can set up MFA.
middleware.ts import { clerkMiddleware , createRouteMatcher } from '@clerk/nextjs/server'
import { NextResponse } from 'next/server'
const isMFARoute = createRouteMatcher ([ '/account/manage-mfa/add(.*)' ])
const isSignInRoute = createRouteMatcher ([ '/sign-in(.*)' ])
export default clerkMiddleware ( async (auth , req) => {
const { userId } = await auth ()
// Redirect to homepage if the user is signed in and on the sign-in page
if (userId !== null && isSignInRoute (req) && ! isMFARoute (req)) {
return NextResponse .redirect ( new URL ( '/' , req .url))
}
// Check if the user is signed in and not on the MFA page
if (userId !== null && ! isMFARoute (req)) {
const res = await fetch ( `https://api.clerk.com/v1/users/ ${ userId } ` , {
headers : {
Authorization : `Bearer ${ process . env . CLERK_SECRET_KEY } ` ,
} ,
})
const userData = await res .json ()
// Redirect to MFA setup page if MFA is not enabled
if ( userData .two_factor_enabled === false ) {
return NextResponse .redirect ( new URL ( '/account/manage-mfa/add' , req .url))
}
}
})
export const config = {
matcher : [
// Skip Next.js internals and all static files, unless found in search params
'/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).*)' ,
// Always run for API routes
'/(api|trpc)(.*)' ,
] ,
}
Install expo-checkbox
for the UI and react-native-qr-svg
for the QR code.
terminal npm install expo-checkbox react-native-qr-svg
terminal yarn add expo-checkbox react-native-qr-svg
terminal pnpm add expo-checkbox react-native-qr-svg
To allow users to configure their MFA settings, you'll create a basic dashboard.
The following example consists of three pages:
The layout page that checks if the user is signed in
The page where users can manage their account, including their MFA settings
The page where users can add TOTP MFA
Use the following tabs to view the code necessary for each page.
Create the (dashboard)
route group. This groups your account page and the "Add TOTP MFA" page.
Create a _layout.tsx
file with the following code. The useAuth()
hook is used to check if the user is signed in. If the user isn't signed in, they'll be redirected to the sign-in page.
app /(dashboard) /_layout.tsx import { Redirect , Stack } from 'expo-router'
import { useAuth } from '@clerk/clerk-expo'
export default function AuthenticatedLayout () {
const { isSignedIn } = useAuth ()
if ( ! isSignedIn) {
return < Redirect href = { '/sign-in' } />
}
return < Stack />
}
In the (dashboard)
group, create an account.tsx
file with the following code. This page shows users whether or not MFA is enabled, and allows them to add MFA with an authenticator app.
app /(dashboard) /account.tsx import React from 'react'
import { useUser } from '@clerk/clerk-expo'
import { useRouter } from 'expo-router'
import { View , Text , Button , FlatList } from 'react-native'
import { BackupCodeResource } from '@clerk/types'
export default function ManageTOTPMfa () {
const router = useRouter ()
const [ backupCodes , setBackupCodes ] = React .useState < BackupCodeResource | undefined >( undefined )
const [ loading , setLoading ] = React .useState ( false )
const { isLoaded , user } = useUser ()
if ( ! isLoaded || ! user) return null
const generateBackupCodes = () => {
setLoading ( true )
void user
?.createBackupCode ()
.then ((backupCodes : BackupCodeResource ) => {
setBackupCodes (backupCodes)
setLoading ( false )
})
.catch ((error) => {
console .log ( 'Error:' , error)
setLoading ( false )
})
}
const disableTOTP = async () => {
await user .disableTOTP ()
}
const MFAEnabled = () => {
return (
< View style = {{ flexDirection : 'row' , alignItems : 'center' }}>
< Text >TOTP via authentication app enabled - </ Text >
< Button onPress = {() => disableTOTP ()} title = "Remove" />
</ View >
)
}
const MFADisabled = () => {
return (
< View style = {{ flexDirection : 'row' , alignItems : 'center' }}>
< Text >Add TOTP via authentication app - </ Text >
< Button onPress = {() => router .push ( '/add-mfa' )} title = "Add" />
</ View >
)
}
return (
<>
< Text >Current MFA Settings</ Text >
< Text >Authenticator App</ Text >
{ user .totpEnabled ? < MFAEnabled /> : < MFADisabled />}
{ user .backupCodeEnabled && (
< View >
< Text >Backup Codes</ Text >
{loading && < Text >Loading...</ Text >}
{backupCodes && ! loading && (
< FlatList
data = { backupCodes .codes}
renderItem = {(code) => < Text >{ code .item}</ Text >}
keyExtractor = {(item) => item}
/>
)}
< Button onPress = {() => generateBackupCodes ()} title = "Regenerate Codes" />
</ View >
)}
</>
)
}
In the (dashboard)
group, create an add-mfa.tsx
file with the following code. This page adds the functionality for generating the QR code and backup codes.
app /(dashboard) /add-mfa.tsx import React from 'react'
import { useUser } from '@clerk/clerk-expo'
import { Link } from 'expo-router'
import { QrCodeSvg } from 'react-native-qr-svg'
import { FlatList , Button , Text , TextInput , View } from 'react-native'
import { BackupCodeResource , TOTPResource } from '@clerk/types'
type AddTotpSteps = 'add' | 'verify' | 'backupcodes' | 'success'
type DisplayFormat = 'qr' | 'uri'
function AddTOTPMfa ({ setStep } : { setStep : React . Dispatch < React . SetStateAction < AddTotpSteps >> }) {
const [ totp , setTotp ] = React .useState < TOTPResource | undefined >( undefined )
const [ displayFormat , setDisplayFormat ] = React .useState < DisplayFormat >( 'qr' )
const { user } = useUser ()
React .useEffect (() => {
void user
?.createTOTP ()
.then ((totp : TOTPResource ) => setTotp (totp))
.catch ((err) => console .error ( JSON .stringify (err , null , 2 )))
} , [])
return (
< View >
< Text >Add TOTP MFA</ Text >
{totp && displayFormat === 'qr' && (
<>
< View >
< QrCodeSvg value = { totp ?.uri || '' } frameSize = { 200 } />
</ View >
< Button title = "Use URI" onPress = {() => setDisplayFormat ( 'uri' )} />
</>
)}
{totp && displayFormat === 'uri' && (
<>
< View >
< Text >{ totp .uri}</ Text >
</ View >
< Button title = "Use QR Code" onPress = {() => setDisplayFormat ( 'qr' )} />
</>
)}
< Button title = "Verify" onPress = {() => setStep ( 'verify' )} />
< Button title = "Reset" onPress = {() => setStep ( 'add' )} />
</ View >
)
}
function VerifyMFA ({ setStep } : { setStep : React . Dispatch < React . SetStateAction < AddTotpSteps >> }) {
const [ code , setCode ] = React .useState ( '' )
const { user } = useUser ()
const verifyTotp = async (e : any ) => {
await user
?.verifyTOTP ({ code })
.then (() => setStep ( 'backupcodes' ))
.catch ((err) => console .error ( JSON .stringify (err , null , 2 )))
}
return (
<>
< Text >Verify MFA</ Text >
< TextInput
value = {code}
placeholder = "Enter code"
placeholderTextColor = "#666666"
onChangeText = {(c) => setCode (c)}
/>
< Button onPress = {verifyTotp} title = "Verify Code" />
< Button onPress = {() => setStep ( 'add' )} title = "Reset" />
</>
)
}
function BackupCodes ({ setStep } : { setStep : React . Dispatch < React . SetStateAction < AddTotpSteps >> }) {
const { user } = useUser ()
const [ backupCode , setBackupCode ] = React .useState < BackupCodeResource | undefined >( undefined )
React .useEffect (() => {
if (backupCode) {
return
}
void user
?.createBackupCode ()
.then ((backupCode : BackupCodeResource ) => setBackupCode (backupCode))
.catch ((err) => console .error ( JSON .stringify (err , null , 2 )))
} , [])
return (
<>
< Text >Verification was a success!</ Text >
{backupCode && (
< View >
< Text >
Save this list of backup codes somewhere safe in case you need to access your account in
an emergency
</ Text >
< FlatList
data = { backupCode . codes .map ((code) => ({
key : code ,
}))}
renderItem = {({ item }) => < Text >{ item .key}</ Text >}
/>
< Button title = "Finish" onPress = {() => setStep ( 'success' )} />
</ View >
)}
</>
)
}
function Success () {
return (
<>
< Text >Success</ Text >
< Text >You successfully added TOTP MFA via an authentication application</ Text >
</>
)
}
export default function AddMfaScreen () {
const [ step , setStep ] = React .useState < AddTotpSteps >( 'add' )
return (
<>
{step === 'add' && < AddTOTPMfa setStep = {setStep} />}
{step === 'verify' && < VerifyMFA setStep = {setStep} />}
{step === 'backupcodes' && < BackupCodes setStep = {setStep} />}
{step === 'success' && < Success />}
< Link href = "/account" >
< Text >Manage MFA</ Text >
</ Link >
</>
)
}