feat: enhance authentication flow by redirecting unauthenticated users and updating token scope

This commit is contained in:
Rémi 2025-01-06 17:39:27 +01:00
parent fce459679c
commit b467ae704c
2 changed files with 5 additions and 2 deletions

View File

@ -1,6 +1,8 @@
import { authOptions } from "@/authOptions";
import axios from "axios"; import axios from "axios";
import moment, { Moment } from "moment"; import moment, { Moment } from "moment";
import { getSession } from "next-auth/react"; import { getSession } from "next-auth/react";
import { redirect } from "next/navigation";
moment.locale("fr"); moment.locale("fr");
@ -35,7 +37,7 @@ axiosInstance.interceptors.request.use(async (config) => {
try { try {
const session = await getSession(); const session = await getSession();
if (!session) { if (!session) {
throw new Error("User is not authenticated"); redirect(authOptions.pages!.signIn!);
} }
cachedAccessToken = session.accessToken; cachedAccessToken = session.accessToken;

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 offline_access", scope: "openid email profile",
response_type: "code", response_type: "code",
}, },
}, },
@ -28,6 +28,7 @@ export const authOptions: AuthOptions = {
userinfo: process.env.OAUTH_USERINFO_URL, userinfo: process.env.OAUTH_USERINFO_URL,
issuer: process.env.OAUTH_ISSUER, issuer: process.env.OAUTH_ISSUER,
jwks_endpoint: process.env.OAUTH_JWKS_ENDPOINT, jwks_endpoint: process.env.OAUTH_JWKS_ENDPOINT,
wellKnown: `${process.env.OAUTH_ISSUER}/.well-known/openid-configuration`,
profile(profile: Session["user"]) { profile(profile: Session["user"]) {
return { return {
id: profile.sub || profile.id, id: profile.sub || profile.id,