import { AuthOptions, Session } from "next-auth"; export const authOptions: AuthOptions = { providers: [ { id: "oauth2", name: "oauth2", type: "oauth", clientId: process.env.OAUTH_CLIENT_ID, clientSecret: process.env.OAUTH_CLIENT_SECRET, wellKnown: process.env.OAUTH_WELL_KNOWN, authorization: { url: process.env.OAUTH_AUTHORIZATION_URL, params: { scope: "openid email profile", response_type: "code", }, }, checks: ["pkce", "state"], idToken: true, token: process.env.OAUTH_TOKEN_URL, userinfo: process.env.OAUTH_USERINFO_URL, issuer: process.env.OAUTH_ISSUER, jwks_endpoint: process.env.OAUTH_JWKS_ENDPOINT, profile(profile: Session["user"]) { return { id: profile.sub || profile.id, name: profile.name || profile.preferred_username || `${profile.given_name} ${profile.family_name}` }; }, }, ], callbacks: { async jwt({ token, account, user }) { if (account) { token.accessToken = account.access_token; token.refreshToken = account.refresh_token; token.expiresAt = Date.now() + account.expires_in * 1000000; } if (user) { token.userId = user.id; } return token; }, async session({ session, token }) { if (token) { session.user.id = token.userId; session.accessToken = token.accessToken; session.refreshToken = token.refreshToken; session.expiresAt = token.expiresAt; } return session; }, }, };