From 5ca038c73649c57ed0323a6a3423dedc1caefd04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sof=C3=ADa=20Aritz?= Date: Thu, 13 Jun 2024 23:48:12 +0200 Subject: [PATCH] checkpoint 3 --- identity-api/index.js | 21 ++++++++++++++ identity-web/src/lib/api.ts | 15 +++++++--- identity-web/src/lib/stores.ts | 22 +++++++++++++-- .../src/routes/dashboard/+page.svelte | 28 ++++++++++++++++++- 4 files changed, 78 insertions(+), 8 deletions(-) diff --git a/identity-api/index.js b/identity-api/index.js index 9270faf..d73e9da 100644 --- a/identity-api/index.js +++ b/identity-api/index.js @@ -24,12 +24,32 @@ fastify.get('/', async (request, reply) => { return IDENTITY_API_LANDING_MESSAGE; }) +fastify.get('/auth/account', { + async handler(request, reply) { + let jwt = request.headers['authorization'].replace('Bearer', '').trim() + let { payload } = await Jose.jwtVerify(jwt, JWT_SECRET) + + let user = users[payload.email] + user.password = undefined + return user + }, + schema: { + headers: { + type: 'object', + properties: { + authorization: { type: 'string' }, + }, + }, + }, +}) + fastify.post('/auth/login', { async handler(request, reply) { let user = users[request.body.email]; if (user != null && user.password == request.body.password) { let token = await new Jose.SignJWT({ uid: user.uid, + email: request.body.email, name: user.name, }) .setProtectedHeader({ alg: JWT_ALG }) @@ -70,6 +90,7 @@ fastify.post('/auth/register', { let user = users[request.body.email] let token = await new Jose.SignJWT({ uid: user.uid, + email: request.body.email, name: user.name, }) .setProtectedHeader({ alg: JWT_ALG }) diff --git a/identity-web/src/lib/api.ts b/identity-web/src/lib/api.ts index 99009e7..87cf58b 100644 --- a/identity-web/src/lib/api.ts +++ b/identity-web/src/lib/api.ts @@ -4,6 +4,11 @@ export type Credentials = { token: string, } +export type Account = { + uid: string, + name: string, +} + function sendRequest(path: string, request: RequestInit = {}, credentials?: Credentials) { if (typeof request !== "string" && credentials != null) { request.headers = { 'Authorization': `Bearer ${credentials.token}`, ...request.headers } @@ -21,11 +26,10 @@ async function asJson(request: Promise): Promise { return (await req.json() as R) } -export type LoginResponse = { token: string, } | { error: string, } export function login(credentials: { email: string, password: string, -}): Promise { +}): Promise<{ token: string, } | { error: string, }> { return asJson(sendRequest('/auth/login', { method: 'POST', headers: { @@ -35,12 +39,11 @@ export function login(credentials: { })) } -export type RegisterResponse = { token: string, } | { error: string, } export function register(credentials: { name: string, email: string, password: string, -}): Promise { +}): Promise<{ token: string, } | { error: string, }> { return asJson(sendRequest('/auth/register', { method: 'POST', headers: { @@ -48,4 +51,8 @@ export function register(credentials: { }, body: JSON.stringify(credentials), })) +} + +export function accountData(credentials: Credentials): Promise { + return asJson(sendRequest('/auth/account', undefined, credentials)) } \ No newline at end of file diff --git a/identity-web/src/lib/stores.ts b/identity-web/src/lib/stores.ts index 96fa351..9c90f53 100644 --- a/identity-web/src/lib/stores.ts +++ b/identity-web/src/lib/stores.ts @@ -1,5 +1,5 @@ import { writable } from "svelte/store"; -import type { Credentials } from "./api"; +import { accountData, type Account, type Credentials } from "./api"; const CREDENTIALS_KEY = 'v0:credentials' @@ -10,10 +10,26 @@ credentials.subscribe((value) => { } }) -export function initializeStores() { +export const account = writable() + +export async function initializeStores() { let rawCredentials = localStorage.getItem(CREDENTIALS_KEY) + let parsedCredentials if (rawCredentials != null && rawCredentials.length > 0) { - try { credentials.set(JSON.parse(rawCredentials)) } + try { + parsedCredentials = JSON.parse(rawCredentials) + credentials.set(parsedCredentials) + } catch (e) { localStorage.removeItem(CREDENTIALS_KEY) } } + + if (parsedCredentials != null) { + let data = await accountData(parsedCredentials) + if ('error' in data) { + credentials.set(null) + localStorage.removeItem(CREDENTIALS_KEY) + } else { + account.set(data) + } + } } \ No newline at end of file diff --git a/identity-web/src/routes/dashboard/+page.svelte b/identity-web/src/routes/dashboard/+page.svelte index 088dbda..0abdac2 100644 --- a/identity-web/src/routes/dashboard/+page.svelte +++ b/identity-web/src/routes/dashboard/+page.svelte @@ -1 +1,27 @@ -

Dashboard

\ No newline at end of file + + +
+
+

Welcome back, {$account?.name}.

+
+
+

Latest activity

+
+

New song: Takin' what's not yours

+

New song: Lovers Rock

+

New memory: § At the sunflower field with Ms. Violet

+
+
+
+

Memories from the past

+
+

§ 2024 Birthday

+

New song: KMAG YOYO

+

§ A new era

+
+
+
+
+
\ No newline at end of file