diff --git a/src/common/models/users.ts b/src/common/models/users.ts index 741766c3..0257452c 100644 --- a/src/common/models/users.ts +++ b/src/common/models/users.ts @@ -18,6 +18,13 @@ export interface SessionUser { student_id?: string; } +export const default_session_user: SessionUser = { + user_id: 0, + username: 'logged_out', + is_admin: false +}; + + export type ParseableUser = Partial; /** diff --git a/src/stores/session.ts b/src/stores/session.ts index a977da8f..0ad1b744 100644 --- a/src/stores/session.ts +++ b/src/stores/session.ts @@ -2,7 +2,7 @@ import { defineStore } from 'pinia'; import { api } from 'boot/axios'; -import { SessionUser, User } from 'src/common/models/users'; +import { SessionUser, User, default_session_user } from 'src/common/models/users'; import { ParseableSessionInfo, parseSessionInfo, SessionInfo, UserPassword } from 'src/common/models/session'; import { ParseableUserCourse } from 'src/common/models/courses'; import { logger } from 'boot/logger'; @@ -30,15 +30,13 @@ export interface SessionState { user_courses: ParseableUserCourse[]; } -const logged_out_user = { username: 'logged_out', user_id: 0, is_admin: false }; - export const useSessionStore = defineStore('session', { // Stores this in localStorage. persist: true, state: (): SessionState => ({ logged_in: false, expiry: 0, - user: logged_out_user, + user: default_session_user, course: { course_id: 0, role: '', @@ -65,7 +63,7 @@ export const useSessionStore = defineStore('session', { if (this.logged_in) { this.user = session_info.user; } else { - this.user = logged_out_user; + this.user = default_session_user; } }, setCourse(course_id: number): void { @@ -126,7 +124,7 @@ export const useSessionStore = defineStore('session', { }, logout() { this.logged_in = false; - this.user = logged_out_user; + this.user = default_session_user; this.course = { course_id: 0, role: '', course_name: '' }; useProblemSetStore().clearAll(); useSettingsStore().clearAll(); diff --git a/tests/stores/session.spec.ts b/tests/stores/session.spec.ts index cbf70c57..32aeca4d 100644 --- a/tests/stores/session.spec.ts +++ b/tests/stores/session.spec.ts @@ -18,7 +18,7 @@ import { useSessionStore } from 'src/stores/session'; import { Course, UserCourse } from 'src/common/models/courses'; import { SessionInfo } from 'src/common/models/session'; -import { SessionUser, User } from 'src/common/models/users'; +import { SessionUser, User, default_session_user } from 'src/common/models/users'; import { cleanIDs, loadCSV } from '../utils'; @@ -38,12 +38,6 @@ describe('Session Store', () => { is_admin: false, }; - const logged_out: SessionUser = { - username: 'logged_out', - user_id: 0, - is_admin: false, - }; - const session_info: SessionInfo = { logged_in: true, user, @@ -151,7 +145,7 @@ describe('Session Store', () => { session.logout(); expect(session.logged_in).toBe(false); - expect(session.user).toStrictEqual(logged_out); + expect(session.user).toStrictEqual(default_session_user); }); });