简体   繁体   English

如何将Google OAuth2 ID令牌与node.js / passport.js结合使用,以验证用户是否有效并经过身份验证?

[英]How can I use the Google OAuth2 ID token with node.js / passport.js to verify if the user is valid and authenticated?

On my front end, I'm using vue.js (not that that matters) and with the Google OAuth flow, I get back an id_token from: 在前端,我正在使用vue.jsvue.js ),并通过Google OAuth流,从以下位置获取了id_token

let googleAuthIdToken = await this.auth2.currentUser
            .get()
            .getAuthResponse().id_token;

I want to then pass that token to my node.js server (Express / Passport) to verify that the user is allowed to login. 然后,我想将该令牌传递给我的node.js服务器(Express / Passport),以验证是否允许用户登录。

I want to use passport and send back a JWT to the front end in my response. 我想使用护照,并在回信中将JWT发送回前端。

Can someone please guide me as to how to accomplish this? 有人可以指导我如何做到这一点吗?

It is easier to make use of a node module called googleapis , After installation, import the module. 使用名为googleapis的节点模块会更容易,安装后,导入该模块。

import { google } from 'googleapis';

Then you need to create an OAuthClient by specifying the CLIENT_ID, CLIENT_SECRET, REDIRECT_URL . 然后,您需要通过指定CLIENT_ID, CLIENT_SECRET, REDIRECT_URL来创建OAuthClient。

const oauth2Client = new google.auth.OAuth2(
    CLIENT_ID,
    CLIENT_SECRET,
    REDIRECT_URL,
);

Then you can get the token from google by using the oauth2Client . 然后,您可以使用oauth2Client从Google获得令牌。

const {tokens} = await oauth2Client.getToken(code);
oauth2Client.setCredentials(tokens);

Inorder to obtain the neccessary user information to store in your own database, You need to call this method. 为了获取必要的用户信息以存储在您自己的数据库中,您需要调用此方法。

const plus = google.plus({ version: 'v1', oauth2Client });
const me = await plus.people.get({ userId: 'me' });

me will contain the user information that you are looking for, once you obtain the user information you can then store it using passport js. me将包含您要查找的用户信息,一旦获得用户信息,便可以使用护照js进行存储。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 node.js 和 passport.js 将经过 Google 身份验证的用户保存到 mysql 数据库中 - Save google authenticated user into mysql database with node.js and passport.js 如何同时使用承载和 oauth2 passport.js 策略? - How to use bearer and oauth2 passport.js strategies togheter? 如何在 Google api 中使用 Passport.js Google OAuth2 策略? - How to use Passport.js Google OAuth2 strategy with Google api? 令牌错误:错误请求; 谷歌 OAuth2; Node.js 上的 Passport.js; 能够使用 console.log 数据,但是会出现错误 - TokenError: Bad Request; Google OAuth2; Passport.js on Node.js; Able to console.log data, however delivers error 如何在Node中使用Passport.js模拟另一个用户? - How can I impersonate another user with Passport.js in Node? req.user 是否保证是有效的、经过身份验证的用户? [passport.js] - Is req.user guaranteed to be a valid, authenticated user? [passport.js] 令牌未通过OAuth2 Google和Passport.js传递给客户端 - Token is not being passed to client side with OAuth2 Google and Passport.js 如何使用护照在我的node.js服务器上验证android应用的GPGS令牌 - How can I verify android app's GPGS token on my node.js server using passport 如何解决node.JS护照oauth2 cors错误 - How to solve node.JS passport oauth2 cors error node.js什么是password.js中的用户 - node.js what is User in passport.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM