BETTER AUTH KEYCLOACK JWT
Current Setup
- Frontend: Next.js App Router with Better Auth (stateless mode)
- Authentication Provider: Keycloak (OAuth2/OIDC)
- Goal:
- Store Keycloak OAuth tokens (idToken, accessToken, refreshToken) in Better Auth JWT
- Send JWT to backend API for user verification
- Use idToken for Keycloak logout
Better Auth does NOT expose OAuth tokens in getSession() response by default.
When using Better Auth with genericOAuth plugin:
What We Need
Why This Matters
- Backend Authentication: We need to send accessToken or a derived JWT to the backend API
- Keycloak Logout: We need idToken to properly logout from Keycloak using id_token_hint
- Token Refresh: We need refreshToken to refresh expired access tokens
Specifically:
- Store them in the JWT session cookie (encrypted)
- Access them via getSession() API
- Send accessToken to backend API
- Use idToken for Keycloak logout
We need a way to: