首页 文章

如何在Firebase中为社交登录创建包装器REST API?

提问于
浏览
0

我正在尝试使用 Cloud 功能为Firebase身份验证创建包装器REST API .

在客户端上使用Facebook Access令牌(使用Facebook SDK)后,如何在Firebase上创建用户或验证用户?

1 回答

  • 2

    如果您使用带有HTTP触发器的Firebase函数,则可以使用firebase.js客户端node.js库对用户进行身份验证并在REST API中返回Firbease令牌 . 您可以将Facebook Access令牌发送到该HTTP endpoints ,使用node.js客户端库使用 signInWithCredential 登录用户,并返回ID令牌和刷新令牌 .

    如果要使用REST API:

    curl 'https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyAssertion?key=[API_KEY]' \
    -H 'Content-Type: application/json' \
    --data-binary '{"postBody":"access_token=[FACEBOOK_ACCESS_TOKEN]&providerId=[facebook.com]","requestUri":"[http://localhost]","returnIdpCredential":true,"returnSecureToken":true}'
    

    这将返回Firebase ID令牌和刷新令牌:

    {
      "idToken": "[ID_TOKEN]",
      "refreshToken": "[REFRESH_TOKEN]",
      ...
    }
    

    这就是Firebase Auth会话所需的全部内容 .

    要构造用户,请使用ID令牌调用以下API:

    curl 'https://www.googleapis.com/identitytoolkit/v3/relyingparty/getAccountInfo?key=[API_KEY]' \
    -H 'Content-Type: application/json' --data-binary '{"idToken":"[FIREBASE_ID_TOKEN]"}'
    

    这将返回用户和相关的数据:

    {
      "kind": "identitytoolkit#GetAccountInfoResponse",
      "users": [
        {
          "localId": "ZY1rJK0...",
          "email": "user@example.com",
          "emailVerified": false,
          "displayName": "John Doe",
          "providerUserInfo": [
            {
              "providerId": "password",
              "displayName": "John Doe",
              "photoUrl": "http://localhost:8080/img1234567890/photo.png",
              "federatedId": "user@example.com",
              "email": "user@example.com",
              "rawId": "user@example.com",
              "screenName": "user@example.com"
            }
          ],
          "photoUrl": "https://lh5.googleusercontent.com/.../photo.jpg",
          "passwordHash": "...",
          "passwordUpdatedAt": 1.484124177E12,
          "validSince": "1484124177",
          "disabled": false,
          "lastLoginAt": "1484628946000",
          "createdAt": "1484124142000",
          "customAuth": false
        }
      ]
    }
    

    要在ID令牌过期后刷新它,请使用返回的刷新令牌:使用REST API:

    curl 'https://securetoken.googleapis.com/v1/token?key=[API_KEY]' \
    -H 'Content-Type: application/x-www-form-urlencoded' \
    --data 'grant_type=refresh_token&refresh_token=[REFRESH_TOKEN]'
    

    这将返回一个新的ID令牌和刷新令牌:

    {
      "expires_in": "3600",
      "token_type": "Bearer",
      "refresh_token": "[REFRESH_TOKEN]",
      "id_token": "[ID_TOKEN]",
      "user_id": "tRcfmLH7o2XrNELi...",
      "project_id": "1234567890"
    }
    

    要在后端使用客户端库: var firebase = require('firebase');

    您将FB访问令牌从客户端发送到HTTP endpoints 并使用它登录:

    var cred = firebase.auth.FacebookAuthProvider.credential(fbAccessToken);
    firebase.auth().signInWithCredential(cred).then(function(user) {
      // User is obtained here.
      // To get refresh token:
      // user.refreshToken
      // To get ID token:
      return user.getIdToken().then(function(idToken) {
        // ...
      })
    }).catch(function(error) {
    });
    

相关问题