-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consolidate the login code to the session store #120
Open
pstaabp
wants to merge
2
commits into
openwebwork:main
Choose a base branch
from
pstaabp:move-login-code
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,16 +2,16 @@ | |
|
||
import { defineStore } from 'pinia'; | ||
import { api } from 'boot/axios'; | ||
import { ParseableUser, User } from 'src/common/models/users'; | ||
import type { SessionInfo } from 'src/common/models/session'; | ||
import { SessionUser, 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'; | ||
import { ResponseError } from 'src/common/api-requests/errors'; | ||
|
||
import { useUserStore } from 'src/stores/users'; | ||
import { useSettingsStore } from 'src/stores/settings'; | ||
import { useProblemSetStore } from 'src/stores/problem_sets'; | ||
import { UserRole } from 'src/stores/permissions'; | ||
import { usePermissionStore, UserRole } from 'src/stores/permissions'; | ||
|
||
interface CourseInfo { | ||
course_name: string; | ||
|
@@ -25,18 +25,20 @@ interface CourseInfo { | |
export interface SessionState { | ||
logged_in: boolean; | ||
expiry: number; | ||
user: ParseableUser; | ||
user: SessionUser; | ||
course: CourseInfo; | ||
user_courses: ParseableUserCourse[]; | ||
} | ||
|
||
const logged_out_user = { username: 'logged_out', user_id: 0, is_admin: false }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. import default user from |
||
|
||
export const useSessionStore = defineStore('session', { | ||
// Stores this in localStorage. | ||
persist: true, | ||
state: (): SessionState => ({ | ||
logged_in: false, | ||
expiry: 0, | ||
user: { username: 'logged_out' }, | ||
user: logged_out_user, | ||
course: { | ||
course_id: 0, | ||
role: '', | ||
|
@@ -45,7 +47,7 @@ export const useSessionStore = defineStore('session', { | |
user_courses: [] | ||
}), | ||
getters: { | ||
full_name: (state): string => `${state.user?.first_name ?? ''} ${state.user?.last_name ?? ''}`, | ||
full_name: (state): string => `${state.user.first_name ?? ''} ${state.user.last_name ?? ''}`, | ||
getUser: (state): User => new User(state.user), | ||
}, | ||
actions: { | ||
|
@@ -63,7 +65,7 @@ export const useSessionStore = defineStore('session', { | |
if (this.logged_in) { | ||
this.user = session_info.user; | ||
} else { | ||
this.user = new User({ username: 'logged_out' }).toObject(); | ||
this.user = logged_out_user; | ||
} | ||
}, | ||
setCourse(course_id: number): void { | ||
|
@@ -102,9 +104,29 @@ export const useSessionStore = defineStore('session', { | |
throw response.data as ResponseError; | ||
} | ||
}, | ||
/** | ||
* Attempt to login to webwork3 with username/password. If successful, fetch | ||
* needed data (usercourses, roles, permissions). | ||
*/ | ||
async login(user_pass: UserPassword): Promise<boolean> { | ||
const response = await api.post('login', user_pass); | ||
const session_info = parseSessionInfo(response.data as ParseableSessionInfo); | ||
if (!session_info.logged_in || !session_info.user.user_id) { | ||
return false; | ||
} else { | ||
// success | ||
this.updateSessionInfo(session_info); | ||
const permission_store = usePermissionStore(); | ||
// permissions require access to user courses and respective roles | ||
await this.fetchUserCourses(); | ||
await permission_store.fetchRoles(); | ||
await permission_store.fetchRoutePermissions(); | ||
return true; | ||
} | ||
}, | ||
logout() { | ||
this.logged_in = false; | ||
this.user = new User({ username: 'logged_out' }).toObject(); | ||
this.user = logged_out_user; | ||
this.course = { course_id: 0, role: '', course_name: '' }; | ||
useProblemSetStore().clearAll(); | ||
useSettingsStore().clearAll(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,11 +15,10 @@ import { api } from 'boot/axios'; | |
|
||
import { getUser } from 'src/common/api-requests/user'; | ||
import { useSessionStore } from 'src/stores/session'; | ||
import { checkPassword } from 'src/common/api-requests/session'; | ||
|
||
import { Course, UserCourse } from 'src/common/models/courses'; | ||
import { SessionInfo } from 'src/common/models/session'; | ||
import { ParseableUser, User } from 'src/common/models/users'; | ||
import { SessionUser, User } from 'src/common/models/users'; | ||
|
||
import { cleanIDs, loadCSV } from '../utils'; | ||
|
||
|
@@ -30,7 +29,7 @@ describe('Session Store', () => { | |
let lisa: User; | ||
|
||
// session now just stores objects not models: | ||
const user: ParseableUser = { | ||
const user: SessionUser = { | ||
first_name: 'Homer', | ||
last_name: 'Simpson', | ||
user_id: 1234, | ||
|
@@ -39,14 +38,10 @@ describe('Session Store', () => { | |
is_admin: false, | ||
}; | ||
|
||
const logged_out: ParseableUser = { | ||
const logged_out: SessionUser = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. import from |
||
username: 'logged_out', | ||
email: '', | ||
last_name: '', | ||
first_name: '', | ||
user_id: 0, | ||
is_admin: false, | ||
student_id: '' | ||
}; | ||
|
||
const session_info: SessionInfo = { | ||
|
@@ -114,19 +109,12 @@ describe('Session Store', () => { | |
}); | ||
|
||
test('Login as a user', async () => { | ||
// test logging in as lisa gives the proper courses. | ||
const session_info = await checkPassword({ | ||
username: 'lisa', password: 'lisa' | ||
}); | ||
expect(session_info.logged_in).toBe(true); | ||
expect(session_info.user).toStrictEqual(lisa.toObject()); | ||
|
||
// test logging in as lisa gives the proper courses. | ||
const session = useSessionStore(); | ||
session.updateSessionInfo(session_info); | ||
|
||
const logged_in = await session.login({ username: 'lisa', password: 'lisa' }); | ||
expect(logged_in).toBe(true); | ||
expect(session.logged_in).toBe(true); | ||
expect(session.user).toStrictEqual(lisa.toObject()); | ||
|
||
}); | ||
|
||
// sort by course name and clean up the _id tags. | ||
|
@@ -135,9 +123,8 @@ describe('Session Store', () => { | |
a.course_name < b.course_name ? -1 : a.course_name > b.course_name ? 1 : 0)); | ||
}; | ||
|
||
test('check user courses', async () => { | ||
test('check user courses', () => { | ||
const session_store = useSessionStore(); | ||
await session_store.fetchUserCourses(); | ||
expect(sortAndClean(session_store.user_courses.map(c => new UserCourse(c)))) | ||
.toStrictEqual(sortAndClean(lisa_courses)); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,23 +96,23 @@ describe('Testing User and CourseUsers', () => { | |
describe('Testing for valid and invalid users.', () => { | ||
|
||
test('setting invalid email', () => { | ||
const user = new User({ username: 'test' }); | ||
const user = new User({ username: 'test', user_id: 10, is_admin: true }); | ||
expect(user.isValid()).toBe(true); | ||
|
||
user.email = 'bad@[email protected]'; | ||
expect(user.isValid()).toBe(false); | ||
}); | ||
|
||
test('setting invalid user_id', () => { | ||
const user = new User({ username: 'test' }); | ||
const user = new User({ username: 'test', user_id: 10, is_admin: true }); | ||
expect(user.isValid()).toBe(true); | ||
|
||
user.user_id = -15; | ||
expect(user.isValid()).toBe(false); | ||
}); | ||
|
||
test('setting invalid username', () => { | ||
const user = new User({ username: 'my username' }); | ||
const user = new User({ username: 'my username', user_id: 10, is_admin: true }); | ||
expect(user.isValid()).toBe(false); | ||
}); | ||
}); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
username:
logged_out
?Perhaps export this default as a constant for use in session.ts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good idea