feat: add OAUTH_SCOPES to environment types and update authOptions for dynamic scope handling

This commit is contained in:
Rémi 2025-01-07 10:49:08 +01:00
parent 1c897648f7
commit 3ac29ae909
4 changed files with 10 additions and 7 deletions

View File

@ -9,4 +9,5 @@ OAUTH_ISSUER=
OAUTH_AUTHORIZATION_URL= OAUTH_AUTHORIZATION_URL=
OAUTH_TOKEN_URL= OAUTH_TOKEN_URL=
OAUTH_USERINFO_URL= OAUTH_USERINFO_URL=
OAUTH_JWKS_ENDPOINT= OAUTH_JWKS_ENDPOINT=
OAUTH_SCOPES="openid email profile offline_access"

View File

@ -8,5 +8,6 @@ declare namespace NodeJS {
OAUTH_USERINFO_URL: string; OAUTH_USERINFO_URL: string;
OAUTH_ISSUER: string; OAUTH_ISSUER: string;
OAUTH_JWKS_ENDPOINT: string; OAUTH_JWKS_ENDPOINT: string;
OAUTH_SCOPES: string;
} }
} }

View File

@ -56,7 +56,7 @@ declare module "next-auth/jwt" {
accessToken: string; accessToken: string;
accessTokenExpires: Moment; accessTokenExpires: Moment;
refreshToken: string; refreshToken: string;
refreshTokenExpires: Moment; refreshTokenExpires: Moment | undefined;
error?: string; error?: string;
user: User | AdapterUser; user: User | AdapterUser;
} }

View File

@ -18,7 +18,7 @@ export const authOptions: AuthOptions = {
authorization: { authorization: {
url: process.env.OAUTH_AUTHORIZATION_URL, url: process.env.OAUTH_AUTHORIZATION_URL,
params: { params: {
scope: "openid email profile", scope: process.env.OAUTH_SCOPES,
response_type: "code", response_type: "code",
}, },
}, },
@ -49,10 +49,11 @@ export const authOptions: AuthOptions = {
token.refreshToken = account.refresh_token; token.refreshToken = account.refresh_token;
token.accessTokenExpires = moment.unix(account.expires_at); token.accessTokenExpires = moment.unix(account.expires_at);
token.refreshTokenExpires = moment().add(
account.refresh_expires_in, token.refreshTokenExpires =
"seconds", account.refresh_expires_in != 0
); ? moment().add(account.refresh_expires_in, "seconds")
: undefined;
const accessTokenDecode = jsonwebtoken.decode( const accessTokenDecode = jsonwebtoken.decode(
account.access_token, account.access_token,