Skip to content

Commit

Permalink
Decouple session management of tokens from user data
Browse files Browse the repository at this point in the history
  • Loading branch information
oudeismetis committed Sep 13, 2024
1 parent 8ac3f59 commit f3a542f
Showing 1 changed file with 9 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
// Pinia Store
import { defineStore } from 'pinia'
import { z } from 'zod'
import { UserShape } from '@/services/users'

const STORAGE_HASH = '{{ random_ascii_string(10) }}'
export const STORAGE_KEY = `{{ cookiecutter.project_slug }}-${STORAGE_HASH}`

interface State {
user: UserShape | null
token: z.string() | null
}

export const useUserStore = defineStore('user', {
state: (): State => ({
user: null,
token: null,
}),
persist: {
key: STORAGE_KEY,
Expand All @@ -21,12 +24,16 @@ export const useUserStore = defineStore('user', {
return !!state.user
},
token: (state) => {
return state.user ? state.user.token : null
return state.token
},
},
actions: {
updateUser(payload: UserShape) {
this.user = payload
const { token, ...userData } = payload
this.user = userData
if (token) {
this.token = token
}
},
clearUser() {
this.$reset()
Expand Down

0 comments on commit f3a542f

Please sign in to comment.