diff --git a/src/app/(main)/layout.tsx b/src/app/(main)/layout.tsx index e073b45..3ab342b 100644 --- a/src/app/(main)/layout.tsx +++ b/src/app/(main)/layout.tsx @@ -1,6 +1,6 @@ import AppBar from "@/components/AppBar"; import { getServerSession } from "next-auth"; -import { authOptions } from "@/config/authConfig"; +import { authOptions, SessionWithTokens } from "@/config/authConfig"; import { redirect } from "next/navigation"; import Box from "@mui/material/Box"; import { NAVIGATION_CONTENT_WIDTH } from "@/config/uiConfig"; @@ -10,23 +10,28 @@ import { AxiosProvider } from "@/app/(main)/axios/AxiosProvider"; import { SetupAxiosInterceptors } from "@/app/(main)/axios/axiosInstance"; import { CameraProvider } from "@/components/Cameras/CameraProvider"; import { UploadProvider } from "@/components/UploadProvider/UploadProvider"; +import SessionProviderWrapper from "@/components/SessionProviderWrapper/SessionProviderWrapper"; export default async function MainLayout({ children, }: { children: React.ReactNode; }) { - const session = await getServerSession(authOptions); + const session = await getServerSession(authOptions) as SessionWithTokens; if (!session?.user) { redirect("/login"); } + console.log(session?.user); + + // Verify if the session is valid if (session) { - SetupAxiosInterceptors(session?.accessToken); + SetupAxiosInterceptors(session?.accessToken ?? null); } return ( + @@ -50,6 +55,7 @@ export default async function MainLayout({ - + + ); } diff --git a/src/config/authConfig.ts b/src/config/authConfig.ts index 125f569..26ab31a 100644 --- a/src/config/authConfig.ts +++ b/src/config/authConfig.ts @@ -5,7 +5,8 @@ import { LOGIN_API_PATH } from "./api"; export interface SessionWithTokens extends Session { accessToken?: string; refreshToken?: string; - abilities: string[] + abilities: string[]; + id?: string | null } export const authOptions: AuthOptions = { @@ -26,6 +27,7 @@ export const authOptions: AuthOptions = { }); const user = await res.json(); + if (res.ok && user) { return user; @@ -42,18 +44,18 @@ export const authOptions: AuthOptions = { // Add the data from user to the token const { token, user } = params; const newToken = { ...token, ...user }; - return newToken; }, session({ session, token }) { const sessionWithToken: SessionWithTokens = { ...session, // Add the data from the token to the session + id: token.id as string | undefined, accessToken: token.accessToken as string | undefined, refreshToken: token.refreshToken as string | undefined, abilities: token.abilities as string[] }; - + console.log(sessionWithToken); return sessionWithToken; }, },