Webapp/src/authOptions.ts

62 lines
1.5 KiB
TypeScript
Raw Normal View History

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