Replies: 2 comments 2 replies
-
Were you able to find a solution? Currently running into the same issue. |
Beta Was this translation helpful? Give feedback.
1 reply
-
For anyone else that runs into this issue, like @peter-y-w I also solved it using cookies: Here's my implementation:
import type { NextApiRequest, NextApiResponse } from 'next'
import NextAuth from 'next-auth'
import KeycloakProvider from 'next-auth/providers/keycloak'
import { cookies } from 'next/headers'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const issuerCookie = cookies().get('kc_issuer')
return await NextAuth(req, res, {
providers: [
KeycloakProvider({
clientId: process.env.KEYCLOAK_ID as string,
clientSecret: '',
issuer:
issuerCookie?.value ??
YOUR_FALLBACK_ISSUER_URL,
}),
],
})
}
export { handler as GET, handler as POST }
'use client'
import { useSession, signIn, signOut } from 'next-auth/react'
import { useEffect } from 'react'
import Cookies from 'js-cookie'
export const Keycloak = () => {
const { data: session } = useSession()
useEffect(() => {
Cookies.set(
'kc_issuer',
YOUR_DYNAMIC_KEYCLOAK_ISSUER_URL,
)
}, [])
return (
<button
type="button"
onClick={() =>
signIn('keycloak')
}
>
Sign in
</button>
)
} with next-auth@5 (beta) export const { handlers, auth, signIn, signOut } = NextAuth(() => {
const issuerCookie = cookies().get('kc_issuer')
return {
providers: [
Keycloak({
clientId: process.env.KEYCLOAK_CLIENT_ID ?? '',
clientSecret: 'REQUIRED_BY_NEXT_AUTH_BUT_UNUSED',
issuer: issuerCookie?.value ?? ''
}),
],
}
}) |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
Hi, I'm using the Keycloak Provider and NextAuth 4.23.1 with the Next13 app router. My constraint is that users live in different Keycloak realms.
My app takes a user's email input and uses a separate Keycloak API call to figure out which realm the user is on. I then use NextAuth's advanced initialisation and third parameter of
signIn()
to pass this realm to[...nextauth]/route.ts
:So far, so good. I can use
getAuthOptions()
everywhere I would otherwise importauthOptions
.The problem now is the GET request.
As you can see, I can grab realm from the
req
passed in if it exists there. However, the issue is there seems to be a call to GET where this just isn't available, and it breaks the app:This GET call happens after authentication with Keycloak and Keycloak redirects back to my app. However it seems to be a call that doesn't take Keycloak's response initially. If I hardcode the realm value in the GET call to progress past this initial call, I can see that
iss
does exist on thetoken.profile
in subsequent GET calls, where I can grab the value. Surely there must be a way of sending this data into the initial GET call?Thanks in advance for your help.
Additional information
Example
No response
Beta Was this translation helpful? Give feedback.
All reactions