@@ -17,8 +17,21 @@ import {
1717 createE2ESession ,
1818 getE2ECurrentUser ,
1919} from "@/lib/test-harness/auth" ;
20+ import {
21+ readCachedAuthUserById ,
22+ syncCachedAuthUser ,
23+ } from "@/lib/auth/user-cache" ;
2024import { findUserById } from "@/lib/auth/users" ;
21- import type { AuthUser } from "@/types/db" ;
25+ import type { AppSessionIdentity , AuthUser } from "@/types/db" ;
26+
27+ export type AuthSessionIdentity = {
28+ id : string ;
29+ isInternalAdmin : boolean ;
30+ isSignupComplete : boolean ;
31+ nickname : string | null ;
32+ provider ?: string ;
33+ sessionIdentity ?: AppSessionIdentity ;
34+ } ;
2235
2336const readAuthSession = cache ( async ( ) : Promise < Session | null > => {
2437 const session = await getAuthSessionSafe ( ) ;
@@ -41,14 +54,44 @@ async function findCurrentUserFromSession(
4154 return null ;
4255 }
4356
44- return findUserById ( session . user . id ) ;
57+ const cachedUser = await readCachedAuthUserById ( session . user . id ) ;
58+ if ( cachedUser !== undefined ) {
59+ return cachedUser ;
60+ }
61+
62+ const user = await findUserById ( session . user . id ) ;
63+ await syncCachedAuthUser ( session . user . id , user ) ;
64+ return user ;
4565}
4666
4767const readCurrentUser = cache ( async ( ) : Promise < AuthUser | null > =>
4868 findCurrentUserFromSession ( await readAuthSession ( ) ) ,
4969) ;
5070
71+ const readAuthIdentity = cache ( async ( ) : Promise < AuthSessionIdentity | null > => {
72+ const session = await readAuthSession ( ) ;
73+ if ( ! session ?. user ?. id ) {
74+ return null ;
75+ }
76+
77+ return {
78+ id : session . user . id ,
79+ isInternalAdmin : session . user . isInternalAdmin === true ,
80+ isSignupComplete : session . user . isSignupComplete === true ,
81+ nickname : typeof session . user . nickname === "string" ? session . user . nickname : null ,
82+ provider :
83+ typeof session . user . provider === "string"
84+ ? session . user . provider
85+ : undefined ,
86+ sessionIdentity :
87+ typeof session . user . sessionIdentity === "string"
88+ ? session . user . sessionIdentity
89+ : undefined ,
90+ } ;
91+ } ) ;
92+
5193export const getAuthSession = readAuthSession ;
94+ export const getAuthIdentity = readAuthIdentity ;
5295export const getCurrentUser = readCurrentUser ;
5396
5497type RequireCurrentUserOptions = {
0 commit comments