Skip to content
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

user authentication #83

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
5decaf2
feat: user collection added (#35)
aninda052 Aug 28, 2023
94fdd18
feat: user registration api added (#35)
aninda052 Aug 28, 2023
1f22030
feat: store hash password using bcrypt (#35)
aninda052 Aug 29, 2023
596ee59
feat: user login api added (#35)
aninda052 Aug 29, 2023
de04a1e
feat: generate jwt token for valid user (#35)
aninda052 Aug 30, 2023
a3f147d
feat: set `currentUser` with every request (#35)
aninda052 Aug 30, 2023
f3e35b3
feat: `loginRequired` middleware added (#35)
aninda052 Aug 30, 2023
366501e
feat: (client) login api integrated with frontend (#25)
aninda052 Sep 11, 2023
85e9c2f
feat: (client) add `alert` component for showing alert (#35)
aninda052 Sep 11, 2023
2a2bd6c
feat: (client) registration api integrated with frontend (#25)
aninda052 Sep 12, 2023
3222430
feat: (client) show server error on registration form (#35)
aninda052 Sep 12, 2023
b51451b
feat: (server)registration api server side validation (#35)
aninda052 Sep 12, 2023
5a33ea5
feat: logout mechanism implemented (#35)
aninda052 Sep 13, 2023
0295ceb
refactor: (client) `alert.js` moved from `pages` to `components`
aninda052 Sep 13, 2023
01bdea9
feat: (server) remove password min-max limit from login validation …
aninda052 Sep 13, 2023
5ae6675
feat: (client) show alert using `Context` (#35)
aninda052 Sep 16, 2023
6edd50d
feat: (client) store user info into localstorage (#35)
aninda052 Sep 17, 2023
eb80736
feat: (client) implement protected routes (#35)
aninda052 Sep 17, 2023
758c557
chore: (server) add `JWT_SECRET` env veriable for docker (#35)
aninda052 Sep 17, 2023
b3a95da
feat: (client) show alert after redirect to login page (#35)
aninda052 Sep 17, 2023
035b620
doc: (server) update .env.template (#35)
aninda052 Sep 18, 2023
966c8fa
Merge branch 'main' into feature/user-authentication
aninda052 Sep 18, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 12 additions & 29 deletions client/src/App.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import React, { useState, useEffect } from "react";
import React, { useContext, useEffect } from "react";

import {
Stack,
Alert,
Snackbar,
} from "@mui/material";

// routes
import Router from "./routes";
Expand All @@ -13,19 +8,25 @@ import ThemeProvider from "./theme";
// components
import ScrollToTop from "./components/scroll-to-top";
import { StyledChart } from "./components/chart";
import ShowAlert from "./components/alert";


import { useSocket } from "./contexts/SocketContext";
import { SetAlertContext } from "./contexts/AlertContext";



export default function App() {
const socket = useSocket();
const [wsResponse, setWsResponse] = useState(null);
const setAlertContext = useContext(SetAlertContext);

useEffect(() => {
socket.on("hello", (msg) => {
console.log("hello", msg);
setWsResponse(
`Video ${msg.title} HLS conversion completed as ${msg.originalname}`
);
setAlertContext({
type:'success',
message: `Video ${msg.title} HLS conversion completed as ${msg.originalname}`
});
});
}, [socket]);

Expand All @@ -34,25 +35,7 @@ export default function App() {
<ScrollToTop />
<StyledChart />
<Router />
<Stack>
<Snackbar
open={wsResponse}
autoHideDuration={5000}
onClose={() => {
setWsResponse(null);
}}
anchorOrigin={{ vertical: "top", horizontal: "center" }}
>
<Alert
onClose={() => {
setWsResponse(null);
}}
severity={"success"}
>
{wsResponse}
</Alert>
</Snackbar>
</Stack>
<ShowAlert />
</ThemeProvider>
);
}
51 changes: 51 additions & 0 deletions client/src/components/alert/Alert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// react
import { useContext } from "react";

// @mui
import {
Stack,
Alert,
Snackbar,
} from "@mui/material";

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


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


export default function ShowAlert() {

const alertContext = useContext(AlertContext);
const setAlertContext = useContext(SetAlertContext);

return (
<Stack>
<Snackbar
open={alertContext.message}
autoHideDuration={4000}
onClose={() => {
setAlertContext({
type: '',
message: null
});
}}
anchorOrigin={{ vertical: "top", horizontal: "center" }}
>
<Alert
onClose={() => {
setAlertContext({
type: '',
message: null
});
}}
severity={alertContext.type}
>
{alertContext.message}
</Alert>
</Snackbar>
</Stack>
);
}

1 change: 1 addition & 0 deletions client/src/components/alert/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './Alert';
20 changes: 20 additions & 0 deletions client/src/contexts/AlertContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// react
import React, { createContext, useState } from "react";

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


export const AlertContext = createContext(null);
export const SetAlertContext = createContext(null);

export const AlertProvider = ({ children }) => {
const [alert, setAlert] = useState({type: null, message: null});

return (
<AlertContext.Provider value={alert}>
<SetAlertContext.Provider value={setAlert}>
{children}
</SetAlertContext.Provider>
</AlertContext.Provider>
);
};
5 changes: 4 additions & 1 deletion client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { BrowserRouter } from "react-router-dom";
import { HelmetProvider } from "react-helmet-async";

import { SocketProvider } from "./contexts/SocketContext";
import { AlertProvider } from "./contexts/AlertContext";

//
import App from "./App";
Expand All @@ -17,7 +18,9 @@ root.render(
<HelmetProvider>
<BrowserRouter>
<SocketProvider>
<App />
<AlertProvider>
<App />
</AlertProvider>
</SocketProvider>
</BrowserRouter>
</HelmetProvider>
Expand Down
45 changes: 41 additions & 4 deletions client/src/layouts/dashboard/header/AccountPopover.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
import { useState } from 'react';
// react
import { useState, useContext } 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';

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

// other
import axios from 'axios';

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


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

const MENU_OPTIONS = [
Expand All @@ -26,6 +40,8 @@ const MENU_OPTIONS = [

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

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



const handleLogout = async () => {
await axios.post(
`${API_SERVER}/api/logout`,
{},
{
withCredentials: true,
}
)
.then(function (response){
localStorage.removeItem("name");
localStorage.removeItem("email");
navigate('/login')
})
.catch(function (error){
setAlertContext({type:'error', message: error.response.data.message});
});

};

return (
<>
<IconButton
Expand Down Expand Up @@ -78,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 All @@ -97,7 +134,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
10 changes: 6 additions & 4 deletions client/src/pages/LoginPage.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { Helmet } from 'react-helmet-async';
import { Link, useLocation } from "react-router-dom";
// @mui
import { styled } from '@mui/material/styles';
import { Link, Container, Typography, Divider, Stack, Button } from '@mui/material';
import { Container, Typography, Divider, Stack, Button } from '@mui/material';
// hooks
import useResponsive from '../hooks/useResponsive';
// components
import Logo from '../components/logo';
import Iconify from '../components/iconify';
// sections
import { LoginForm } from '../sections/auth/login';
import { LoginForm } from '../sections/auth/';

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

Expand Down Expand Up @@ -41,6 +42,7 @@ const StyledContent = styled('div')(({ theme }) => ({
// ----------------------------------------------------------------------

export default function LoginPage() {
const location = useLocation();
const mdUp = useResponsive('up', 'md');

return (
Expand Down Expand Up @@ -75,7 +77,7 @@ export default function LoginPage() {

<Typography variant="body2" sx={{ mb: 5 }}>
Don’t have an account? {''}
<Link variant="subtitle2">Get started</Link>
<Link to="/registration">Get started</Link>
</Typography>

<Stack direction="row" spacing={2}>
Expand All @@ -98,7 +100,7 @@ export default function LoginPage() {
</Typography>
</Divider>

<LoginForm />
<LoginForm location={location}/>
</StyledContent>
</Container>
</StyledRoot>
Expand Down
Loading