feat: auth store type improvements
This commit is contained in:
parent
7b53e684aa
commit
176ad565cc
6 changed files with 53 additions and 56 deletions
|
@ -1,11 +1,19 @@
|
|||
import type {IAbstract} from './IAbstract'
|
||||
import type {IUserSettings} from './IUserSettings'
|
||||
|
||||
export const AUTH_TYPES = {
|
||||
'UNKNOWN': 0,
|
||||
'USER': 1,
|
||||
'LINK_SHARE': 2,
|
||||
} as const
|
||||
|
||||
export interface IUser extends IAbstract {
|
||||
id: number
|
||||
email: string
|
||||
username: string
|
||||
name: string
|
||||
exp: number
|
||||
type: typeof AUTH_TYPES[keyof typeof AUTH_TYPES],
|
||||
|
||||
created: Date
|
||||
updated: Date
|
||||
|
|
|
@ -11,4 +11,5 @@ export interface IUserSettings extends IAbstract {
|
|||
defaultListId: undefined | IList['id']
|
||||
weekStart: 0 | 1 | 2 | 3 | 4 | 5 | 6
|
||||
timezone: string
|
||||
language: string
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
import AbstractModel from './abstractModel'
|
||||
import UserSettingsModel from '@/models/userSettings'
|
||||
|
||||
import type { IUser } from '@/modelTypes/IUser'
|
||||
import { AUTH_TYPES, type IUser } from '@/modelTypes/IUser'
|
||||
import type { IUserSettings } from '@/modelTypes/IUserSettings'
|
||||
|
||||
export default class UserModel extends AbstractModel<IUser> implements IUser {
|
||||
|
@ -9,10 +9,12 @@ export default class UserModel extends AbstractModel<IUser> implements IUser {
|
|||
email = ''
|
||||
username = ''
|
||||
name = ''
|
||||
exp = 0
|
||||
type = AUTH_TYPES.UNKNOWN
|
||||
|
||||
created: Date = null
|
||||
updated: Date = null
|
||||
settings: IUserSettings = null
|
||||
created: Date
|
||||
updated: Date
|
||||
settings: IUserSettings
|
||||
|
||||
constructor(data: Partial<IUser> = {}) {
|
||||
super()
|
||||
|
@ -21,9 +23,7 @@ export default class UserModel extends AbstractModel<IUser> implements IUser {
|
|||
this.created = new Date(this.created)
|
||||
this.updated = new Date(this.updated)
|
||||
|
||||
if (this.settings !== null) {
|
||||
this.settings = new UserSettingsModel(this.settings)
|
||||
}
|
||||
this.settings = new UserSettingsModel(this.settings || {})
|
||||
}
|
||||
|
||||
getAvatarUrl(size = 50) {
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
|
||||
import AbstractModel from './abstractModel'
|
||||
|
||||
import type {IUserSettings} from '@/modelTypes/IUserSettings'
|
||||
import type {IList} from '@/modelTypes/IList'
|
||||
import {getCurrentLanguage} from '@/i18n'
|
||||
|
||||
export default class UserSettingsModel extends AbstractModel<IUserSettings> implements IUserSettings {
|
||||
name = ''
|
||||
|
@ -10,11 +9,12 @@ export default class UserSettingsModel extends AbstractModel<IUserSettings> impl
|
|||
discoverableByName = false
|
||||
discoverableByEmail = false
|
||||
overdueTasksRemindersEnabled = true
|
||||
defaultListId: undefined | IList['id'] = undefined
|
||||
weekStart: IUserSettings['weekStart'] = 0
|
||||
defaultListId = undefined
|
||||
weekStart = 0 as IUserSettings['weekStart']
|
||||
timezone = ''
|
||||
language = getCurrentLanguage()
|
||||
|
||||
constructor(data: Partial<IUserSettings>) {
|
||||
constructor(data: Partial<IUserSettings> = {}) {
|
||||
super()
|
||||
this.assignData(data)
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import type { IList } from '@/modelTypes/IList'
|
|||
import type { IAttachment } from '@/modelTypes/IAttachment'
|
||||
import type { ILabel } from '@/modelTypes/ILabel'
|
||||
import type { INamespace } from '@/modelTypes/INamespace'
|
||||
import type { IUser } from '@/modelTypes/IUser'
|
||||
|
||||
export interface RootStoreState {
|
||||
loading: boolean,
|
||||
|
@ -22,25 +23,10 @@ export interface AttachmentState {
|
|||
attachments: IAttachment[],
|
||||
}
|
||||
|
||||
export const AUTH_TYPES = {
|
||||
'UNKNOWN': 0,
|
||||
'USER': 1,
|
||||
'LINK_SHARE': 2,
|
||||
} as const
|
||||
|
||||
export interface Info {
|
||||
id: number // what kind of id is this?
|
||||
type: typeof AUTH_TYPES[keyof typeof AUTH_TYPES],
|
||||
getAvatarUrl: () => string
|
||||
settings: IUserSettings
|
||||
name: string
|
||||
email: string
|
||||
exp: any
|
||||
}
|
||||
export interface AuthState {
|
||||
authenticated: boolean,
|
||||
isLinkShareAuth: boolean,
|
||||
info: Info | null,
|
||||
info: IUser | null,
|
||||
needsTotpPasscode: boolean,
|
||||
avatarUrl: string,
|
||||
lastUserInfoRefresh: Date | null,
|
||||
|
|
|
@ -9,28 +9,24 @@ import {getToken, refreshToken, removeToken, saveToken} from '@/helpers/auth'
|
|||
import {setLoadingPinia} from '@/store/helper'
|
||||
import {success} from '@/message'
|
||||
import {redirectToProvider} from '@/helpers/redirectToProvider'
|
||||
import type {AuthState, Info} from '@/store/types'
|
||||
import {AUTH_TYPES} from '@/store/types'
|
||||
import {AUTH_TYPES, type IUser} from '@/modelTypes/IUser'
|
||||
import type {AuthState} from '@/store/types'
|
||||
import type {IUserSettings} from '@/modelTypes/IUserSettings'
|
||||
import router from '@/router'
|
||||
import {useConfigStore} from '@/stores/config'
|
||||
|
||||
function defaultSettings(settings: Partial<IUserSettings>) {
|
||||
if (typeof settings.weekStart === 'undefined' || settings.weekStart === '') {
|
||||
settings.weekStart = 0
|
||||
}
|
||||
return settings
|
||||
}
|
||||
import UserSettingsModel from '@/models/userSettings'
|
||||
|
||||
export const useAuthStore = defineStore('auth', {
|
||||
state: () : AuthState => ({
|
||||
authenticated: false,
|
||||
isLinkShareAuth: false,
|
||||
info: null,
|
||||
needsTotpPasscode: false,
|
||||
|
||||
info: null,
|
||||
avatarUrl: '',
|
||||
settings: new UserSettingsModel(),
|
||||
|
||||
lastUserInfoRefresh: null,
|
||||
settings: {}, // should be IUserSettings
|
||||
isLoading: false,
|
||||
}),
|
||||
getters: {
|
||||
|
@ -52,23 +48,24 @@ export const useAuthStore = defineStore('auth', {
|
|||
this.isLoading = isLoading
|
||||
},
|
||||
|
||||
setInfo(info: Info) {
|
||||
setUser(info: IUser | null) {
|
||||
this.info = info
|
||||
if (info !== null) {
|
||||
this.avatarUrl = info.getAvatarUrl()
|
||||
this.reloadAvatar()
|
||||
|
||||
if (info.settings) {
|
||||
this.settings = defaultSettings(info.settings)
|
||||
this.settings = new UserSettingsModel(info.settings)
|
||||
}
|
||||
|
||||
this.isLinkShareAuth = info.id < 0
|
||||
}
|
||||
},
|
||||
setUserSettings(settings: IUserSettings) {
|
||||
this.settings = defaultSettings(settings)
|
||||
const info = this.info !== null ? this.info : {} as Info
|
||||
info.name = settings.name
|
||||
this.info = info
|
||||
this.settings = new UserSettingsModel(settings)
|
||||
this.info = new UserModel({
|
||||
...this.info !== null ? this.info : {},
|
||||
name: settings.name,
|
||||
})
|
||||
},
|
||||
setAuthenticated(authenticated: boolean) {
|
||||
this.authenticated = authenticated
|
||||
|
@ -190,7 +187,7 @@ export const useAuthStore = defineStore('auth', {
|
|||
const info = new UserModel(JSON.parse(atob(base64)))
|
||||
const ts = Math.round((new Date()).getTime() / 1000)
|
||||
authenticated = info.exp >= ts
|
||||
this.setInfo(info)
|
||||
this.setUser(info)
|
||||
|
||||
if (authenticated) {
|
||||
this.refreshUserInfo()
|
||||
|
@ -199,7 +196,7 @@ export const useAuthStore = defineStore('auth', {
|
|||
|
||||
this.setAuthenticated(authenticated)
|
||||
if (!authenticated) {
|
||||
this.setInfo(null)
|
||||
this.setUser(null)
|
||||
this.redirectToProviderIfNothingElseIsEnabled()
|
||||
}
|
||||
},
|
||||
|
@ -227,12 +224,12 @@ export const useAuthStore = defineStore('auth', {
|
|||
const response = await HTTP.get('user')
|
||||
const info = new UserModel({
|
||||
...response.data,
|
||||
type: this.info.type,
|
||||
email: this.info.email,
|
||||
exp: this.info.exp,
|
||||
...(this.info?.type && {type: this.info?.type}),
|
||||
...(this.info?.email && {email: this.info?.email}),
|
||||
...(this.info?.exp && {exp: this.info?.exp}),
|
||||
})
|
||||
|
||||
this.setInfo(info)
|
||||
this.setUser(info)
|
||||
this.updateLastUserRefresh()
|
||||
|
||||
if (
|
||||
|
@ -262,9 +259,14 @@ export const useAuthStore = defineStore('auth', {
|
|||
}
|
||||
},
|
||||
|
||||
async saveUserSettings(payload) {
|
||||
const {settings} = payload
|
||||
const showMessage = payload.showMessage ?? true
|
||||
async saveUserSettings({
|
||||
settings,
|
||||
showMessage = true,
|
||||
}: {
|
||||
settings: IUserSettings
|
||||
showMessage : boolean
|
||||
}) {
|
||||
// const showMessage = payload.showMessage ?? true
|
||||
const userSettingsService = new UserSettingsService()
|
||||
|
||||
// FIXME
|
||||
|
|
Loading…
Reference in a new issue