Skip to content

Commit b430164

Browse files
committed
Register button now calls register api route. Put AuthorizeRoute back in.
1 parent ab0aab9 commit b430164

File tree

7 files changed

+47
-10
lines changed

7 files changed

+47
-10
lines changed

.env

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
REACT_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

src/App.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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;

src/components/HeaderComponent.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff 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

src/redux/Slices/LoginSlice.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff 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

src/views/HomeView.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,22 @@ import { Route, Switch } from "react-router-dom";
44

55
import HeaderComponent from "../components/HeaderComponent";
66
import FooterComponent from "../components/FooterComponent";
7-
import LoginView from "./LoginView";
7+
import LoginRegisterView from "./LoginRegisterView";
88
import LogoutView from "./LogoutView";
99
import LoginSuccessView from "./LoginSuccessView";
1010
import ProjectsListScreenView from "./ProjectsListScreenView";
11+
import AuthorizeRoute from "../Authentication/AuthorizeRoute";
1112

1213
const 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 />
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@ import { useSelector, useDispatch } from "react-redux";
33
import login from "../redux/Slices/LoginSlice";
44
import { 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;

0 commit comments

Comments
 (0)