2017-07-25 69 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發佈的令牌? –