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

Commit

Permalink
wip(plugins/auth): add resolveUserData option to plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelrk committed May 30, 2024
1 parent 7da191a commit 1f3d87f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
6 changes: 2 additions & 4 deletions lib/plugins/auth/routes/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,15 @@ export const getRoutesByProvider = (
deletedAt: userCurrent?.deletedAt,
} as unknown as AuthUser;

// IMPORTANT: remove undefined values to prevent "Unsupported type of value"
// IMPORTANT: remove undefined values to prevent error "Unsupported type of value"
// and let the database handle setting defaults (e.g. null or anything else)
Object.keys(user).forEach((key) => {
console.log(key, user[key])
if (user[key] === undefined) delete user[key];
});

console.log({ userCurrent, user })

if (!userCurrent) {
await ctx.state.auth.createUser(user);
await ctx.state.auth.createUserSession(user, sessionId);
} else {
await ctx.state.auth.updateUser(user);
await ctx.state.auth.updateUserSession(user, sessionId);
Expand Down
20 changes: 7 additions & 13 deletions lib/plugins/auth/utils/adapters/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,21 @@ export const createDatabaseAuth = (db: ReturnType<typeof database>): Auth => {
return {
createUser: async (user: AuthUser) => {
user.id = id();
await db.transaction(async (tx) => {
await tx.insert($users).values(user).returning();
await tx.insert($sessions).values({
id: user.sessionId,
$userId: user.id,
});
});
await db.insert($users).values(user);
},
createUserSession: async (user: AuthUser, sessionId: string) => {
await db.insert($sessions).values({ id: sessionId, $userId: user.id });
},
updateUser: async (user: AuthUser) => {
user.updatedAt = new Date().toISOString();
await db.update($users).set(user).where(eq($users.id, user.id));
await db.query.$users.findFirst({ where: eq($users.id, user.id) });
},
updateUserSession: async (user: AuthUser, sessionId: string) => {
user.updatedAt = new Date().toISOString();
// IMPORTANT: this invalidates the old session and creates a new one.
// If multiple sessions per user are allowed, this should be adjusted.
await db.transaction(async (tx) => {
await tx.delete($sessions).where(eq($sessions.$userId, user.id));
await tx.insert($sessions).values({
id: sessionId,
$userId: user.id,
});
await tx.insert($sessions).values({ id: sessionId, $userId: user.id });
});
},
getUser: async (authId: string) => {
Expand Down
16 changes: 12 additions & 4 deletions lib/plugins/auth/utils/adapters/datastore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,27 @@ export const createDatastoreAuth = (kv = KV): Auth => {
user.updatedAt = user.createdAt;
user.deletedAt = "";
const usersKey = ["users", user.authId];
const usersBySessionKey = ["usersBySession", user.sessionId];

const atomicOp = kv.atomic()
.check({ key: usersKey, versionstamp: null })
.set(usersKey, user);

const res = await atomicOp.commit();
if (!res.ok) throw new Error("Failed to create user");
},
createUserSession: async (user: AuthUser, sessionId: string) => {
user.updatedAt = new Date().toISOString();
const usersBySessionKey = ["usersBySession", sessionId];

const atomicOp = kv.atomic()
.check({ key: usersBySessionKey, versionstamp: null })
.set(usersKey, user)
.set(usersBySessionKey, user);

const res = await atomicOp.commit();
if (!res.ok) throw new Error("Failed to create user");
if (!res.ok) throw new Error("Failed to create user session");
},
updateUser: async (user: AuthUser) => {
user.updatedAt = new Date().toISOString();
user.updatedAt ||= new Date().toISOString();
const usersKey = ["users", user.authId];
const usersBySessionKey = ["usersBySession", user.sessionId];

Expand Down

0 comments on commit 1f3d87f

Please sign in to comment.