Force multi-factor authentication (MFA) for all users
By default, Clerk does not enforce multi-factor authentication (MFA) for all users. This guide demonstrates how to force MFA for all users by using 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.
Every User object has a two_factor_enabled property that indicates whether the user has MFA enabled. Store this property in the session token so that you can check it in your clerkMiddleware().
In the Clerk Dashboard, navigate to the Sessions page.
In the Customize session token section, select Edit.
In the modal that opens, add a key-value pair to the Claims object. The key can be any string, but the value must be the user.two_factor_enabled property, as shown in the following example.
Update your clerkMiddleware() to check if the user has MFA enabled.
Next.js
Astro
Nuxt
By default, the Nuxt SDK automatically adds the clerkMiddleware() helper to your Nuxt application. To manually configure the middleware, in your nuxt.config.ts file, under the clerk property, set skipServerMiddleware: true.
Then, in your server/middleware/clerk.ts file, add the following code: