Open pranayseela opened 2 months 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. The front end will handle the user interaction and the initial sign-in process, while the back end will handle the verification of the Google token and manage user sessions.
backend/routes.js
express
and the custom authentication module (auth.js
).const express = require('express');
const { verifyGoogleToken } = require('./auth');
const router = express.Router();
router.get('/auth/google', (req, res) => {
const googleSignInURL = 'https://accounts.google.com/o/oauth2/v2/auth?client_id=YOUR_CLIENT_ID&redirect_uri=YOUR_REDIRECT_URI&response_type=code&scope=email profile';
res.redirect(googleSignInURL);
});
router.get('/auth/google/callback', async (req, res) => {
const { code } = req.query;
try {
const user = await verifyGoogleToken(code);
// Handle user session (e.g., create a session, issue a JWT, etc.)
res.status(200).json({ message: 'Sign-in successful', user });
} catch (error) {
res.status(400).json({ message: 'Sign-in failed', error: error.message });
}
});
module.exports = router;
backend/auth.js
google-auth-library
and express-session
installed.express-session
to manage user sessions.const { OAuth2Client } = require('google-auth-library');
const session = require('express-session');
const CLIENT_ID = 'YOUR_GOOGLE_CLIENT_ID';
const client = new OAuth2Client(CLIENT_ID);
const sessionMiddleware = session({
secret: 'your_secret_key',
resave: false,
saveUninitialized: true,
cookie: { secure: false }
});
async function verifyGoogleToken(token) {
const ticket = await client.verifyIdToken({
idToken: token,
audience: CLIENT_ID,
});
const payload = ticket.getPayload();
return payload;
}
async function handleGoogleSignIn(req, res) {
const token = req.body.token;
try {
const userData = await verifyGoogleToken(token);
req.session.user = {
id: userData.sub,
email: userData.email,
name: userData.name,
};
res.status(200).send({ message: 'Sign-in successful', user: req.session.user });
} catch (error) {
res.status(401).send({ message: 'Invalid token', error: error.message });
}
}
module.exports = {
sessionMiddleware,
handleGoogleSignIn,
};
js/main.js
index.html
file.;(function () {
'use strict';
function initializeGoogleSignIn() {
gapi.load('auth2', function() {
gapi.auth2.init({
client_id: 'YOUR_GOOGLE_CLIENT_ID.apps.googleusercontent.com'
});
});
}
function onSignIn(googleUser) {
var profile = googleUser.getBasicProfile();
var id_token = googleUser.getAuthResponse().id_token;
$.ajax({
type: 'POST',
url: '/auth/google',
data: { id_token: id_token },
success: function(response) {
console.log('User signed in successfully');
},
error: function(error) {
console.log('Error signing in', error);
}
});
}
function signOut() {
var auth2 = gapi.auth2.getAuthInstance();
auth2.signOut().then(function () {
console.log('User signed out.');
});
}
$(function(){
initializeGoogleSignIn();
});
window.onSignIn = onSignIn;
window.signOut = signOut;
}());
index.html
<head>
section.<!DOCTYPE html>
<html class="no-js">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>TEKVENUE</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="TEKVENUE" />
<meta name="keywords" content="TEKVENUE" />
<meta name="author" content="Pranay" />
<link rel="shortcut icon" href="favicon.ico">
<link href='https://fonts.googleapis.com/css?family=Open+Sans:400,300,700' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="css/animate.css">
<link rel="stylesheet" href="css/icomoon.css">
<link rel="stylesheet" href="css/bootstrap.css">
<link rel="stylesheet" href="css/owl.carousel.min.css">
<link rel="stylesheet" href="css/owl.theme.default.min.css">
<link rel="stylesheet" href="css/index.css">
<script src="js/modernizr-2.6.2.min.js"></script>
<script src="https://apis.google.com/js/platform.js" async defer></script>
<meta name="google-signin-client_id" content="YOUR_GOOGLE_CLIENT_ID.apps.googleusercontent.com">
</head>
<body style="background-color:black;">
<div class="box-wrap">
<header role="banner" id="fh5co-header" style="background-color:black; margin-bottom:0em; padding-bottom:1em;">
<div class="container" style="margin-left:0px;">
<nav class="navbar navbar-default" style="background-color:black;">
<div class="row" style="background-color:black;">
<div class="col-md-3">
<div class="fh5co-navbar-brand" style="line-height:0;">
<img src="images/logo.jpg" alt="TEKVENUE" height="42" width="42">
<a class="fh5co-logo" href="index.html" style="color:orange;"> TEKVENUE</a>
<p style="font-size:12px; color:deeppink; margin-left:72px;">The Place For The Technology</p>
</div>
</div>
<div class="col-md-9 main-nav">
<ul class="nav text-right">
<li><a href="index.html" style="color:orange;">HOME</a></li>
<li><a href="about.html" style="color:cornflowerblue;">About Us</a></li>
<li><a href="services.html" style="color:deeppink;">Services</a></li>
<li><a href="contact.html" style="color:green;">Contact Us</a></li>
</ul>
</div>
</div>
</nav>
</div>
</header>
<div id="fh5co-intro-section" class="section-overlay animate-box" data-animate-effect="fadeIn">
<img src="images/img_large_11.jpg" alt="TEKVENUE" height="400" width="100%">
</div>
</div>
<div id="fh5co-media-section">
<div class="container">
<div class="row">
<div class="col-md-7 animate-box" style="width:50%">
<div class="fh5co-cover" style="background-image: url(images/work-1.jpg);">
<div class="desc" style="position:relative;background:#996300;">
<h1 style="color:white;text-decoration: underline;">Who we are</h1>
<p>We are an Information Technology company focused on IT staffing and software project development.</p>
<p>Tekvenue is a global leader when considering the best means of support of your business’s processes, data analytics, reporting systems, and information. Tying together the vast interconnected computing systems in today’s corporations is a challenging task even in the best of times. Ensuring that all systems involved in the process flow can “talk” to each other and keeping all departments, business units, and customers informed is part of Tekvenue’s commitment to Enterprise System Management (ESM).</p>
<p>By leveraging the best minds in the fields of ESM, Tekvenue is uniquely positioned to structure your IT processes to maximize your return on investment.</p>
<span>Learn More</span>
</div>
</div>
</div>
<div class="col-md-5 animate-box" style="width:50%">
<div class="fh5co-cover" style="background-image: url(images/work-1.jpg);">
<div class="desc" style="position:relative;background:#996300;">
<h1 style="color:white;text-decoration: underline;">Our vision</h1>
<p>Our vision is to become a leading technology company with strong focus on customer relationships, coherence with partners, and employee creativity.</p>
<p>We serve clients in critical, technology-dependent industries such as health care, telecom, banking, finance, manufacturing, petroleum, retail, and beyond. We deploy our renowned staffing and IT solutions in the United States, India, and Canada. Contact us today and let us apply our years of experience in IT and business development to your technology needs.</p>
<p>We love new projects! Let's get started.</p>
<span>Learn More</span>
</br>
</br>
</br>
</br>
</div>
</div>
</div>
</div>
</div>
</div>
<div id="fh5co-intro-section" style="bottom-padding:2em;">
<div class="fh5co-intro-cover text-center animate-box" data-stellar-background-ratio="0.5" style="background:#996300;">
<a href="#" class="btn">Our company will provide your business with the professional attention it deserves.</a>
</div>
</div>
<div class="g-signin2" data-onsuccess="onSignIn"></div>
<footer>
<div id="footer">
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3 text-center">
<p>Copyright 2017 TEKVENUE <a href="#" style="color:orange;">TEKVENUE</a>. All Rights Reserved.</p>
<p class="fh5co-social-icons">
<a href="#"><i class="icon-twitter-with-circle"></i></a>
<a href="#"><i class="icon-facebook-with-circle"></i></a>
<a href="#"><i class="icon-instagram-with-circle"></i></a>
</p>
</div>
</div>
</div>
</div>
</footer>
</div>
<script src="js/jquery.min.js"></script>
<script src="js/jquery.easing.1.3.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/owl.carousel.min.js"></script>
<script src="js/jquery.waypoints.min.js"></script>
<script src="js/jquery.stellar.min.js"></script>
<script src="js/main.js"></script>
</body>
</html>
By following these steps, you will be able to integrate Google sign-in functionality into your application.
Click here to create a Pull Request with the proposed solution
Files used for this task:
We are an Information Technology company focused on IT staffing and software project development.
Tekvenue is a global leader when considering the best means of support of your business’s processes, data analytics, reporting systems, and information. Tying together the vast interconnected computing systems in today’s corporations is a challenging task even in the best of times. Ensuring that all systems involved in the process flow can “talk” to each other and keeping all departments, business units, and customers informed is part of Tekvenue’s commitment to Enterprise System Management (ESM).
By leveraging the best minds in the fields of ESM, Tekvenue is uniquely positioned to structure your IT processes to maximize your return on investment.
Learn MoreOur vision is to become a leading technology company with strong focus on customer relationships, coherence with partners, and employee creativity.
We serve clients in critical, technology-dependent industries such as health care, telecom, banking, finance, manufacturing, petroleum, retail, and beyond. We deploy our renowned staffing and IT solutions in the United States, India, and Canada. Contact us today and let us apply our years of experience in IT and business development to your technology needs.
We love new projects! Let's get started.
Learn More
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