feat: auth store type improvements

This commit is contained in:
Dominik Pschenitschni 2022-09-23 16:21:05 +02:00
parent 7b53e684aa
commit 176ad565cc
No known key found for this signature in database
GPG key ID: B257AC0149F43A77
6 changed files with 53 additions and 56 deletions

View file

@ -1,11 +1,19 @@
import type {IAbstract} from './IAbstract' import type {IAbstract} from './IAbstract'
import type {IUserSettings} from './IUserSettings' import type {IUserSettings} from './IUserSettings'
export const AUTH_TYPES = {
'UNKNOWN': 0,
'USER': 1,
'LINK_SHARE': 2,
} as const
export interface IUser extends IAbstract { export interface IUser extends IAbstract {
id: number id: number
email: string email: string
username: string username: string
name: string name: string
exp: number
type: typeof AUTH_TYPES[keyof typeof AUTH_TYPES],
created: Date created: Date
updated: Date updated: Date

View file

@ -11,4 +11,5 @@ export interface IUserSettings extends IAbstract {
defaultListId: undefined | IList['id'] defaultListId: undefined | IList['id']
weekStart: 0 | 1 | 2 | 3 | 4 | 5 | 6 weekStart: 0 | 1 | 2 | 3 | 4 | 5 | 6
timezone: string timezone: string
language: string
} }

View file

@ -1,7 +1,7 @@
import AbstractModel from './abstractModel' import AbstractModel from './abstractModel'
import UserSettingsModel from '@/models/userSettings' 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' import type { IUserSettings } from '@/modelTypes/IUserSettings'
export default class UserModel extends AbstractModel<IUser> implements IUser { export default class UserModel extends AbstractModel<IUser> implements IUser {
@ -9,10 +9,12 @@ export default class UserModel extends AbstractModel<IUser> implements IUser {
email = '' email = ''
username = '' username = ''
name = '' name = ''
exp = 0
type = AUTH_TYPES.UNKNOWN
created: Date = null created: Date
updated: Date = null updated: Date
settings: IUserSettings = null settings: IUserSettings
constructor(data: Partial<IUser> = {}) { constructor(data: Partial<IUser> = {}) {
super() super()
@ -21,9 +23,7 @@ export default class UserModel extends AbstractModel<IUser> implements IUser {
this.created = new Date(this.created) this.created = new Date(this.created)
this.updated = new Date(this.updated) 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) { getAvatarUrl(size = 50) {

View file

@ -1,8 +1,7 @@
import AbstractModel from './abstractModel' import AbstractModel from './abstractModel'
import type {IUserSettings} from '@/modelTypes/IUserSettings' 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 { export default class UserSettingsModel extends AbstractModel<IUserSettings> implements IUserSettings {
name = '' name = ''
@ -10,11 +9,12 @@ export default class UserSettingsModel extends AbstractModel<IUserSettings> impl
discoverableByName = false discoverableByName = false
discoverableByEmail = false discoverableByEmail = false
overdueTasksRemindersEnabled = true overdueTasksRemindersEnabled = true
defaultListId: undefined | IList['id'] = undefined defaultListId = undefined
weekStart: IUserSettings['weekStart'] = 0 weekStart = 0 as IUserSettings['weekStart']
timezone = '' timezone = ''
language = getCurrentLanguage()
constructor(data: Partial<IUserSettings>) { constructor(data: Partial<IUserSettings> = {}) {
super() super()
this.assignData(data) this.assignData(data)
} }

View file

@ -4,6 +4,7 @@ import type { IList } from '@/modelTypes/IList'
import type { IAttachment } from '@/modelTypes/IAttachment' import type { IAttachment } from '@/modelTypes/IAttachment'
import type { ILabel } from '@/modelTypes/ILabel' import type { ILabel } from '@/modelTypes/ILabel'
import type { INamespace } from '@/modelTypes/INamespace' import type { INamespace } from '@/modelTypes/INamespace'
import type { IUser } from '@/modelTypes/IUser'
export interface RootStoreState { export interface RootStoreState {
loading: boolean, loading: boolean,
@ -22,25 +23,10 @@ export interface AttachmentState {
attachments: IAttachment[], 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 { export interface AuthState {
authenticated: boolean, authenticated: boolean,
isLinkShareAuth: boolean, isLinkShareAuth: boolean,
info: Info | null, info: IUser | null,
needsTotpPasscode: boolean, needsTotpPasscode: boolean,
avatarUrl: string, avatarUrl: string,
lastUserInfoRefresh: Date | null, lastUserInfoRefresh: Date | null,

View file

@ -9,28 +9,24 @@ import {getToken, refreshToken, removeToken, saveToken} from '@/helpers/auth'
import {setLoadingPinia} from '@/store/helper' import {setLoadingPinia} from '@/store/helper'
import {success} from '@/message' import {success} from '@/message'
import {redirectToProvider} from '@/helpers/redirectToProvider' import {redirectToProvider} from '@/helpers/redirectToProvider'
import type {AuthState, Info} from '@/store/types' import {AUTH_TYPES, type IUser} from '@/modelTypes/IUser'
import {AUTH_TYPES} from '@/store/types' import type {AuthState} from '@/store/types'
import type { IUserSettings } from '@/modelTypes/IUserSettings' import type {IUserSettings} from '@/modelTypes/IUserSettings'
import router from '@/router' import router from '@/router'
import {useConfigStore} from '@/stores/config' import {useConfigStore} from '@/stores/config'
import UserSettingsModel from '@/models/userSettings'
function defaultSettings(settings: Partial<IUserSettings>) {
if (typeof settings.weekStart === 'undefined' || settings.weekStart === '') {
settings.weekStart = 0
}
return settings
}
export const useAuthStore = defineStore('auth', { export const useAuthStore = defineStore('auth', {
state: () : AuthState => ({ state: () : AuthState => ({
authenticated: false, authenticated: false,
isLinkShareAuth: false, isLinkShareAuth: false,
info: null,
needsTotpPasscode: false, needsTotpPasscode: false,
info: null,
avatarUrl: '', avatarUrl: '',
settings: new UserSettingsModel(),
lastUserInfoRefresh: null, lastUserInfoRefresh: null,
settings: {}, // should be IUserSettings
isLoading: false, isLoading: false,
}), }),
getters: { getters: {
@ -52,23 +48,24 @@ export const useAuthStore = defineStore('auth', {
this.isLoading = isLoading this.isLoading = isLoading
}, },
setInfo(info: Info) { setUser(info: IUser | null) {
this.info = info this.info = info
if (info !== null) { if (info !== null) {
this.avatarUrl = info.getAvatarUrl() this.reloadAvatar()
if (info.settings) { if (info.settings) {
this.settings = defaultSettings(info.settings) this.settings = new UserSettingsModel(info.settings)
} }
this.isLinkShareAuth = info.id < 0 this.isLinkShareAuth = info.id < 0
} }
}, },
setUserSettings(settings: IUserSettings) { setUserSettings(settings: IUserSettings) {
this.settings = defaultSettings(settings) this.settings = new UserSettingsModel(settings)
const info = this.info !== null ? this.info : {} as Info this.info = new UserModel({
info.name = settings.name ...this.info !== null ? this.info : {},
this.info = info name: settings.name,
})
}, },
setAuthenticated(authenticated: boolean) { setAuthenticated(authenticated: boolean) {
this.authenticated = authenticated this.authenticated = authenticated
@ -190,7 +187,7 @@ export const useAuthStore = defineStore('auth', {
const info = new UserModel(JSON.parse(atob(base64))) const info = new UserModel(JSON.parse(atob(base64)))
const ts = Math.round((new Date()).getTime() / 1000) const ts = Math.round((new Date()).getTime() / 1000)
authenticated = info.exp >= ts authenticated = info.exp >= ts
this.setInfo(info) this.setUser(info)
if (authenticated) { if (authenticated) {
this.refreshUserInfo() this.refreshUserInfo()
@ -199,7 +196,7 @@ export const useAuthStore = defineStore('auth', {
this.setAuthenticated(authenticated) this.setAuthenticated(authenticated)
if (!authenticated) { if (!authenticated) {
this.setInfo(null) this.setUser(null)
this.redirectToProviderIfNothingElseIsEnabled() this.redirectToProviderIfNothingElseIsEnabled()
} }
}, },
@ -227,12 +224,12 @@ export const useAuthStore = defineStore('auth', {
const response = await HTTP.get('user') const response = await HTTP.get('user')
const info = new UserModel({ const info = new UserModel({
...response.data, ...response.data,
type: this.info.type, ...(this.info?.type && {type: this.info?.type}),
email: this.info.email, ...(this.info?.email && {email: this.info?.email}),
exp: this.info.exp, ...(this.info?.exp && {exp: this.info?.exp}),
}) })
this.setInfo(info) this.setUser(info)
this.updateLastUserRefresh() this.updateLastUserRefresh()
if ( if (
@ -262,9 +259,14 @@ export const useAuthStore = defineStore('auth', {
} }
}, },
async saveUserSettings(payload) { async saveUserSettings({
const {settings} = payload settings,
const showMessage = payload.showMessage ?? true showMessage = true,
}: {
settings: IUserSettings
showMessage : boolean
}) {
// const showMessage = payload.showMessage ?? true
const userSettingsService = new UserSettingsService() const userSettingsService = new UserSettingsService()
// FIXME // FIXME