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

Commit

Permalink
fix(plugins/auth): fix invited users flow for first signins
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelrk committed Jul 9, 2024
1 parent 3193273 commit 88057aa
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
10 changes: 9 additions & 1 deletion lib/plugins/auth/routes/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ export const getRoutesByProvider = (
provider,
tokens.accessToken,
);
const userCurrent = await ctx.state.auth.getUser(userProvider.authId);
let userCurrent = await ctx.state.auth.getUser(userProvider.authId);
if (!userCurrent) {
// IMPORTANT: authId can be provisionally hard-coded to the unique email of the user
// when first being invited or when manually creating users in the database therefore
// we also attempt to find the user by email if the above query by authId fails
userCurrent = await ctx.state.auth.getInvitedUser(userProvider.email);
}

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

console.log({ userProvider, userCurrent, user });

if (!userCurrent) {
if (allowNewUserRegistration === true) {
await ctx.state.auth.createUser(user);
Expand Down
13 changes: 6 additions & 7 deletions lib/plugins/auth/utils/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,13 @@ export const createDatabaseAuth = (db: ReturnType<typeof database>): Auth => {
const user = await db.query.$users.findFirst({
where: eq($users.authId, authId),
}) as AuthUser;
if (user) return user;
// IMPORTANT: authId can be provisionally hard-coded to the unique email of the user
// when first being invited or when manually creating users in the database therefore
// we also attempt to find the user by email if the above query by authId fails
const userTemporary = await db.query.$users.findFirst({
where: eq($users.email, authId),
return user ?? null;
},
getInvitedUser: async (email: string) => {
const user = await db.query.$users.findFirst({
where: eq($users.authId, email),
}) as AuthUser;
return userTemporary ?? null;
return user ?? null;
},
getUserBySession: async (sessionId: string) => {
const session = await db.query.$sessions.findFirst({
Expand Down
6 changes: 6 additions & 0 deletions lib/plugins/auth/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ export type Auth = {
* Gets the user with the given authId from the database.
*/
getUser: (authId: string) => Promise<AuthUser | null>;
/**
* Gets the user with the given email from the database. This is used when
* the user is invited manually by explicitly setting "$users.authId" to his
* email addres in the database. This authId will be overwritten on first login.
*/
getInvitedUser: (email: string) => Promise<AuthUser | null>;
/**
* Gets the user with the given session ID from the database. The first attempt
* is done with eventual consistency. If that returns `null`, the second
Expand Down

0 comments on commit 88057aa

Please sign in to comment.