[v4] Get oauth access_token or custom claims (keycloak provider) #3087
Replies: 2 comments
-
Any update on this? |
Beta Was this translation helpful? Give feedback.
-
Hi there! Having the same problem, I started tinkering around, and it is actually your message that put me on the right way. As you saw using debug mode, next-auth returns everything it gets from the auth provider in the You can do it like so: import NextAuth from "next-auth";
import KeycloakProvider from "next-auth/providers/keycloak";
export default NextAuth({
providers: [
KeycloakProvider({
clientId: process.env.KEYCLOAK_ID,
clientSecret: process.env.KEYCLOAK_SECRET,
issuer: process.env.KEYCLOAK_ISSUER,
}),
],
pages: {
signIn: "/auth/signin",
},
jwt: {
secret: process.env.JWT_SECRET,
},
// debug: true,
callbacks: {
jwt: ({ token, account }) => {
if (account != null) {
token.idToken = account.id_token // comes from keycloak; you can do the same with every other property returned by it
return token
}
},
session: ({ session, token }) => {
if (token?.idToken != null) {
session.idToken = token.idToken // you can now access idToken anywhere in your app with getSession()
}
return session
}
}
}); Hope it helped! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Question 💬
When using getToken() method in either a next.js v12 middleware, or any page with serverSideProps I'm able to access certain properties like email, name or sub. Setting the raw option to true I get a JWT Token without payload (something like: [...]MjU2R0NNIn0..ATQhw0Hn[...]) - there is just no payload at all.
const token = await getToken({ req, secret, raw: true })
If I turn on debug: true in NextAuth config (/pages/api/auth/[...nextauth].ts) I can see a complete access_token and id_token, as well as a complete set informations (OAuthProfile) including custom claims I added in Keycloak.
I've used the next-auth-example as starting point and everything works like a charm with Keycloak as provider (I'm able to login and logout etc.) - I'm just not able to get my custom mappers/claims.
How to reproduce ☕️
pages/api/auth/[...nextauth].ts
pages/_middleware.ts:
Contributing 🙌🏽
Yes, I am willing to help answer this question in a PR
Beta Was this translation helpful? Give feedback.
All reactions