46 lines
1.1 KiB
TypeScript
46 lines
1.1 KiB
TypeScript
|
|
import NextAuth from 'next-auth'
|
||
|
|
import Keycloak from 'next-auth/providers/keycloak'
|
||
|
|
|
||
|
|
export const { handlers, auth, signIn, signOut } = NextAuth({
|
||
|
|
providers: [
|
||
|
|
Keycloak({
|
||
|
|
clientId: process.env.KEYCLOAK_CLIENT_ID!,
|
||
|
|
clientSecret: process.env.KEYCLOAK_CLIENT_SECRET!,
|
||
|
|
issuer: process.env.KEYCLOAK_ISSUER!,
|
||
|
|
}),
|
||
|
|
],
|
||
|
|
pages: {
|
||
|
|
signIn: '/login',
|
||
|
|
},
|
||
|
|
callbacks: {
|
||
|
|
authorized({ auth }) {
|
||
|
|
return !!auth?.user
|
||
|
|
},
|
||
|
|
async jwt({ token, account, profile }) {
|
||
|
|
if (account) {
|
||
|
|
token.accessToken = account.access_token
|
||
|
|
token.idToken = account.id_token
|
||
|
|
token.expiresAt = account.expires_at
|
||
|
|
token.refreshToken = account.refresh_token
|
||
|
|
}
|
||
|
|
if (profile) {
|
||
|
|
token.name = profile.name
|
||
|
|
token.email = profile.email
|
||
|
|
}
|
||
|
|
return token
|
||
|
|
},
|
||
|
|
async session({ session, token }) {
|
||
|
|
if (token.accessToken) {
|
||
|
|
session.accessToken = token.accessToken as string
|
||
|
|
}
|
||
|
|
return session
|
||
|
|
},
|
||
|
|
},
|
||
|
|
})
|
||
|
|
|
||
|
|
declare module 'next-auth' {
|
||
|
|
interface Session {
|
||
|
|
accessToken?: string
|
||
|
|
}
|
||
|
|
}
|