Open alexanmtz opened 3 days ago
To implement Google sign-in functionality, we need to ensure that both the backend and frontend components are correctly configured to handle Google OAuth authentication. This involves setting up the Google OAuth strategy on the backend, defining the necessary routes for authentication, and updating the frontend to initiate the OAuth flow and handle the authentication response.
File: config/passport.js
clientID
, clientSecret
, and callbackURL
).const GoogleStrategy = require('passport-google-oauth20').Strategy;
const secrets = require('./secrets');
passport.use(new GoogleStrategy({
clientID: secrets.google.id,
clientSecret: secrets.google.secret,
callbackURL: secrets.oauthCallbacks.googleCallbackUrl
}, (accessToken, refreshToken, profile, done) => {
// Handle user authentication and creation logic
}));
File: modules/app/routes/auth.js
const express = require('express');
const passport = require('passport');
const router = express.Router();
router.get('/authorize/google', passport.authenticate('google', { scope: ['email'] }));
router.get('/callback/google', passport.authenticate('google', {
successRedirect: '/',
failureRedirect: '/signin'
}));
module.exports = router;
File: frontend/src/actions/loginActions.js
export const AUTHORIZED_GOOGLE_REQUESTED = 'AUTHORIZED_GOOGLE_REQUESTED';
export const AUTHORIZED_GOOGLE_SUCCESS = 'AUTHORIZED_GOOGLE_SUCCESS';
export const AUTHORIZED_GOOGLE_ERROR = 'AUTHORIZED_GOOGLE_ERROR';
const authorizedGoogleRequested = () => ({ type: AUTHORIZED_GOOGLE_REQUESTED, logged: false, completed: false });
const authorizedGoogleSuccess = user => ({ type: AUTHORIZED_GOOGLE_SUCCESS, logged: true, completed: true, user });
const authorizedGoogleError = error => ({ type: AUTHORIZED_GOOGLE_ERROR, logged: false, completed: true, error });
export const authorizeGoogle = () => {
return dispatch => {
dispatch(authorizedGoogleRequested());
window.location.href = `${api.API_URL}/authorize/google`;
};
};
File: frontend/src/reducers/loginReducer.js
import {
AUTHORIZED_GOOGLE_SUCCESS,
AUTHORIZED_GOOGLE_ERROR
} from '../actions/loginActions';
export const loggedIn = (state = { logged: false, user: {}, completed: true, error: {} }, action) => {
switch (action.type) {
case AUTHORIZED_GOOGLE_SUCCESS:
return { ...state, logged: true, user: action.user, completed: true };
case AUTHORIZED_GOOGLE_ERROR:
return { ...state, logged: false, completed: true, error: action.error };
default:
return state;
}
};
File: frontend/src/components/session/provider-login-buttons.tsx
import React from 'react';
import { Button } from '@material-ui/core';
import GoogleLogo from '../../images/google-logo.png';
import api from '../../consts';
const ProviderLoginButtons = ({ provider }) => (
<div>
<Button
href={`${api.API_URL}/authorize/google`}
variant='contained'
color='secondary'
disabled={provider === 'google'}
>
<img width='16' src={GoogleLogo} />
<span style={{ marginLeft: 10 }}>Google</span>
</Button>
</div>
);
export default ProviderLoginButtons;
Click here to create a Pull Request with the proposed solution
Files used for this task:
What - description of what you me to do Example: Hey @autopilot implement a Google sign-in on my website. Make changes to the front end and the back end of the application
Why - explain why this is important Example: I want to allow users to signup and login using their Google account