-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #341 from bounswe/frontend/feature/google-sign-in
Frontend/feature/google sign in
- Loading branch information
Showing
4 changed files
with
63 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import React, { useEffect } from 'react'; | ||
import { useNavigate, useLocation } from 'react-router-dom'; | ||
import googleLogin from '../../../api/requests/googleLogin'; | ||
|
||
function GoogleLogin() { | ||
const navigate = useNavigate(); | ||
const location = useLocation(); | ||
|
||
useEffect(() => { | ||
const code = new URLSearchParams(location.search).get('code'); | ||
if (code) { | ||
googleLogin(code).then(success => { | ||
if (success) { | ||
// Redirect to profile if login was successful | ||
navigate('/profile'); | ||
} else { | ||
// Redirect to a login error page or back to login | ||
navigate('/auth/sign-in'); | ||
} | ||
}); | ||
} | ||
}, [location.search, navigate]); | ||
|
||
return ( | ||
<div> | ||
<h1>Redirecting..</h1> | ||
</div> | ||
); | ||
} | ||
|
||
export default GoogleLogin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
prediction-polls/frontend/src/api/requests/googleLogin.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const baseUrl = process.env.REACT_APP_BACKEND_LINK; | ||
async function googleLogin(code) { | ||
try { | ||
const response = await fetch(`${baseUrl}/auth/google`, { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify({ 'code': code }) | ||
}); | ||
const data = await response.json(); | ||
if (response.status === 200 && data.accessToken && data.refreshToken) { | ||
localStorage.setItem('accessToken', data.accessToken); | ||
localStorage.setItem('refreshToken', data.refreshToken); | ||
return { success: true }; | ||
} else { | ||
return { success: false }; | ||
} | ||
} catch (error) { | ||
return false; | ||
} | ||
} | ||
|
||
export default googleLogin; |