-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement support for jwt in API requests (#28)
* feat: implement basic login page with token fetching * feat: implement basic displaying of login error * feat: implement authentication hook with wrapper component * refactor: remove spurious log * feat: use form for login endpoint payload * feat: redirect to frontcover on successful login * chore: configure dummy secret key for dev * fix: fix navigation * feat: implement automatic redirection to original route after login * refactor: use assessmentID in MarkingPage and improve router
- Loading branch information
1 parent
38181d4
commit 0865c10
Showing
15 changed files
with
282 additions
and
71 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ services: | |
command: poetry run uvicorn main:app --reload --host 0.0.0.0 --port 5004 | ||
environment: | ||
- DB_URL=postgresql://user:[email protected]/answerbook | ||
- SECRET_KEY=dev_secret_key | ||
- MATHPIX_APP_ID | ||
- MATHPIX_APP_KEY | ||
volumes: | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { useParams } from 'react-router-dom' | ||
|
||
export const useAssessmentParams = () => { | ||
const { year, moduleCode, qualifier } = useParams() | ||
return { | ||
assessmentID: `y${year}_${moduleCode}_${qualifier}`, | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import axios from 'axios' | ||
import { jwtDecode } from 'jwt-decode' | ||
import { toPairs } from 'lodash' | ||
import { useState } from 'react' | ||
|
||
import { BASE_URL } from '../api/axiosInstance' | ||
import routes from '../api/routes' | ||
import { useAssessmentParams } from './assessmentParams' | ||
|
||
export interface Credentials { | ||
username: string | ||
password: string | ||
} | ||
|
||
const ACCESS_TOKEN_KEY = 'answerbook-access-token' | ||
|
||
export function getToken() { | ||
return localStorage.getItem(ACCESS_TOKEN_KEY) | ||
} | ||
|
||
export const useAuthentication = () => { | ||
const { assessmentID } = useAssessmentParams() | ||
const [authError, setAuthError] = useState() | ||
|
||
function hasValidToken(token: string | null) { | ||
if (!token) return false | ||
try { | ||
const decoded = jwtDecode(token) | ||
if (!decoded || !decoded.exp || !decoded.sub) return false | ||
const currentTime = Math.floor(Date.now() / 1000) | ||
return decoded.exp > currentTime && (decoded.sub as any).assessment_code === assessmentID | ||
} catch (error) { | ||
return false | ||
} | ||
} | ||
|
||
function saveToken(token: string) { | ||
localStorage.setItem(ACCESS_TOKEN_KEY, token) | ||
} | ||
|
||
function requestToken(credentials: Credentials) { | ||
const form = new FormData() | ||
toPairs(credentials).forEach(([k, v]) => form.set(k, v)) | ||
return axios | ||
.post(`${BASE_URL}${routes.login(assessmentID)}`, form) | ||
.then(({ data }) => { | ||
saveToken(data.access_token) | ||
}) | ||
.catch((error) => setAuthError(error?.response?.data?.detail ?? 'Authentication failed')) | ||
} | ||
|
||
return { authError, requestToken, hasValidToken } | ||
} |
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
Oops, something went wrong.