From 367f92630caec9bfe73b8ba9b55ce1126a613945 Mon Sep 17 00:00:00 2001
From: kutaysaran <74209499+kutaysaran@users.noreply.github.com>
Date: Thu, 9 Nov 2023 23:31:47 +0300
Subject: [PATCH 1/2] Google Sign in
---
.../frontend/src/Pages/Auth/Google/index.jsx | 35 +++++++++++++++++++
.../frontend/src/Pages/Auth/SignIn/index.jsx | 8 +++--
.../frontend/src/Routes/Router.jsx | 4 +++
.../frontend/src/api/requests/googleLogin.jsx | 25 +++++++++++++
4 files changed, 70 insertions(+), 2 deletions(-)
create mode 100644 prediction-polls/frontend/src/Pages/Auth/Google/index.jsx
create mode 100644 prediction-polls/frontend/src/api/requests/googleLogin.jsx
diff --git a/prediction-polls/frontend/src/Pages/Auth/Google/index.jsx b/prediction-polls/frontend/src/Pages/Auth/Google/index.jsx
new file mode 100644
index 00000000..13ced48b
--- /dev/null
+++ b/prediction-polls/frontend/src/Pages/Auth/Google/index.jsx
@@ -0,0 +1,35 @@
+import React, { useEffect } from 'react';
+import { useLocation } from 'react-router-dom';
+import googleLogin from '../../../api/requests/googleLogin';
+
+function GoogleLogin() {
+ const location = useLocation();
+
+ useEffect(() => {
+ const code = new URLSearchParams(location.search).get('code');
+ console.log(code);
+ if (code) {
+ // Call the googleLogin function with the code
+ googleLogin(code).then(success => {
+ if (success) {
+ // Redirect to home page or dashboard if login was successful
+ console.log('Logged in successfully');
+ // Replace with your path to redirect
+ // this could be using history.push('/path') if you're using react-router
+ } else {
+ // Handle the error scenario, maybe set an error message state and display it
+ console.error('Failed to log in with Google');
+ }
+ });
+ }
+ }, [location]);
+
+ return (
+
+
Google Login Page
+ {/* Render additional UI elements or messages here if needed */}
+
+ );
+}
+
+export default GoogleLogin;
diff --git a/prediction-polls/frontend/src/Pages/Auth/SignIn/index.jsx b/prediction-polls/frontend/src/Pages/Auth/SignIn/index.jsx
index 3defd3a1..869b6595 100644
--- a/prediction-polls/frontend/src/Pages/Auth/SignIn/index.jsx
+++ b/prediction-polls/frontend/src/Pages/Auth/SignIn/index.jsx
@@ -86,12 +86,16 @@ const messageStyle = {
color: "#FC1612"
}
+
function SignIn() {
const [passwordVisible, setPasswordVisible] = React.useState(false);
const [username, setUsername] = React.useState("");
const [password, setPassword] = React.useState("");
const [message, setMessage] = React.useState("");
const navigate = useNavigate();
+ const handleLogin = () => {
+ window.location.href = getGoogleOAuthURL();
+ };
const handleSignIn = async (e) => {
e.preventDefault();
@@ -104,7 +108,7 @@ function SignIn() {
password: password
})
};
- const response = await fetch(process.env.REACT_APP_BACKEND_LINK+'/login', requestOptions);
+ const response = await fetch(process.env.REACT_APP_BACKEND_LINK+'/auth/login', requestOptions);
const data = await response.json();
if (response.status === 201 && data.accessToken && data.refreshToken) {
@@ -128,7 +132,7 @@ function SignIn() {
-