From 77f99f33211bc2ab22c19df437ed018bd6a13b3d Mon Sep 17 00:00:00 2001 From: Gabriel Pelouze Date: Thu, 8 Aug 2024 16:40:06 +0200 Subject: [PATCH] fix code following next-auth update --- vre-panel/components/VLabInstances.tsx | 2 +- vre-panel/next-auth.d.ts | 78 ++--------------------- vre-panel/pages/api/auth/[...nextauth].ts | 16 +++-- 3 files changed, 15 insertions(+), 81 deletions(-) diff --git a/vre-panel/components/VLabInstances.tsx b/vre-panel/components/VLabInstances.tsx index 727e76b..394f3dd 100644 --- a/vre-panel/components/VLabInstances.tsx +++ b/vre-panel/components/VLabInstances.tsx @@ -31,7 +31,7 @@ const VLabInstances: React.FC = ({vlab, slug}) => { return } - const username = session.data.user.name + const username = session.data.user?.name const requestOptions: RequestInit = { method: "POST", diff --git a/vre-panel/next-auth.d.ts b/vre-panel/next-auth.d.ts index 1fc0c0f..568ba3b 100644 --- a/vre-panel/next-auth.d.ts +++ b/vre-panel/next-auth.d.ts @@ -5,86 +5,18 @@ declare module 'next-auth' { * Returned by `useSession`, `getSession` and received as a prop on the `Provider` React Context */ interface Session { - user: { - sub: string; - email_verified: boolean; - name: string; - preferred_username: string; - given_name: string; - family_name: string; - email: string; - id: string; - org_name?: string; - telephone?: string; - }; + accessToken?: string; + accessTokenExpiry?: number; error: string; - accessTokenExpiry: number; - } - /** - * The shape of the user object returned in the OAuth providers' `profile` callback, - * or the second parameter of the `session` callback, when using a database. - */ - interface User { - sub: string; - email_verified: boolean; - name: string; - telephone: string; - preferred_username: string; - org_name: string; - given_name: string; - family_name: string; - email: string; - id: string; - } - /** - * Usually contains information about the provider being used - * and also extends `TokenSet`, which is different tokens returned by OAuth Providers. - */ - interface Account { - provider: string; - type: string; - id: string; - accessToken: string; - accessTokenExpires?: any; - refreshToken: string; - idToken: string; - access_token: string; - expires_at: number; - refresh_expires_in: number; - refresh_token: string; - token_type: string; - id_token: string; - 'not-before-policy': number; - session_state: string; - scope: string; - } - /** The OAuth profile returned from your provider */ - interface Profile { - sub: string; - email_verified: boolean; - name: string; - telephone: string; - preferred_username: string; - org_name: string; - given_name: string; - family_name: string; - email: string; } } declare module 'next-auth/jwt' { /** Returned by the `jwt` callback and `getToken`, when using JWT sessions */ interface JWT { - name: string; - email: string; - sub: string; - name: string; - email: string; - sub: string; - accessToken: string; - refreshToken: string; - accessTokenExpiry: number; - user: User; + accessToken?: string; + refreshToken?: string; + accessTokenExpiry?: number; error: string; } } \ No newline at end of file diff --git a/vre-panel/pages/api/auth/[...nextauth].ts b/vre-panel/pages/api/auth/[...nextauth].ts index b137ef9..66ef9fb 100644 --- a/vre-panel/pages/api/auth/[...nextauth].ts +++ b/vre-panel/pages/api/auth/[...nextauth].ts @@ -1,5 +1,6 @@ import axios from "axios"; import NextAuth from "next-auth" +import { Session } from "next-auth"; import { JWT } from "next-auth/jwt"; import KeycloakProvider from "next-auth/providers/keycloak" import type { NextApiRequest, NextApiResponse } from 'next' @@ -64,18 +65,19 @@ export default (req : NextApiRequest, res: NextApiResponse) => { token.accessTokenExpiry = account.expires_at; } - const expDate = new Date(token.accessTokenExpiry * 1e3) - const nowDate = new Date() - const tokenExpired = (expDate < nowDate) - - if (tokenExpired) { - token = await refreshAccessToken(token); + if (token.accessTokenExpiry) { + const expDate = new Date(token.accessTokenExpiry * 1e3) + const nowDate = new Date() + const tokenExpired = (expDate < nowDate) + if (tokenExpired) { + token = await refreshAccessToken(token); + } } return token; }, - async session({ session, token }) { + async session({ session, token }: { session: Session, token: JWT }) { if (token) { session.error = token.error; session.accessToken = token.accessToken;