Skip to content
This repository was archived by the owner on Sep 22, 2024. It is now read-only.

Commit 88057aa

Browse files
committed
fix(plugins/auth): fix invited users flow for first signins
1 parent 3193273 commit 88057aa

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

lib/plugins/auth/routes/mod.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,13 @@ export const getRoutesByProvider = (
4141
provider,
4242
tokens.accessToken,
4343
);
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+
}
4551

4652
// IMPORTANT: must explicitly set all properties to prevent "undefined" values
4753
const user = {
@@ -66,6 +72,8 @@ export const getRoutesByProvider = (
6672
if (user[key] === undefined) delete user[key];
6773
});
6874

75+
console.log({ userProvider, userCurrent, user });
76+
6977
if (!userCurrent) {
7078
if (allowNewUserRegistration === true) {
7179
await ctx.state.auth.createUser(user);

lib/plugins/auth/utils/adapter.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,13 @@ export const createDatabaseAuth = (db: ReturnType<typeof database>): Auth => {
3131
const user = await db.query.$users.findFirst({
3232
where: eq($users.authId, authId),
3333
}) 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),
4039
}) as AuthUser;
41-
return userTemporary ?? null;
40+
return user ?? null;
4241
},
4342
getUserBySession: async (sessionId: string) => {
4443
const session = await db.query.$sessions.findFirst({

lib/plugins/auth/utils/types.ts

+6
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ export type Auth = {
4444
* Gets the user with the given authId from the database.
4545
*/
4646
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>;
4753
/**
4854
* Gets the user with the given session ID from the database. The first attempt
4955
* is done with eventual consistency. If that returns `null`, the second

0 commit comments

Comments
 (0)