-
Notifications
You must be signed in to change notification settings - Fork 0
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
Ry 86 switch over auth #96
Changes from all commits
e9e3d83
8f7c2bc
7552713
07402b5
bff2fab
82721be
78adb5b
c5fc64d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,23 +3,41 @@ import apiClient from '@api/apiClient'; | |
import useLoginContext from './useLoginContext'; | ||
import { useNavigate } from 'react-router-dom'; | ||
import { Button, Stack } from '@mui/material'; | ||
import { CognitoJwtVerifier } from 'aws-jwt-verify'; | ||
|
||
const verifier = CognitoJwtVerifier.create({ | ||
userPoolId: import.meta.env.VITE_COGNITO_USER_POOL_ID as string, | ||
tokenUse: 'access', | ||
clientId: import.meta.env.VITE_COGNITO_CLIENT_ID as string, | ||
}); | ||
|
||
/** | ||
* Login Page component first checks if the user has been redirected from the | ||
* Cognito login page with an authorization code. If the code is present, it | ||
* fetches the user's access token and stores it in the context. | ||
*/ | ||
export default function LoginPage() { | ||
const { setToken } = useLoginContext(); | ||
const navigate = useNavigate(); | ||
|
||
useEffect(() => { | ||
const urlParams = new URLSearchParams(window.location.search); | ||
const authCode = urlParams.get('code'); | ||
|
||
async function getToken() { | ||
if (authCode) { | ||
const sessionToken = sessionStorage.getItem('token'); | ||
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. if the token expires, will this cause a 404 loop? since the token in sessionToken doesnt get reverified? 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. I'm not sure I'll do some research. How would I test this? 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. You can replace the token in local storage with something invalid and reload the page 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. When I mess with the token in storage it gives a 401 error. Should I be regenerating tokens? I was also wondering if you knew how token expiration was handled previously or if it mattered at all before. 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. you don't need to explicitly regenerate tokens because the component itself will just boot you to the login page, you just need to make sure you don't navigate to the '/' endpoint if the token is invalid. The way it was handled previously is if it failed it would display the login button where its hard coded to redirect to the google oauth page. Line 42 in this file. |
||
|
||
if (sessionToken) { | ||
try { | ||
const token = JSON.parse(sessionToken); | ||
await verifier.verify(token); | ||
setToken(token); | ||
navigate('/'); | ||
} catch (error) { | ||
console.log('Error verifying token:', error); | ||
sessionStorage.removeItem('token'); | ||
} | ||
} else if (authCode) { | ||
try { | ||
const token = await apiClient.getToken(authCode); | ||
console.log('Fetched Token:', token); | ||
|
||
sessionStorage.setItem('token', JSON.stringify(token)); | ||
setToken(token); | ||
navigate('/'); | ||
} catch (error) { | ||
|
@@ -29,6 +47,7 @@ export default function LoginPage() { | |
} | ||
getToken(); | ||
}, [navigate, setToken]); | ||
|
||
return ( | ||
<Stack | ||
width="100vw" | ||
|
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.
good change!