mirror of
https://github.com/Monadical-SAS/reflector.git
synced 2025-12-20 20:29:06 +00:00
compile fix
This commit is contained in:
@@ -3,7 +3,11 @@
|
||||
import { createContext, useContext, useEffect } from "react";
|
||||
import { useSession as useNextAuthSession } from "next-auth/react";
|
||||
import { configureApiAuth } from "./apiClient";
|
||||
import { assertExtendedToken, CustomSession } from "./types";
|
||||
import {
|
||||
assertExtendedToken,
|
||||
assertExtendedTokenAndUserId,
|
||||
CustomSession,
|
||||
} from "./types";
|
||||
|
||||
type AuthContextType =
|
||||
| { status: "loading" }
|
||||
@@ -19,7 +23,7 @@ const AuthContext = createContext<AuthContextType | undefined>(undefined);
|
||||
|
||||
export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
const { data: session, status } = useNextAuthSession();
|
||||
const customSession = session ? assertExtendedToken(session) : null;
|
||||
const customSession = session ? assertExtendedTokenAndUserId(session) : null;
|
||||
|
||||
const contextValue: AuthContextType =
|
||||
status === "loading"
|
||||
|
||||
@@ -83,7 +83,6 @@ export const authOptions: AuthOptions = {
|
||||
return await lockedRefreshAccessToken(token);
|
||||
},
|
||||
async session({ session, token }) {
|
||||
// TODO no as
|
||||
const extendedToken = token as JWTWithAccessToken;
|
||||
return {
|
||||
...session,
|
||||
@@ -91,7 +90,7 @@ export const authOptions: AuthOptions = {
|
||||
accessTokenExpires: extendedToken.accessTokenExpires,
|
||||
error: extendedToken.error,
|
||||
user: {
|
||||
id: extendedToken.sub,
|
||||
id: assertExists(extendedToken.sub),
|
||||
name: extendedToken.name,
|
||||
email: extendedToken.email,
|
||||
},
|
||||
|
||||
@@ -13,6 +13,9 @@ export interface CustomSession extends Session {
|
||||
accessToken: string;
|
||||
accessTokenExpires: number;
|
||||
error?: string;
|
||||
user: Session["user"] & {
|
||||
id: string;
|
||||
};
|
||||
}
|
||||
|
||||
// assumption that JWT is JWTWithAccessToken - not ideal, TODO find a reason we have to do that
|
||||
@@ -39,3 +42,25 @@ export const assertExtendedToken = <T>(
|
||||
}
|
||||
throw new Error("Token is not extended with access token");
|
||||
};
|
||||
|
||||
export const assertExtendedTokenAndUserId = <U, T extends { user?: U }>(
|
||||
t: T,
|
||||
): T & {
|
||||
accessTokenExpires: number;
|
||||
accessToken: string;
|
||||
user: U & {
|
||||
id: string;
|
||||
};
|
||||
} => {
|
||||
const extendedToken = assertExtendedToken(t);
|
||||
if (typeof (extendedToken.user as any)?.id === "string") {
|
||||
return t as T & {
|
||||
accessTokenExpires: number;
|
||||
accessToken: string;
|
||||
user: U & {
|
||||
id: string;
|
||||
};
|
||||
};
|
||||
}
|
||||
throw new Error("Token is not extended with user id");
|
||||
};
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useEffect } from "react";
|
||||
import { useSession as useNextAuthSession } from "next-auth/react";
|
||||
import { Session } from "next-auth";
|
||||
import { useAuth } from "./AuthProvider";
|
||||
|
||||
const assertUserId = <T>(u: T): T & { id: string } => {
|
||||
if (typeof (u as { id: string }).id !== "string")
|
||||
throw new Error("Expected user.id to be a string");
|
||||
return u as T & { id: string };
|
||||
};
|
||||
|
||||
// the current assumption in useSessionUser is that "useNextAuthSession" also returns user.id, although useNextAuthSession documentation states it doesn't
|
||||
// the hook is to isolate the potential impact and to document this behaviour
|
||||
export default function useUserId() {
|
||||
const auth = useAuth();
|
||||
return auth.status === "authenticated" ? assertUserId(auth.user) : null;
|
||||
}
|
||||
Reference in New Issue
Block a user