在 Node.js 中使用 Passport 进行 Google 身份验证
以下方法介绍了如何使用 nodeJs 中的护照向 google 进行身份验证。身份验证基本上是在授予用户访问网站或服务之前对用户的验证。使用 Google 帐户完成的身份验证称为 Google 身份验证。我们可以使用 Google 在其开发人员门户上提供的 OAuth API 进行 Google 身份验证。
创建节点项目并安装模块:
第 1 步:使用以下命令创建 Node 项目。
npm init
按住回车键并在终点行相应地输入“是/否”。
第 2 步:安装所需的模块。
npm install express passport passport-google-oauth2 cookie-session
第三步:创建两个文件index.js和passport.js
项目结构:
第 4 步:创建基本服务器。
index.js
const express = require('express');
const app = express();
app.get('/' , (req , res) => {
res.send("GeeksForGeeks
");
});
app.listen(4000 , () => {
console.log("Server running on port 4000");
});
passport.js
const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth2').Strategy;
passport.serializeUser((user , done) => {
done(null , user);
})
passport.deserializeUser(function(user, done) {
done(null, user);
});
passport.use(new GoogleStrategy({
clientID:"YOUR ID", // Your Credentails here.
clientSecret:"YOUR SECRET", // Your Credentails here.
callbackURL:"http://localhost:4000/auth/callback",
passReqToCallback:true
},
function(request, accessToken, refreshToken, profile, done) {
return done(null, profile);
}
));
index.js
const express = require('express');
const app = express();
const passport = require('passport');
const cookieSession = require('cookie-session');
require('./passport');
app.use(cookieSession({
name: 'google-auth-session',
keys: ['key1', 'key2']
}));
app.use(passport.initialize());
app.use(passport.session());
app.get('/', (req, res) => {
res.send("")
});
// Auth
app.get('/auth' , passport.authenticate('google', { scope:
[ 'email', 'profile' ]
}));
// Auth Callback
app.get( '/auth/callback',
passport.authenticate( 'google', {
successRedirect: '/auth/callback/success',
failureRedirect: '/auth/callback/failure'
}));
// Success
app.get('/auth/callback/success' , (req , res) => {
if(!req.user)
res.redirect('/auth/callback/failure');
res.send("Welcome " + req.user.email);
});
// failure
app.get('/auth/callback/failure' , (req , res) => {
res.send("Error");
})
app.listen(4000 , () => {
console.log("Server Running on port 4000");
});
输出:
第 5 步:现在转到 google 平台并生成您的凭据。
第 6 步:创建登录功能。
护照.js
const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth2').Strategy;
passport.serializeUser((user , done) => {
done(null , user);
})
passport.deserializeUser(function(user, done) {
done(null, user);
});
passport.use(new GoogleStrategy({
clientID:"YOUR ID", // Your Credentails here.
clientSecret:"YOUR SECRET", // Your Credentails here.
callbackURL:"http://localhost:4000/auth/callback",
passReqToCallback:true
},
function(request, accessToken, refreshToken, profile, done) {
return done(null, profile);
}
));
index.js
const express = require('express');
const app = express();
const passport = require('passport');
const cookieSession = require('cookie-session');
require('./passport');
app.use(cookieSession({
name: 'google-auth-session',
keys: ['key1', 'key2']
}));
app.use(passport.initialize());
app.use(passport.session());
app.get('/', (req, res) => {
res.send("")
});
// Auth
app.get('/auth' , passport.authenticate('google', { scope:
[ 'email', 'profile' ]
}));
// Auth Callback
app.get( '/auth/callback',
passport.authenticate( 'google', {
successRedirect: '/auth/callback/success',
failureRedirect: '/auth/callback/failure'
}));
// Success
app.get('/auth/callback/success' , (req , res) => {
if(!req.user)
res.redirect('/auth/callback/failure');
res.send("Welcome " + req.user.email);
});
// failure
app.get('/auth/callback/failure' , (req , res) => {
res.send("Error");
})
app.listen(4000 , () => {
console.log("Server Running on port 4000");
});
运行应用程序的步骤:从项目的根目录使用以下命令运行应用程序:
node index.js
输出: