Skip to content

Commit

Permalink
feat: logout mechanism implemented (foyzulkarim#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
aninda052 committed Sep 13, 2023
1 parent b51451b commit 5a33ea5
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 14 deletions.
33 changes: 32 additions & 1 deletion client/src/layouts/dashboard/header/AccountPopover.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
// react
import { useState } from 'react';
import { useNavigate } from 'react-router-dom';

// @mui
import { alpha } from '@mui/material/styles';
import { Box, Divider, Typography, Stack, MenuItem, Avatar, IconButton, Popover } from '@mui/material';

// mocks_
import account from '../../../_mock/account';

// other
import axios from 'axios';

// constants
import { API_SERVER } from '../../../constants';


// ----------------------------------------------------------------------

const MENU_OPTIONS = [
Expand All @@ -26,6 +37,7 @@ const MENU_OPTIONS = [

export default function AccountPopover() {
const [open, setOpen] = useState(null);
const navigate = useNavigate();

const handleOpen = (event) => {
setOpen(event.currentTarget);
Expand All @@ -35,6 +47,25 @@ export default function AccountPopover() {
setOpen(null);
};



const handleLogout = async () => {
await axios.post(
`${API_SERVER}/api/logout`,
{},
{
withCredentials: true,
}
)
.then(function (response){
navigate('/login')
})
.catch(function (error){
console.log(error)
});

};

return (
<>
<IconButton
Expand Down Expand Up @@ -97,7 +128,7 @@ export default function AccountPopover() {

<Divider sx={{ borderStyle: 'dashed' }} />

<MenuItem onClick={handleClose} sx={{ m: 1 }}>
<MenuItem onClick={handleLogout} sx={{ m: 1 }}>
Logout
</MenuItem>
</Popover>
Expand Down
18 changes: 13 additions & 5 deletions client/src/pages/VideosPage.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
// react
import { Helmet } from 'react-helmet-async';
import { useState, useEffect } from 'react';
import { useNavigate } from 'react-router-dom';

import axios from 'axios';

// @mui
import { Container, Stack, Typography } from '@mui/material';

// components
import {
VideoSort,
VideoList,
ProductCartWidget,
ProductFilterSidebar,
} from '../sections/@dashboard/products';
import { API_SERVER } from '../constants';

// page
import ShowAlert from '../pages/alert'

// other
import axios from 'axios';

// constants
import { API_SERVER } from '../constants';


// ----------------------------------------------------------------------

export default function ProductsPage() {
Expand Down Expand Up @@ -63,13 +71,13 @@ export default function ProductsPage() {
return (
<>
<Helmet>
<title> Dashboard: Products | Minimal UI </title>
<title> Dashboard: Videos </title>
</Helmet>
<ShowAlert data={{alertType, alertMessage, setAlertMessage}} />

<Container>
<Typography variant='h4' sx={{ mb: 5 }}>
Products
Videos
</Typography>

<Stack
Expand Down
2 changes: 1 addition & 1 deletion client/src/sections/auth/LoginForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default function LoginForm() {
.then(function (response){
setAlertType('success');
setAlertMessage('Login Successful');
setTimeout(() => navigate('/videos'), 2000);
setTimeout(() => navigate('/videos'), 1000);
})
.catch(function (error){
setAlertType('error');
Expand Down
16 changes: 10 additions & 6 deletions client/src/sections/auth/RegistrationForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ export default function RegistrationForm() {
await axios.post(`${API_SERVER}/api/registration`,
values,
{
withCredentials: true,
headers: {
'Content-Type': 'application/json'
}
Expand All @@ -64,11 +63,16 @@ export default function RegistrationForm() {
setTimeout(() => navigate('/login'), 2000);
})
.catch(function (errors){
setAlertType('error');
setAlertMessage('Something went wrong');
errors.response.data.message.details.forEach((error) => {
setFieldError(error.context.key, error.message)
})
if(errors.response.data.message.details){
errors.response.data.message.details.forEach((error) => {
setFieldError(error.context.key, error.message)
})
}
else{
setAlertType('error');
setAlertMessage('Something went wrong');
}

});

};
Expand Down
11 changes: 10 additions & 1 deletion server/src/modules/authentication/controller.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { loginValidate, authenticate } = require('./request');
const { generateJwtToken } = require('./utils');
const { setCookie } = require('../../utils/cookie')
const { setCookie, removeCookie } = require('../../utils/cookie')

const setupRoutes = (app) => {

Expand Down Expand Up @@ -43,6 +43,15 @@ const setupRoutes = (app) => {

});

app.post('/api/logout', async (req, res) => {

removeCookie(res, 'Bearer')
return res
.status(200)
.json({ message:'logout successful'});

});

};


Expand Down

0 comments on commit 5a33ea5

Please sign in to comment.