From 5b2a782257d8087479630649dc948cd028a47f1c Mon Sep 17 00:00:00 2001 From: Ted Wollman <25165500+TheTedder@users.noreply.github.com> Date: Thu, 13 Jun 2024 17:14:21 -0400 Subject: [PATCH 1/3] Use a cookie to store the current user. --- composables/useCurrentUser.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/composables/useCurrentUser.ts b/composables/useCurrentUser.ts index 8c9114ee..837591ec 100644 --- a/composables/useCurrentUser.ts +++ b/composables/useCurrentUser.ts @@ -1,10 +1,14 @@ +import { useCookie } from 'nuxt/app' import type { UserViewModel } from 'lib/api/data-contracts' +import type { CookieRef } from 'nuxt/app' -export function useCurrentUser() { - return useState('current_user', () => ({ - id: '', - username: '', - })) +export function useCurrentUser(): CookieRef { + return useCookie('current_user', { + default: () => ({ + id: '', + username: '', + }), + }) } export default useCurrentUser From dff31c77fefa8a8ee9d2ea62ede7ba63ac7c8eae Mon Sep 17 00:00:00 2001 From: Ted Wollman <25165500+TheTedder@users.noreply.github.com> Date: Thu, 13 Jun 2024 17:15:42 -0400 Subject: [PATCH 2/3] Use a cookie to store the session token. --- composables/useSessionToken.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/composables/useSessionToken.ts b/composables/useSessionToken.ts index daec49d1..cafbfb11 100644 --- a/composables/useSessionToken.ts +++ b/composables/useSessionToken.ts @@ -1,5 +1,8 @@ -export function useSessionToken() { - return useState('session_token') +import { useCookie } from 'nuxt/app' +import type { CookieRef } from 'nuxt/app' + +export function useSessionToken(): CookieRef { + return useCookie('session_token') } export default useSessionToken From daf83cbcccd791c8565d16a2cbad2c351f2f6652 Mon Sep 17 00:00:00 2001 From: Edward Runkel Date: Wed, 19 Jun 2024 08:05:06 -0400 Subject: [PATCH 3/3] Remove importing nuxt composable and mock it --- composables/useCurrentUser.ts | 1 - composables/useSessionToken.ts | 1 - vitest.setup.ts | 19 +++++++++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/composables/useCurrentUser.ts b/composables/useCurrentUser.ts index 837591ec..e901b39d 100644 --- a/composables/useCurrentUser.ts +++ b/composables/useCurrentUser.ts @@ -1,4 +1,3 @@ -import { useCookie } from 'nuxt/app' import type { UserViewModel } from 'lib/api/data-contracts' import type { CookieRef } from 'nuxt/app' diff --git a/composables/useSessionToken.ts b/composables/useSessionToken.ts index cafbfb11..34b8ac72 100644 --- a/composables/useSessionToken.ts +++ b/composables/useSessionToken.ts @@ -1,4 +1,3 @@ -import { useCookie } from 'nuxt/app' import type { CookieRef } from 'nuxt/app' export function useSessionToken(): CookieRef { diff --git a/vitest.setup.ts b/vitest.setup.ts index 26a822f8..8fd39016 100644 --- a/vitest.setup.ts +++ b/vitest.setup.ts @@ -11,6 +11,25 @@ const fetchMock = createFetchMock(vi) // sets globalThis.fetch and globalThis.fetchMock to our mocked version fetchMock.enableMocks() +const cookies = reactive<{ [key: string]: any }>({}) +interface CookieOptions { + default?: () => any +} + +export const useCookieMock = vi.fn((key: string, opts?: CookieOptions) => { + const cookie = toRef(cookies, key) + if (cookie.value === undefined && opts?.default) { + const initialValue = opts.default() + if (isRef(initialValue)) { + cookies[key] = initialValue + return initialValue as Ref + } + cookie.value = initialValue + } + return cookie +}) +vi.stubGlobal('useCookie', useCookieMock) + // Stolen from here: // https://zenn.dev/ninebolt6/articles/cadc924cb2416d const payload = reactive<{ state: Record }>({