File tree Expand file tree Collapse file tree 7 files changed +47
-10
lines changed
Expand file tree Collapse file tree 7 files changed +47
-10
lines changed Original file line number Diff line number Diff line change 11REACT_APP_API_URL = https://localhost:5001
2- REACT_APP_LOGIN_URL = /register?account=github&redirect=http://localhost:3000/login-callback
2+ REACT_APP_LOGIN_URL = /login?account=github&redirect=http://localhost:3000/login-callback
3+ REACT_APP_SIGN_UP_URL = /register?account=github&redirect=http://localhost:3000/login-callback
Original file line number Diff line number Diff line change @@ -13,10 +13,10 @@ const App = () => (
1313 < React . Fragment >
1414 < BrowserRouter >
1515 < Switch >
16+ < Route path = "/" component = { HomeView } />
1617 < Route path = "/login-callback" component = { LoginCallback } />
1718 < Route path = "/login-success" component = { LoginSuccessView } />
1819 < Route path = "/logout" component = { LogoutView } />
19- < Route path = "/" component = { HomeView } />
2020 </ Switch >
2121 </ BrowserRouter >
2222 </ React . Fragment >
Original file line number Diff line number Diff line change 1+ import React from "react" ;
2+ import { Route } from "react-router-dom" ;
3+ import { useSelector , useDispatch } from "react-redux" ;
4+ import login from "../redux/Slices/LoginSlice" ;
5+
6+ const AuthorizeRoute = ( props ) => {
7+ const isLoggedIn = useSelector ( ( state ) => state . login . isLoggedIn ) ;
8+ const dispatch = useDispatch ( ) ;
9+
10+ React . useEffect ( ( ) => {
11+ if ( isLoggedIn === false )
12+ dispatch ( login . actions . redirectToLogin ( props . path ) ) ;
13+ } , [ isLoggedIn ] ) ;
14+
15+ return < Route { ...props } /> ;
16+ } ;
17+
18+ export default AuthorizeRoute ;
Original file line number Diff line number Diff line change @@ -17,7 +17,7 @@ const HeaderComponent = () => {
1717 inlineIndent = "10em"
1818 theme = "dark"
1919 mode = "horizontal"
20- defaultSelectedKeys = { [ "2 " ] }
20+ defaultSelectedKeys = { [ "1 " ] }
2121 className = "main-menu w-1/2 float-left"
2222 >
2323 < Menu . Item key = "1" > Projects</ Menu . Item >
@@ -37,7 +37,9 @@ const LoginButtons = () => (
3737 < Link to = "login" >
3838 < Button type = "primary" > Login</ Button >
3939 </ Link >
40- < Button type = "primary" > Sign up</ Button >
40+ < Link to = "signup" >
41+ < Button type = "primary" > Sign up</ Button >
42+ </ Link >
4143 </ React . Fragment >
4244) ;
4345
Original file line number Diff line number Diff line change @@ -16,6 +16,15 @@ function redirectToLogin(state, action) {
1616 window . location . href = redirectUrl ;
1717}
1818
19+ /*
20+ * Redirects to the login page with the current state
21+ * Sends the current state too
22+ */
23+ function redirectToSignUp ( state , action ) {
24+ const redirectUrl = `${ process . env . REACT_APP_API_URL } ${ process . env . REACT_APP_SIGN_UP_URL } ` ;
25+ window . location . href = redirectUrl ;
26+ }
27+
1928/*
2029 * Recieves back the state from 'redirectToLogin'
2130 */
@@ -37,6 +46,7 @@ const login = createSlice({
3746 reducers : {
3847 redirectToLogin,
3948 loginCallback,
49+ redirectToSignUp,
4050 } ,
4151} ) ;
4252
Original file line number Diff line number Diff line change @@ -4,20 +4,22 @@ import { Route, Switch } from "react-router-dom";
44
55import HeaderComponent from "../components/HeaderComponent" ;
66import FooterComponent from "../components/FooterComponent" ;
7- import LoginView from "./LoginView " ;
7+ import LoginRegisterView from "./LoginRegisterView " ;
88import LogoutView from "./LogoutView" ;
99import LoginSuccessView from "./LoginSuccessView" ;
1010import ProjectsListScreenView from "./ProjectsListScreenView" ;
11+ import AuthorizeRoute from "../Authentication/AuthorizeRoute" ;
1112
1213const HomeView = ( ) => (
1314 < Layout >
1415 < HeaderComponent />
1516 < Layout . Content className = "site-layout" style = { styles . content } >
1617 < Switch >
17- < Route path = "/login" component = { LoginView } />
18+ < Route path = "/login" render = { ( ) => < LoginRegisterView isLogin /> } />
19+ < Route path = "/signup" render = { ( ) => < LoginRegisterView isRegister /> } />
1820 < Route path = "/login-success" component = { LoginSuccessView } />
1921 < Route path = "/logout" component = { LogoutView } />
20- < Route path = "/projects" component = { ProjectsListScreenView } />
22+ < AuthorizeRoute path = "/projects" component = { ProjectsListScreenView } />
2123 </ Switch >
2224 </ Layout . Content >
2325 < FooterComponent />
Original file line number Diff line number Diff line change @@ -3,14 +3,18 @@ import { useSelector, useDispatch } from "react-redux";
33import login from "../redux/Slices/LoginSlice" ;
44import { GithubLoginButton } from "react-social-login-buttons" ;
55
6- const LoginView = ( props ) => {
6+ const LoginRegisterView = ( props ) => {
77 const isLoggedIn = useSelector ( ( state ) => state . login . isLoggedIn ) ;
88 const dispatch = useDispatch ( ) ;
99
1010 const initLogin = ( provider ) => {
11+ let action = props . isLogin
12+ ? login . actions . redirectToLogin
13+ : login . actions . redirectToSignUp ;
14+ debugger ;
1115 if ( isLoggedIn === false ) {
1216 if ( provider === "github" ) {
13- dispatch ( login . actions . redirectToLogin ( props . path ) ) ;
17+ dispatch ( action ( props . path ) ) ;
1418 }
1519 }
1620 } ;
@@ -28,4 +32,4 @@ const LoginView = (props) => {
2832 ) ;
2933} ;
3034
31- export default LoginView ;
35+ export default LoginRegisterView ;
You can’t perform that action at this time.
0 commit comments