-
Notifications
You must be signed in to change notification settings - Fork 26
migrate from local storage to cookies for jwt token #1875
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
base: dev
Are you sure you want to change the base?
Changes from 1 commit
7096a59
6f4cc84
f4cc26f
e50725f
d8835d3
7655794
2a20627
e94bc7e
92e0f3e
eb88b44
8bb5da1
5fb27b4
7357224
01fbea9
8e6e19d
5ae5cfa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,17 +2,20 @@ import React from 'react'; | |
| import UserNavbar from './UserNavbar'; | ||
| import AdminNavbar from './AdminNavbar'; | ||
| import { useSCE } from '../context/SceContext'; | ||
| import Cookies from 'universal-cookie'; | ||
|
|
||
| function NavBarWrapper({ | ||
| enableAdminNavbar = false, | ||
| component: Component, | ||
| ...appProps | ||
| }) { | ||
| const { user, setUser, setAuthenticated } = useSCE(); | ||
| const cookies = new Cookies(); | ||
|
|
||
| function handleLogout() { | ||
| setAuthenticated(false); | ||
| setUser({}); | ||
| cookies.remove('jwtToken'); | ||
| window.localStorage.removeItem('jwtToken'); | ||
|
||
| window.location.reload(); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,24 @@ | ||
| import React, { useState } from 'react'; | ||
| import { loginUser } from '../../APIFunctions/Auth'; | ||
| import { useSCE } from '../../Components/context/SceContext'; | ||
| import Cookies from 'universal-cookie'; | ||
|
|
||
| export default function Login() { | ||
| const { setAuthenticated } = useSCE(); | ||
| const queryParams = new URLSearchParams(window.location.search); | ||
| const [errorMsg, setErrorMsg] = useState(''); | ||
| const cookies = new Cookies(); | ||
|
|
||
| async function handleSubmit(e) { | ||
| e.preventDefault(); | ||
| window.localStorage.removeItem('jwtToken'); // clean up local storage | ||
| const email = e.target.email.value; | ||
| const password = e.target.password.value; | ||
| setErrorMsg(''); | ||
| const loginStatus = await loginUser(email, password); | ||
| if (!loginStatus.error) { | ||
| setAuthenticated(true); | ||
| window.localStorage.setItem('jwtToken', loginStatus.token); | ||
| cookies.set('jwtToken', loginStatus.token); // expire cookie after 1 week | ||
|
||
| if (queryParams.get('redirect')) { | ||
| window.location.href = queryParams.get('redirect'); | ||
| return; | ||
|
|
@@ -54,7 +57,7 @@ export default function Login() { | |
| type="email" | ||
| placeholder="[email protected]" | ||
| required | ||
| autofocus="autofocus" | ||
| autoFocus="autofocus" | ||
| className="input input-bordered w-full" | ||
| /> | ||
| </div> | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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.
Remove local storage line