Skip to content

Commit

Permalink
feat: (client) show alert after redirect to login page (foyzulkarim#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
aninda052 committed Sep 17, 2023
1 parent 758c557 commit b3a95da
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion client/src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default function Router() {
const routes = useRoutes([
{
path: '/',
element: isLoggedIn ? <DashboardLayout /> : <Navigate to="/login" state={{ from: location }}/>,
element: isLoggedIn ? <DashboardLayout /> : <Navigate to="/login" state={{ from: location, reason: 'You must log in first' }}/>,
children: [
{ element: <Navigate to='/videos' />, index: true },
{ path: 'dashboard', element: <DashboardAppPage /> },
Expand Down
11 changes: 7 additions & 4 deletions client/src/sections/auth/LoginForm.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// react
import { useState, useContext } from 'react';
import { useState, useContext, useEffect } from 'react';
import { useNavigate } from 'react-router-dom';

// @mui
Expand Down Expand Up @@ -34,6 +34,12 @@ export default function LoginForm({location}) {

const setAlertContext = useContext(SetAlertContext);

useEffect( () => {
if (location.state && location.state.reason){
setAlertContext({type:'error', message: location.state.reason});
}
}, [])

const handleSubmit = async () => {

const redirectUrl = location.state ? location.state.from.pathname : '/videos'
Expand Down Expand Up @@ -71,9 +77,6 @@ export default function LoginForm({location}) {

return (
<>

{/* <ShowAlert data={{alertType, alertMessage, setAlertMessage}} /> */}

<Stack spacing={3}>
<TextField name="email" onChange={handleInput} label="Email address" />

Expand Down

0 comments on commit b3a95da

Please sign in to comment.