File tree Expand file tree Collapse file tree 5 files changed +54
-3
lines changed
components/authentication Expand file tree Collapse file tree 5 files changed +54
-3
lines changed Original file line number Diff line number Diff line change 1+ import React , { useState , useEffect } from "react" ;
2+ import Router from "next/router" ;
3+ import { isAuthenticated } from "../../actions/authentication" ;
4+
5+ const Admin = ( { children} ) => {
6+
7+
8+ useEffect ( ( ) => {
9+ if ( ! isAuthenticated ( ) ) {
10+ Router . push ( `/login` ) ;
11+ } else if ( isAuthenticated ( ) . role !== 1 ) {
12+ Router . push ( `/` ) ;
13+ }
14+ } , [ ] )
15+
16+ return (
17+ < >
18+ { children }
19+ </ >
20+ )
21+
22+ }
23+
24+ export default Admin ;
Original file line number Diff line number Diff line change @@ -31,9 +31,9 @@ const LoginAuth = () => {
3131 // authenticate the user
3232 authenticate ( data , ( ) => {
3333 if ( isAuthenticated ( ) && isAuthenticated ( ) . role === 1 ) {
34- Router . push ( `/admin ` )
34+ Router . push ( `/adminDashboard ` )
3535 } else {
36- Router . push ( `/user ` )
36+ Router . push ( `/userDashboard ` )
3737 }
3838 } )
3939
@@ -54,7 +54,6 @@ const LoginAuth = () => {
5454 return (
5555
5656 < form onSubmit = { handleSubmit } >
57-
5857 { /* Email */ }
5958 < div className = "form-group" >
6059 < input type = "email" value = { email } className = "form-control" onChange = { handleChange ( "email" ) } placeholder = "Enter your @Email address" />
Original file line number Diff line number Diff line change 1+ import React , { useState , useEffect } from "react" ;
2+ import Router from "next/router" ;
3+ import { isAuthenticated } from "../../actions/authentication" ;
4+
5+ const PrivateRoute = ( { children} ) => {
6+
7+
8+ useEffect ( ( ) => {
9+ if ( ! isAuthenticated ( ) ) {
10+ Router . push ( `/login` )
11+ }
12+ } , [ ] )
13+
14+ return (
15+ < >
16+ { children }
17+ </ >
18+ )
19+
20+ }
21+
22+ export default PrivateRoute ;
Original file line number Diff line number Diff line change 11import Layout from "../../components/Layout" ;
2+ import Admin from "../../components/authentication/Admin" ;
23import Link from "next/link" ;
34
45
56const AdminIndex = ( ) => {
67 return (
78 < Layout >
9+ < Admin >
810 < h2 > Admin Dashboard</ h2 >
11+ </ Admin >
912 </ Layout >
1013 )
1114}
Original file line number Diff line number Diff line change 11import Layout from "../../components/Layout" ;
2+ import PrivateRoute from "../../components/authentication/PrivateRoute" ;
23import Link from "next/link" ;
34
45
56const UserIndex = ( ) => {
67 return (
78 < Layout >
9+ < PrivateRoute >
810 < h2 > User Dashboard</ h2 >
11+ </ PrivateRoute >
912 </ Layout >
1013 )
1114}
You can’t perform that action at this time.
0 commit comments