2017-07-25 65 views
0

我有一个移动应用程序与服务器进行通信。要在移动应用中进行身份验证,我正在使用Google登录。在返回符号的的accessToken我发送到我的服务器,并确认使用google-auth-library作为这里建议:https://developers.google.com/identity/sign-in/web/backend-auth缓存Google JWT

import GoogleAuth from 'google-auth-library' 
const auth = new GoogleAuth() 
const client = new auth.OAuth2(MyClientId, '', '') 

apiRoutes.use((req, res, next) => { 
    // get the token from the request 
    const token = req.token 

    if (token) { 
     // verify secret with google 
     client.verifyIdToken(token, MyClientId, (err, payload) => 
      // proceed with the user authenticated 
      ... 

是否有必要做这样的判断与每一个用户发出请求?做一些缓存会是一个好习惯吗?或者在我的服务器上拥有自己的JWT实现,其中包含Google负载?

回答

1

不,服务器通常应该在验证access token后为用户创建一个帐户,将Google ID与其他用户详细信息(ID,电子邮件,名称等)一起保存在数据库中,然后将访问令牌返回给移动应用。

一旦后者(通常存储在本地)到期,它可以是refreshed而不会提示用户许可。

+0

为了澄清,当你说“然后返回一个访问令牌”,并且“它可以被刷新”时,你指的是我的服务器发出的令牌,而不是Google发布的令牌? –