Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug/LBGG-590: Persistent login #616

Merged
merged 3 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions composables/useCurrentUser.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import type { UserViewModel } from 'lib/api/data-contracts'
import type { CookieRef } from 'nuxt/app'

export function useCurrentUser() {
return useState<UserViewModel>('current_user', () => ({
id: '',
username: '',
}))
export function useCurrentUser(): CookieRef<UserViewModel> {
return useCookie('current_user', {
default: () => ({
id: '',
username: '',
}),
})
}

export default useCurrentUser
6 changes: 4 additions & 2 deletions composables/useSessionToken.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export function useSessionToken() {
return useState<string>('session_token')
import type { CookieRef } from 'nuxt/app'

export function useSessionToken(): CookieRef<string> {
return useCookie<string>('session_token')
}

export default useSessionToken
19 changes: 19 additions & 0 deletions vitest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if both cookie and opts.default don't have values? Won't we be returning an undefined ref?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's actually just how regular cookies work anyway.

const initialValue = opts.default()
if (isRef(initialValue)) {
cookies[key] = initialValue
return initialValue as Ref<any>
}
cookie.value = initialValue
}
return cookie
})
vi.stubGlobal('useCookie', useCookieMock)

// Stolen from here:
// https://zenn.dev/ninebolt6/articles/cadc924cb2416d
const payload = reactive<{ state: Record<string, any> }>({
Expand Down
Loading