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 b5d84f5 commit 7da191a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 27 deletions.
41 changes: 22 additions & 19 deletions lib/plugins/auth/routes/mod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { PluginRoute } from "$fresh/server.ts";
import { deepMerge } from "../../../deps/std/collections/deep_merge.ts";
import type { AuthConfig } from "../plugin.ts";
import {
getAuthConfig,
Expand Down Expand Up @@ -40,31 +39,35 @@ export const getRoutesByProvider = (
);
const userCurrent = await ctx.state.auth.getUser(userProvider.authId);

// IMPORTANT: must explicitly set all properties to prevent "undefined" values
const user = {
id: userCurrent?.id,
sessionId,
provider: userProvider.provider,
authId: userProvider.authId,
name: userProvider.name,
email: userProvider.email,
avatar: userProvider.avatar,
projects: {
[Deno.env.get("NETZO_PROJECT_ID")!]: {
roles: {},
},
},
data: {},
provider: userProvider?.provider,
authId: userProvider?.authId,
name: userProvider?.name,
email: userProvider?.email,
avatar: userProvider?.avatar,
data: options?.resolveUserData?.(userCurrent ?? {}, req, ctx) ?? {},
createdAt: userCurrent?.createdAt,
updatedAt: userCurrent?.updatedAt,
deletedAt: userCurrent?.deletedAt,
} as unknown as AuthUser;

// IMPORTANT: remove undefined values to prevent "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) {
Object.keys(user).forEach((key) => {
// remove undefined values to let database handle setting defaults
if (user[key] === undefined) delete user[key];
});
await ctx.state.auth.createUser(user);
} else {
const data = deepMerge(user, userCurrent);
await ctx.state.auth.updateUser(data);
await ctx.state.auth.updateUserSession(data, sessionId);
await ctx.state.auth.updateUser(user);
await ctx.state.auth.updateUserSession(user, sessionId);
}

return response;
Expand Down
9 changes: 2 additions & 7 deletions lib/plugins/auth/utils/adapters/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,13 @@ export const createDatabaseAuth = (db: ReturnType<typeof database>): Auth => {
},
updateUser: async (user: AuthUser) => {
user.updatedAt = new Date().toISOString();

await db.update($users).set(user).where(eq($users.id, user.id)).execute();
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();
const newUser: AuthUser = { ...user, sessionId };
await db.transaction(async (tx) => {
await tx.update($users).set(newUser).where(
eq($users.authId, user.authId),
);
await tx.delete($sessions).where(eq($sessions.id, user.sessionId));
await tx.delete($sessions).where(eq($sessions.$userId, user.id));
await tx.insert($sessions).values({
id: sessionId,
$userId: user.id,
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/auth/utils/adapters/datastore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const createDatastoreAuth = (kv = KV): Auth => {
if (!res.ok) throw new Error("Failed to create user");
},
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 7da191a

Please sign in to comment.