Skip to content

Commit fb8e00b

Browse files
committed
Added OAuth via GitHub
1 parent b323ecb commit fb8e00b

File tree

6 files changed

+298
-194
lines changed

6 files changed

+298
-194
lines changed

src/App.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import createCache from "@emotion/cache";
4141

4242
// Soft UI Dashboard React routes
4343
import routes from "./routes";
44-
import { ProtectedRoute } from './ProtectedRoute';
44+
import { ProtectedRoute } from "./ProtectedRoute";
4545

4646
// Soft UI Dashboard React contexts
4747
import { useSoftUIController, setMiniSidenav, setOpenConfigurator } from "context";
@@ -104,7 +104,7 @@ export default function App() {
104104

105105
if (route.route) {
106106
if (route.protected) {
107-
return(
107+
return (
108108
<Route key={route.key} element={<ProtectedRoute />}>
109109
<Route path={route.route} element={route.component} />
110110
</Route>

src/api/auth.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
import axios from "./index";
2+
import { API_SERVER } from "config/constant";
23

34
class AuthApi {
45
static Login = (data) => {
5-
return axios.post(`${base}/login`, data);
6+
return axios.post(`${API_SERVER}users/login`, data);
67
};
78

89
static Register = (data) => {
9-
return axios.post(`${base}/register`, data);
10+
return axios.post(`${API_SERVER}users/register`, data);
11+
};
12+
13+
static Authorize = (code) => {
14+
return axios.get(`${API_SERVER}sessions/oauth/github?code=${code}`);
1015
};
1116

1217
static Logout = (data) => {
13-
return axios.post(`${base}/logout`, data, { headers: { Authorization: `${data.token}` } });
18+
return axios.post(`${API_SERVER}users/logout`, data, {
19+
headers: { Authorization: `${data.token}` },
20+
});
1421
};
1522
}
1623

17-
let base = "users";
18-
1924
export default AuthApi;

src/config/constant.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ let BACKEND_SERVER = null;
22
if (process.env.REACT_APP_BACKEND_SERVER) {
33
BACKEND_SERVER = process.env.REACT_APP_BACKEND_SERVER;
44
} else {
5-
BACKEND_SERVER = "http://localhost:5000/";
5+
BACKEND_SERVER = "http://localhost:5000/api/";
66
}
77

88
export const API_SERVER = BACKEND_SERVER;
9+
export const ADMIN = 1;
10+
export const USER = 2;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import SoftButton from "components/SoftButton";
2+
import SoftBox from "components/SoftBox";
3+
4+
const client_id = process.env.REACT_APP_GITHUB_OAUTH_CLIENT_ID;
5+
const redirect_uri = process.env.REACT_APP_GITHUB_OAUTH_REDIRECT_URL;
6+
7+
const GithubSocial = () => {
8+
return (
9+
<SoftBox px={1} mt={2}>
10+
<a
11+
href={`https://github.com/login/oauth/authorize?scope=user&client_id=${client_id}&redirect_uri=${redirect_uri}`}
12+
>
13+
<SoftButton variant="outlined" color="light">
14+
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="32" viewBox="0 0 24 24">
15+
<path d="M12 0c-6.626 0-12 5.373-12 12 0 5.302 3.438 9.8 8.207 11.387.599.111.793-.261.793-.577v-2.234c-3.338.726-4.033-1.416-4.033-1.416-.546-1.387-1.333-1.756-1.333-1.756-1.089-.745.083-.729.083-.729 1.205.084 1.839 1.237 1.839 1.237 1.07 1.834 2.807 1.304 3.492.997.107-.775.418-1.305.762-1.604-2.665-.305-5.467-1.334-5.467-5.931 0-1.311.469-2.381 1.236-3.221-.124-.303-.535-1.524.117-3.176 0 0 1.008-.322 3.301 1.23.957-.266 1.983-.399 3.003-.404 1.02.005 2.047.138 3.006.404 2.291-1.552 3.297-1.23 3.297-1.23.653 1.653.242 2.874.118 3.176.77.84 1.235 1.911 1.235 3.221 0 4.609-2.807 5.624-5.479 5.921.43.372.823 1.102.823 2.222v3.293c0 .319.192.694.801.576 4.765-1.589 8.199-6.086 8.199-11.386 0-6.627-5.373-12-12-12z" />
16+
</svg>
17+
</SoftButton>
18+
</a>
19+
</SoftBox>
20+
);
21+
};
22+
23+
export default GithubSocial;

src/layouts/authentication/sign-in/index.js

Lines changed: 140 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ Coded by www.creative-tim.com
1313
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
1414
*/
1515

16-
import { useState } from "react";
17-
16+
import { useState, useEffect } from "react";
17+
import { RotatingLines } from "react-loader-spinner";
1818
// react-router-dom components
19-
import { Link } from "react-router-dom";
19+
import { Link, useNavigate } from "react-router-dom";
2020

2121
// @mui material components
2222
import Switch from "@mui/material/Switch";
@@ -29,52 +29,57 @@ import SoftButton from "components/SoftButton";
2929

3030
// Authentication layout components
3131
import CoverLayout from "layouts/authentication/components/CoverLayout";
32+
import GithubSocial from "layouts/authentication/components/Socials/github";
33+
import Separator from "layouts/authentication/components/Separator";
3234

3335
// Images
3436
import curved9 from "assets/images/curved-images/curved-6.jpg";
3537

3638
import AuthApi from "../../../api/auth";
37-
import { useNavigate } from 'react-router-dom';
3839
import { useAuth } from "../../../auth-context/auth.context";
40+
import { API_SERVER } from "config/constant";
3941

4042
function SignIn() {
4143
const navigate = useNavigate();
4244

4345
const [rememberMe, setRememberMe] = useState(true);
4446
const [formData, setFormData] = useState({});
45-
const [error, setError] = useState("")
47+
const [error, setError] = useState("");
48+
const [isLoading, setIsLoading] = useState(false);
4649

4750
const { setUser } = useAuth();
4851
const { user } = useAuth();
4952

5053
const handleSetRememberMe = () => setRememberMe(!rememberMe);
5154

52-
const handleFormData = e => {
55+
const handleFormData = (e) => {
5356
setFormData({
5457
...formData,
55-
[e.target.name]: e.target.value
56-
})
57-
}
58+
[e.target.name]: e.target.value,
59+
});
60+
};
5861

59-
const submitFormData = e => {
62+
const submitFormData = (e) => {
6063
e.preventDefault();
61-
AuthApi.Login(formData).then(response => {
62-
if(response.data.success) {
63-
return setProfile(response);
64-
} else {
65-
setError(response.data.msg)
66-
}
67-
}).catch(error => {
68-
if (error.response) {
69-
return setError(error.response.data.msg);
70-
}
71-
return setError("There has been an error.");
72-
})
73-
}
64+
AuthApi.Login(formData)
65+
.then((response) => {
66+
if (response.data.success) {
67+
return setProfile(response);
68+
} else {
69+
setError(response.data.msg);
70+
}
71+
})
72+
.catch((error) => {
73+
if (error.response) {
74+
return setError(error.response.data.msg);
75+
}
76+
return setError("There has been an error.");
77+
});
78+
};
7479

7580
const handleRedirect = () => {
7681
return navigate("/dashboard");
77-
}
82+
};
7883

7984
const setProfile = (response) => {
8085
let user = { ...response.data.user };
@@ -85,51 +90,102 @@ function SignIn() {
8590
return navigate("/dashboard");
8691
};
8792

93+
useEffect(() => {
94+
const url = window.location.href;
95+
const hasCode = url.includes("?code=");
96+
97+
// If Github API returns the code parameter
98+
if (hasCode) {
99+
const newUrl = url.split("?code=");
100+
window.history.pushState({}, null, newUrl[0]);
101+
setIsLoading(true);
102+
103+
const requestData = {
104+
code: newUrl[1],
105+
};
106+
107+
AuthApi.Authorize(requestData.code)
108+
.then(({ data }) => {
109+
if (data.user) {
110+
setUser(JSON.stringify(data.user));
111+
localStorage.setItem("user", JSON.stringify(data.user));
112+
handleRedirect();
113+
} else {
114+
setError("no user returned");
115+
}
116+
})
117+
.catch((error) => {
118+
setError(error.message);
119+
})
120+
.finally(() => setIsLoading(false));
121+
}
122+
}, []);
123+
88124
return (
89125
<CoverLayout
90126
title="Welcome back"
91-
description="Enter your email and password to sign in"
127+
description="Login through github or enter your email and password to sign in"
92128
image={curved9}
93129
>
94-
{user && user.token ? (
95-
<div>
96-
<h3 style={{ textAlign: "center" }}>You are already signed in.</h3>
97-
<SoftBox mt={4} mb={1}>
98-
<SoftButton variant="gradient" buttonColor="info" fullWidth onClick={handleRedirect}>
99-
{`Let's go`}
100-
</SoftButton>
130+
{isLoading ? (
131+
<SoftBox display="flex" justifyContent="center">
132+
<RotatingLines
133+
strokeColor="black"
134+
strokeWidth="5"
135+
animationDuration="0.75"
136+
width="96"
137+
visible={true}
138+
/>
101139
</SoftBox>
102-
</div>
103-
) : (
104-
<SoftBox component="form" role="form">
105-
<SoftBox mb={2}>
106-
<SoftBox mb={1} ml={0.5}>
107-
<SoftTypography component="label" variant="caption" fontWeight="bold">
108-
Email
109-
</SoftTypography>
140+
) : user && user.token ? (
141+
<div>
142+
<h3 style={{ textAlign: "center" }}>You are already signed in.</h3>
143+
<SoftBox mt={4} mb={1}>
144+
<SoftButton variant="gradient" buttonColor="info" fullWidth onClick={handleRedirect}>
145+
{`Let's go`}
146+
</SoftButton>
110147
</SoftBox>
111-
<SoftInput type="email" name="email" onChange={handleFormData} placeholder="Email" />
112-
</SoftBox>
113-
<SoftBox mb={2}>
114-
<SoftBox mb={1} ml={0.5}>
115-
<SoftTypography component="label" variant="caption" fontWeight="bold">
116-
Password
117-
</SoftTypography>
148+
</div>
149+
) : (
150+
<>
151+
<SoftBox display="flex" flexDirection="column" alignItems="center" mb={2}>
152+
<GithubSocial />
118153
</SoftBox>
119-
<SoftInput type="password" name="password" onChange={handleFormData} placeholder="Password" />
120-
</SoftBox>
121-
<SoftBox display="flex" alignItems="center">
122-
<Switch checked={rememberMe} onChange={handleSetRememberMe} />
123-
<SoftTypography
124-
variant="button"
125-
fontWeight="regular"
126-
onClick={handleSetRememberMe}
127-
sx={{ cursor: "pointer", userSelect: "none" }}
128-
>
129-
&nbsp;&nbsp;Remember me
130-
</SoftTypography>
131-
</SoftBox>
132-
<SoftBox mt={2} mb={2} textAlign="center">
154+
<Separator />
155+
<SoftBox component="form" role="form">
156+
<SoftBox mb={2}>
157+
<SoftBox mb={1} ml={0.5}>
158+
<SoftTypography component="label" variant="caption" fontWeight="bold">
159+
Email
160+
</SoftTypography>
161+
</SoftBox>
162+
<SoftInput type="email" name="email" onChange={handleFormData} placeholder="Email" />
163+
</SoftBox>
164+
<SoftBox mb={2}>
165+
<SoftBox mb={1} ml={0.5}>
166+
<SoftTypography component="label" variant="caption" fontWeight="bold">
167+
Password
168+
</SoftTypography>
169+
</SoftBox>
170+
<SoftInput
171+
type="password"
172+
name="password"
173+
onChange={handleFormData}
174+
placeholder="Password"
175+
/>
176+
</SoftBox>
177+
<SoftBox display="flex" alignItems="center">
178+
<Switch checked={rememberMe} onChange={handleSetRememberMe} />
179+
<SoftTypography
180+
variant="button"
181+
fontWeight="regular"
182+
onClick={handleSetRememberMe}
183+
sx={{ cursor: "pointer", userSelect: "none" }}
184+
>
185+
&nbsp;&nbsp;Remember me
186+
</SoftTypography>
187+
</SoftBox>
188+
<SoftBox mt={2} mb={2} textAlign="center">
133189
<h6
134190
style={{
135191
fontSize: ".8em",
@@ -142,27 +198,29 @@ function SignIn() {
142198
{error}
143199
</h6>
144200
</SoftBox>
145-
<SoftBox mt={4} mb={1}>
146-
<SoftButton variant="gradient" color="info" onClick={submitFormData} fullWidth>
147-
sign in
148-
</SoftButton>
149-
</SoftBox>
150-
<SoftBox mt={3} textAlign="center">
151-
<SoftTypography variant="button" color="text" fontWeight="regular">
152-
Don&apos;t have an account?{" "}
153-
<SoftTypography
154-
component={Link}
155-
to="/authentication/sign-up"
156-
variant="button"
157-
color="info"
158-
fontWeight="medium"
159-
textGradient
160-
>
161-
Sign up
162-
</SoftTypography>
163-
</SoftTypography>
164-
</SoftBox>
165-
</SoftBox> )}
201+
<SoftBox mt={4} mb={1}>
202+
<SoftButton variant="gradient" color="info" onClick={submitFormData} fullWidth>
203+
sign in
204+
</SoftButton>
205+
</SoftBox>
206+
<SoftBox mt={3} textAlign="center">
207+
<SoftTypography variant="button" color="text" fontWeight="regular">
208+
Don&apos;t have an account?{" "}
209+
<SoftTypography
210+
component={Link}
211+
to="/authentication/sign-up"
212+
variant="button"
213+
color="info"
214+
fontWeight="medium"
215+
textGradient
216+
>
217+
Sign up
218+
</SoftTypography>
219+
</SoftTypography>
220+
</SoftBox>
221+
</SoftBox>
222+
</>
223+
)}
166224
</CoverLayout>
167225
);
168226
}

0 commit comments

Comments
 (0)