Skip to content

Commit

Permalink
WIP: added a default_session_user
Browse files Browse the repository at this point in the history
  • Loading branch information
pstaabp committed Aug 31, 2022
1 parent 56a4c79 commit 534c39e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
7 changes: 7 additions & 0 deletions src/common/models/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SessionUser>;

/**
Expand Down
10 changes: 4 additions & 6 deletions src/stores/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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: '',
Expand All @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down
10 changes: 2 additions & 8 deletions tests/stores/session.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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,
Expand Down Expand Up @@ -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);

});
});
Expand Down

0 comments on commit 534c39e

Please sign in to comment.