Skip to content

Commit

Permalink
fix code following next-auth update
Browse files Browse the repository at this point in the history
  • Loading branch information
gpelouze committed Aug 8, 2024
1 parent d4192f5 commit 77f99f3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 81 deletions.
2 changes: 1 addition & 1 deletion vre-panel/components/VLabInstances.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const VLabInstances: React.FC<Props> = ({vlab, slug}) => {
return
}

const username = session.data.user.name
const username = session.data.user?.name

const requestOptions: RequestInit = {
method: "POST",
Expand Down
78 changes: 5 additions & 73 deletions vre-panel/next-auth.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
16 changes: 9 additions & 7 deletions vre-panel/pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 77f99f3

Please sign in to comment.