From 4acd17f06bfc71cd96aecc66408cc335ebe52b46 Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Mon, 1 Jul 2024 10:15:08 +0530 Subject: [PATCH] Funnel point --- web/packages/next/local-user.ts | 2 ++ web/packages/shared/storage/localStorage/index.ts | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/web/packages/next/local-user.ts b/web/packages/next/local-user.ts index 8e9fd05f62..e7bf356c15 100644 --- a/web/packages/next/local-user.ts +++ b/web/packages/next/local-user.ts @@ -1,6 +1,8 @@ // TODO: This file belongs to the accounts package import { z } from "zod"; +// TODO: During login the only field present is email. Which makes this +// optionality indicated by these types incorrect. const LocalUser = z.object({ /** The user's ID. */ id: z.number(), diff --git a/web/packages/shared/storage/localStorage/index.ts b/web/packages/shared/storage/localStorage/index.ts index 5a22e9db21..ad3b580aec 100644 --- a/web/packages/shared/storage/localStorage/index.ts +++ b/web/packages/shared/storage/localStorage/index.ts @@ -1,4 +1,5 @@ import log from "@/next/log"; +import { removeKV, setKV } from "packages/next/kv"; export enum LS_KEYS { USER = "user", @@ -46,3 +47,16 @@ export const getData = (key: LS_KEYS) => { }; export const clearData = () => localStorage.clear(); + +// TODO: Migrate this to `local-user.ts`, with (a) more precise optionality +// indication of the constituent fields, (b) moving any fields that need to be +// accessed from web workers to KV DB. +// +// Creating a new function here to act as a funnel point. +export const setLSUser = (user: object) => { + const token = user["token"]; + token && typeof token == "string" + ? setKV("token", token) + : removeKV("token"); + setData(LS_KEYS.USER, user); +};