我正在尝试使用云函数创建用于firebase身份验证的包装器REST API。
一旦我在客户端上拥有Facebook访问令牌(使用Facebook SDK)?
如果使用http触发器使用firebase函数,则可以使用firebase.js client node.js库来验证用户并返回REST API中的Firbease令牌。您会将Facebook访问令牌发送到该HTTP端点,使用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到期后刷新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端点,然后登录它:
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) {
});