From 31bab2ea00edf292a4a35eabd861f7a8576a8df5 Mon Sep 17 00:00:00 2001 From: majortom327 Date: Tue, 30 Jan 2024 19:25:53 -0800 Subject: [PATCH] Refactor note server and add user properties to auth server --- app/models/_note.server.old.ts | 81 ------------------- app/services/auth.server.ts | 6 +- .../auth_strategies/auth0.strategy.ts | 13 ++- 3 files changed, 16 insertions(+), 84 deletions(-) delete mode 100644 app/models/_note.server.old.ts diff --git a/app/models/_note.server.old.ts b/app/models/_note.server.old.ts deleted file mode 100644 index 8babd95..0000000 --- a/app/models/_note.server.old.ts +++ /dev/null @@ -1,81 +0,0 @@ -import arc from "@architect/functions"; -import { createId } from "@paralleldrive/cuid2"; - -import type { User } from "./user.server"; - -export interface Note { - id: ReturnType; - userId: User["id"]; - title: string; - body: string; -} - -interface NoteItem { - pk: User["id"]; - sk: `note#${Note["id"]}`; -} - -const skToId = (sk: NoteItem["sk"]): Note["id"] => sk.replace(/^note#/, ""); -const idToSk = (id: Note["id"]): NoteItem["sk"] => `note#${id}`; - -export async function getNote({ - id, - userId, -}: Pick): Promise { - const db = await arc.tables(); - - const result = await db.note.get({ pk: userId, sk: idToSk(id) }); - - if (result) { - return { - userId: result.pk, - id: result.sk, - title: result.title, - body: result.body, - }; - } - return null; -} - -export async function getNoteListItems({ - userId, -}: Pick): Promise[]> { - const db = await arc.tables(); - - const result = await db.note.query({ - KeyConditionExpression: "pk = :pk", - ExpressionAttributeValues: { ":pk": userId }, - }); - - // eslint-disable-next-line @typescript-eslint/no-explicit-any - return result.Items.map((n: any) => ({ - title: n.title, - id: skToId(n.sk), - })); -} - -export async function createNote({ - body, - title, - userId, -}: Pick): Promise { - const db = await arc.tables(); - - const result = await db.note.put({ - pk: userId, - sk: idToSk(createId()), - title: title, - body: body, - }); - return { - id: skToId(result.sk), - userId: result.pk, - title: result.title, - body: result.body, - }; -} - -export async function deleteNote({ id, userId }: Pick) { - const db = await arc.tables(); - return db.note.delete({ pk: userId, sk: idToSk(id) }); -} diff --git a/app/services/auth.server.ts b/app/services/auth.server.ts index 7c7f6e7..701785b 100644 --- a/app/services/auth.server.ts +++ b/app/services/auth.server.ts @@ -7,7 +7,11 @@ import { auth0Strategy } from "./auth_strategies/auth0.strategy"; export interface User { id: string; - // Add your own user properties here or extend with a type from your database + email: string; + name: string; + avatar: string; + accessToken: string; + refreshToken?: string; } export type AuthStrategy = (typeof AuthStrategies)[keyof typeof AuthStrategies]; diff --git a/app/services/auth_strategies/auth0.strategy.ts b/app/services/auth_strategies/auth0.strategy.ts index f1ace26..e889cbb 100644 --- a/app/services/auth_strategies/auth0.strategy.ts +++ b/app/services/auth_strategies/auth0.strategy.ts @@ -4,6 +4,7 @@ import { Auth0Strategy } from "remix-auth-auth0"; import type { User } from "~/services/auth.server"; import { AuthStrategies } from "~/services/auth_strategies"; import Env from "../env.server"; +import { prop, head } from "rambda"; const clientID = Env.get("AUTH0_CLIENT_ID"); const clientSecret = Env.get("AUTH0_CLIENT_SECRET"); @@ -22,8 +23,16 @@ export const auth0Strategy = new Auth0Strategy( domain, callbackURL: `${Env.get("APP_URL")}/auth/${AuthStrategies.AUTH0}/callback`, }, - async ({ accessToken, refreshToken, profile, extraParams }) => { + async ({ accessToken, refreshToken, profile }) => { // Do something with the tokens and profile - return {}; + + return { + id: profile.id!, + email: prop("value", head(profile?.emails || [])), + name: profile.displayName ?? profile.name?.givenName ?? "", + avatar: prop("value", head(profile?.photos || [])), + accessToken, + refreshToken, + }; }, );