Skip to content

Commit

Permalink
feat: (client) store user info into localstorage (foyzulkarim#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
aninda052 committed Sep 17, 2023
1 parent 5ae6675 commit 6edd50d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
14 changes: 10 additions & 4 deletions client/src/layouts/dashboard/header/AccountPopover.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// react
import { useState } from 'react';
import { useState, useContext } from 'react';
import { useNavigate } from 'react-router-dom';

// @mui
Expand All @@ -9,6 +9,9 @@ import { Box, Divider, Typography, Stack, MenuItem, Avatar, IconButton, Popover
// mocks_
import account from '../../../_mock/account';

// Context
import { SetAlertContext } from "../../../contexts/AlertContext";

// other
import axios from 'axios';

Expand Down Expand Up @@ -37,6 +40,7 @@ const MENU_OPTIONS = [

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

const handleOpen = (event) => {
Expand All @@ -58,10 +62,12 @@ export default function AccountPopover() {
}
)
.then(function (response){
localStorage.removeItem("name");
localStorage.removeItem("email");
navigate('/login')
})
.catch(function (error){
console.log(error)
setAlertContext({type:'error', message: error.response.data.message});
});

};
Expand Down Expand Up @@ -109,10 +115,10 @@ export default function AccountPopover() {
>
<Box sx={{ my: 1.5, px: 2.5 }}>
<Typography variant="subtitle2" noWrap>
{account.displayName}
{localStorage.getItem('name')}
</Typography>
<Typography variant="body2" sx={{ color: 'text.secondary' }} noWrap>
{account.email}
{localStorage.getItem('email')}
</Typography>
</Box>

Expand Down
11 changes: 4 additions & 7 deletions client/src/sections/auth/LoginForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ export default function LoginForm() {
const navigate = useNavigate();

const [showPassword, setShowPassword] = useState(false);

// const [alertMessage, setAlertMessage] = useState(null);
// const [alertType, setAlertType] = useState('success');

const [loginData, setloginData] = useState({
email: '',
password: ''
Expand All @@ -40,7 +36,6 @@ export default function LoginForm() {

const handleSubmit = async () => {


await axios.post(`${API_SERVER}/api/login`,
loginData,
{
Expand All @@ -51,7 +46,11 @@ export default function LoginForm() {
}
)
.then(function (response){
console.log(response.data.user.name)
localStorage.setItem("name", response.data.user.name);
localStorage.setItem("email", response.data.user.email);
setAlertContext({type:'success', message: 'Login Successful'});

setTimeout(() => navigate('/videos'), 1000);
})
.catch(function (error){
Expand All @@ -61,7 +60,6 @@ export default function LoginForm() {
};

const handleInput = (event) => {

setloginData({
...loginData,
[event.target.name]: event.target.value
Expand All @@ -70,7 +68,6 @@ export default function LoginForm() {
};



return (
<>

Expand Down

0 comments on commit 6edd50d

Please sign in to comment.