Search code examples
next.jsazure-active-directoryazure-ad-b2cnext-auth

Next Auth Azure Ad B2C signout problem session kills on app but not on azure AD


I am integrating Next Auth with Azure AD B2C i am able to create a login session when i login or signup on azure AD but when i signout using next Auth i am not signing out of azure AD and it automatically signins me in till the azure AD session expires that is 1 day after a day i will get option again to sign in.

Tried following documentation but got no result any help would be appreciated! The thing is next auth provides solution for signin sign up and stuff but the session at my app gets killed on signout but it kills the reason for MFA(multi factor authentication) if azure AD session is maintained which can be used again and signed in without credentials to my app!


Solution

  • You can either..

    1. Force users to re-enter their credentials on each login

      Reference: Next-Auth "Additional parameters" documentation

      signIn("azure-ad-b2c", null, { prompt: "login" })
      
    2. Defer calling signOut() until after you redirect to B2C, as B2C handles clearing its session

      Reference: Benjamin Fox Blog, Azure B2C with Next-Auth

    <button 
      href={`https://${process.env.AUTH_TENANT_NAME}.b2clogin.com/${process.env.AUTH_TENANT_NAME}.onmicrosoft.com/${process.env.USER_FLOW}/oauth2/v2.0/logout?post_logout_redirect_uri=${process.env.NEXTAUTH_URL}/auth/signout`}
    >
      Sign Out
    </button>

    where the /auth/signout page calls Next-Auth's signOut()