Skip to content

Commit

Permalink
Merge pull request #82 from suvarnakale/shiksha-2.0
Browse files Browse the repository at this point in the history
Issue #PS-320 feat: Remember me and logout functionality
  • Loading branch information
itsvick authored May 7, 2024
2 parents 620293a + b583e45 commit a42d189
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/pages/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import Loader from '../components/Loader';
import Snackbar, { SnackbarOrigin } from '@mui/material/Snackbar';
import Link from '@mui/material/Link';
import Checkbox from '@mui/material/Checkbox';

interface State extends SnackbarOrigin {
openModal: boolean;
}
Expand All @@ -36,6 +35,7 @@ const LoginPage = () => {
const [showPassword, setShowPassword] = useState(false);
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
const [rememberMe, setRememberMe] = useState(true);
const [showToastMessage, setShowToastMessage] = useState(false);
const [usernameError, setUsernameError] = useState(false);
const [passwordError, setPasswordError] = useState(false);
Expand All @@ -59,8 +59,8 @@ const LoginPage = () => {
const { vertical, horizontal, openModal } = state;

useEffect(() => {
const token = localStorage.getItem('token');
if (token) {
const refreshToken = localStorage.getItem('refreshToken');
if (refreshToken) {
router.push('/Dashboard');
}
}, []);
Expand Down Expand Up @@ -109,7 +109,9 @@ const LoginPage = () => {
const token = response?.access_token;
const refreshToken = response?.refresh_token;
localStorage.setItem('token', token);
localStorage.setItem('refreshToken', refreshToken);
rememberMe
? localStorage.setItem('refreshToken', refreshToken)
: localStorage.removeItem('refreshToken');

// const userResponse = await getUserId();
// localStorage.setItem('userId', userResponse?.userId);
Expand Down Expand Up @@ -273,7 +275,10 @@ const LoginPage = () => {
</Link>
</Box>
<Box marginTop={'1rem'} className="RememberMecheckbox">
<Checkbox defaultChecked />
<Checkbox
defaultChecked
onChange={(e) => setRememberMe(e.target.checked)}
/>
{t('LOGIN_PAGE.REMEMBER_ME')}
</Box>
<Box
Expand Down
18 changes: 18 additions & 0 deletions src/pages/Logout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { useEffect } from 'react';
import { useRouter } from 'next/router';

function Logout() {
const router = useRouter();
useEffect(() => {
localStorage.removeItem('token');
localStorage.removeItem('refreshToken');
localStorage.removeItem('userId');
localStorage.removeItem('parentCohortId');

router.push('/');
}, []);

return '';
}

export default Logout;

0 comments on commit a42d189

Please sign in to comment.