This repository was archived by the owner on Sep 22, 2024. It is now read-only.
File tree 3 files changed +21
-8
lines changed
3 files changed +21
-8
lines changed Original file line number Diff line number Diff line change @@ -41,7 +41,13 @@ export const getRoutesByProvider = (
41
41
provider ,
42
42
tokens . accessToken ,
43
43
) ;
44
- const userCurrent = await ctx . state . auth . getUser ( userProvider . authId ) ;
44
+ let userCurrent = await ctx . state . auth . getUser ( userProvider . authId ) ;
45
+ if ( ! userCurrent ) {
46
+ // IMPORTANT: authId can be provisionally hard-coded to the unique email of the user
47
+ // when first being invited or when manually creating users in the database therefore
48
+ // we also attempt to find the user by email if the above query by authId fails
49
+ userCurrent = await ctx . state . auth . getInvitedUser ( userProvider . email ) ;
50
+ }
45
51
46
52
// IMPORTANT: must explicitly set all properties to prevent "undefined" values
47
53
const user = {
@@ -66,6 +72,8 @@ export const getRoutesByProvider = (
66
72
if ( user [ key ] === undefined ) delete user [ key ] ;
67
73
} ) ;
68
74
75
+ console . log ( { userProvider, userCurrent, user } ) ;
76
+
69
77
if ( ! userCurrent ) {
70
78
if ( allowNewUserRegistration === true ) {
71
79
await ctx . state . auth . createUser ( user ) ;
Original file line number Diff line number Diff line change @@ -31,14 +31,13 @@ export const createDatabaseAuth = (db: ReturnType<typeof database>): Auth => {
31
31
const user = await db . query . $users . findFirst ( {
32
32
where : eq ( $users . authId , authId ) ,
33
33
} ) as AuthUser ;
34
- if ( user ) return user ;
35
- // IMPORTANT: authId can be provisionally hard-coded to the unique email of the user
36
- // when first being invited or when manually creating users in the database therefore
37
- // we also attempt to find the user by email if the above query by authId fails
38
- const userTemporary = await db . query . $users . findFirst ( {
39
- where : eq ( $users . email , authId ) ,
34
+ return user ?? null ;
35
+ } ,
36
+ getInvitedUser : async ( email : string ) => {
37
+ const user = await db . query . $users . findFirst ( {
38
+ where : eq ( $users . authId , email ) ,
40
39
} ) as AuthUser ;
41
- return userTemporary ?? null ;
40
+ return user ?? null ;
42
41
} ,
43
42
getUserBySession : async ( sessionId : string ) => {
44
43
const session = await db . query . $sessions . findFirst ( {
Original file line number Diff line number Diff line change @@ -44,6 +44,12 @@ export type Auth = {
44
44
* Gets the user with the given authId from the database.
45
45
*/
46
46
getUser : ( authId : string ) => Promise < AuthUser | null > ;
47
+ /**
48
+ * Gets the user with the given email from the database. This is used when
49
+ * the user is invited manually by explicitly setting "$users.authId" to his
50
+ * email addres in the database. This authId will be overwritten on first login.
51
+ */
52
+ getInvitedUser : ( email : string ) => Promise < AuthUser | null > ;
47
53
/**
48
54
* Gets the user with the given session ID from the database. The first attempt
49
55
* is done with eventual consistency. If that returns `null`, the second
You can’t perform that action at this time.
0 commit comments