Open nagysobhy opened 1 week ago
To implement Google sign-in functionality on your website, we need to make changes to both the front end and the back end of the application. This involves adding a Google sign-in button on the front end, handling the sign-in process, and managing user sessions on the back end.
File: src/css/custom.css
.google-signin-button {
background-color: #4285F4;
color: white;
padding: 10px 20px;
border: none;
border-radius: 4px;
font-size: 16px;
cursor: pointer;
display: inline-flex;
align-items: center;
justify-content: center;
text-decoration: none;
transition: background-color 0.3s ease;
}
.google-signin-button:hover {
background-color: #357ae8;
}
.google-signin-button:active {
background-color: #2a65c7;
}
.google-signin-button img {
margin-right: 8px;
}
File: src/components/HomepageFeatures/index.js
import React from 'react';
import clsx from 'clsx';
import { GoogleLogin } from 'react-google-login';
import styles from './styles.module.css';
const FeatureList = [
{
title: 'Easy to Use',
Svg: require('@site/static/img/undraw_docusaurus_mountain.svg').default,
description: (
<>
Docusaurus was designed from the ground up to be easily installed and
used to get your website up and running quickly.
</>
),
},
{
title: 'Focus on What Matters',
Svg: require('@site/static/img/undraw_docusaurus_tree.svg').default,
description: (
<>
Docusaurus lets you focus on your docs, and we'll do the chores. Go
ahead and move your docs into the <code>docs</code> directory.
</>
),
},
{
title: 'Powered by React',
Svg: require('@site/static/img/undraw_docusaurus_react.svg').default,
description: (
<>
Extend or customize your website layout by reusing React. Docusaurus can
be extended while reusing the same header and footer.
</>
),
},
];
function Feature({Svg, title, description}) {
return (
<div className={clsx('col col--4')}>
<div className="text--center">
<Svg className={styles.featureSvg} role="img" />
</div>
<div className="text--center padding-horiz--md">
<h3>{title}</h3>
<p>{description}</p>
</div>
</div>
);
}
export default function HomepageFeatures() {
const handleLoginSuccess = (response) => {
console.log('Login Success:', response);
// Handle login success (e.g., send token to the server)
};
const handleLoginFailure = (response) => {
console.log('Login Failed:', response);
// Handle login failure
};
return (
<section className={styles.features}>
<div className="container">
<div className="row">
{FeatureList.map((props, idx) => (
<Feature key={idx} {...props} />
))}
</div>
<div className="text--center">
<GoogleLogin
clientId={process.env.REACT_APP_GOOGLE_CLIENT_ID}
buttonText="Sign in with Google"
onSuccess={handleLoginSuccess}
onFailure={handleLoginFailure}
cookiePolicy={'single_host_origin'}
/>
</div>
</div>
</section>
);
}
react-google-login
DependencyFile: package.json
{
"dependencies": {
"react-google-login": "^5.2.2"
}
}
Run the following command to install the new dependency:
npm install
File: src/server/routes.js
const express = require('express');
const { verifyGoogleToken, createSession, destroySession } = require('./auth');
const router = express.Router();
router.get('/auth/google/callback', async (req, res) => {
try {
const token = req.query.token;
const user = await verifyGoogleToken(token);
if (user) {
await createSession(req, user);
res.redirect('/dashboard');
} else {
res.status(401).send('Unauthorized');
}
} catch (error) {
res.status(500).send('Internal Server Error');
}
});
router.post('/auth/logout', (req, res) => {
destroySession(req);
res.redirect('/');
});
module.exports = router;
File: src/server/auth.js
const { OAuth2Client } = require('google-auth-library');
const client = new OAuth2Client(process.env.GOOGLE_CLIENT_ID);
async function verifyGoogleToken(idToken) {
try {
const ticket = await client.verifyIdToken({
idToken,
audience: process.env.GOOGLE_CLIENT_ID,
});
const payload = ticket.getPayload();
return payload;
} catch (error) {
console.error('Error verifying Google ID token:', error);
throw new Error('Invalid Google ID token');
}
}
function createSession(user) {
// Implement session creation logic here
return {
sessionId: 'some-session-id',
user,
};
}
module.exports = {
verifyGoogleToken,
createSession,
};
File: docusaurus.config.js
require('dotenv').config();
const config = {
customFields: {
googleClientId: process.env.GOOGLE_CLIENT_ID,
googleClientSecret: process.env.GOOGLE_CLIENT_SECRET,
},
};
module.exports = config;
Create a .env
file in the root directory:
GOOGLE_CLIENT_ID=your-google-client-id
GOOGLE_CLIENT_SECRET=your-google-client-secret
google-auth-library
Run the following command to install the necessary dependency:
npm install google-auth-library
This setup ensures that your application can handle Google sign-in on both the front end and the back end, providing a seamless user experience.
Click here to create a Pull Request with the proposed solution
Files used for this task:
docs
directory.
>
),
},
{
title: 'Powered by React',
Svg: require('@site/static/img/undraw_docusaurus_react.svg').default,
description: (
<>
Extend or customize your website layout by reusing React. Docusaurus can
be extended while reusing the same header and footer.
>
),
},
];
function Feature({Svg, title, description}) {
return (
{description}
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