diff --git a/src/modelTypes/IUser.ts b/src/modelTypes/IUser.ts index d2ee19ea..2465a37d 100644 --- a/src/modelTypes/IUser.ts +++ b/src/modelTypes/IUser.ts @@ -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 diff --git a/src/modelTypes/IUserSettings.ts b/src/modelTypes/IUserSettings.ts index 9550e1e6..31e921e0 100644 --- a/src/modelTypes/IUserSettings.ts +++ b/src/modelTypes/IUserSettings.ts @@ -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 } \ No newline at end of file diff --git a/src/models/user.ts b/src/models/user.ts index a0148445..d7071c24 100644 --- a/src/models/user.ts +++ b/src/models/user.ts @@ -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 implements IUser { @@ -9,10 +9,12 @@ export default class UserModel extends AbstractModel 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 = {}) { super() @@ -21,9 +23,7 @@ export default class UserModel extends AbstractModel 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) { diff --git a/src/models/userSettings.ts b/src/models/userSettings.ts index 308c72d8..ddb6f029 100644 --- a/src/models/userSettings.ts +++ b/src/models/userSettings.ts @@ -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 implements IUserSettings { name = '' @@ -10,11 +9,12 @@ export default class UserSettingsModel extends AbstractModel 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) { + constructor(data: Partial = {}) { super() this.assignData(data) } diff --git a/src/store/types.ts b/src/store/types.ts index 8216082f..3bda4dfb 100644 --- a/src/store/types.ts +++ b/src/store/types.ts @@ -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, diff --git a/src/stores/auth.ts b/src/stores/auth.ts index 5d528d39..1f949e7f 100644 --- a/src/stores/auth.ts +++ b/src/stores/auth.ts @@ -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 type { IUserSettings } from '@/modelTypes/IUserSettings' +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) { - 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