author avatar

adithya.hebbar

Tue Aug 27 2024

To logout from Keycloak using the signOut function in NextAuth, you need to override the default behavior to ensure that the user is properly logged out from Keycloak as well. Here's how you can update your signOut function:

async signOut({ token }) {
  if (token.provider === "keycloak") {
    const issuerUrl = authOptions.providers.find((p) => p.id === "keycloak")
      .options!.issuer!;
    const logOutUrl = new URL(
      `${issuerUrl}/protocol/openid-connect/logout`
    );
    logOutUrl.searchParams.set("id_token_hint", token.id_token!);
    await fetch(logOutUrl);
  }
}

#keycloak #nextauth #nextjs #js