import { NuxtAuthHandler } from "#auth";
import KeycloakProvider, {
type KeycloakProfile,
} from "next-auth/providers/keycloak";
const rtc = useRuntimeConfig();
export default NuxtAuthHandler({
secret: "somethingyoushoulddefinitelychangehere",
pages: {
signIn: "/login",
},
providers: [
// @ts-expect-error You need to use .default here for it to work during SSR. May be fixed via Vite at some point
KeycloakProvider.default({
clientId: rtc.keycloakClientId,
clientSecret: rtc.keycloakClientSecret,
issuer: rtc.keycloakIssuer,
profile(profile: KeycloakProfile) {
return {
uid: profile.sub,
name: profile.name ?? profile.preferred_username,
email: profile.email,
roles: profile.roles,
};
},
})
],
callbacks: {
// Callback when the JWT is created / updated, see https://next-auth.js.org/configuration/callbacks#jwt-callback
jwt: async ({ token, user }) => {
const isSignIn = user ? true : false;
if (isSignIn) {
token.uid = user ? user.uid || "" : "";
token.roles = user ? user.roles || [] : [];
}
return Promise.resolve(token);
},
// Callback whenever session is checked, see https://next-auth.js.org/configuration/callbacks#session-callback
session: async ({ session, token }) => {
session.roles = token.roles;
session.uid = token.uid;
return Promise.resolve(session);
},
},
});