2017-08-11 78 views
1

我想了解使用令牌的Firebase自定义auth方法,但文档对我来说并不是那么清楚。之前,我试图与智威汤逊发电机玩,我想检查是否有使用的身份验证响应令牌通过JavaScript登录web客户端的方式:Firebase:通过REST获取令牌并使用signInWithCustomToken

1)首先,在击:

curl 'https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key=[theapikey]' -H 'Content-Type: application/json' --data-binary '{"email":"[the-user-email]","password":"[the-user-valid-password]","returnSecureToken":true}' 

2)响应:

{ 
"kind": "identitytoolkit#VerifyPasswordResponse", 
"localId": "<theresponse-localid>", 
"email": "<theresponse-user-email>", 
"displayName": "<theresponse-displayname>", 
"idToken": "<theresponse-idtoken-string>", 
"registered": true, 
"refreshToken": "<theresponse-refreshToken>", 
"expiresIn": "3600" 
} 

3)因此,我的结果复制到一个JavaScript对象:

var token = { 
"kind": "identitytoolkit#VerifyPasswordResponse", 
"localId": "<localid-string>", 
"email": "<user-email>", 
"displayName": "<user-displayname>", 
"idToken": "<idtoken-string>", 
"registered": true, 
"refreshToken": "<refreshtoken-string>", 
"expiresIn": "3600" 
}; 

4)我已经尝试了每一种尝试使用该令牌进行验证:

firebase.auth().signInWithCustomToken(token).catch(function(error) { 
    console.log("firebase.auth().signInWithCustomToken() Error: "); 
    console.log(error); 
} 

firebase.auth().signInWithCustomToken(JSON.stringify(token)).catch(function(error) { 
    console.log("firebase.auth().signInWithCustomToken() Error: "); 
    console.log(error); 
} 

firebase.auth().signInWithCustomToken(token.idToken).catch(function(error) { 
    console.log("firebase.auth().signInWithCustomToken() Error: "); 
    console.log(error); 
} 

5)对于每一个,我得到同样的错误:

"Object { code: "auth/invalid-custom-token", message: "The custom token format is incorrect", stack: "" }" 

显然我错了,但大多数文档我阅读描述创建自定义JWT令牌。恐怕我还不明白。

+0

什么是您的具体问题? –

+0

我希望能够使用REST api来生成一个令牌,我可以在JavaScript中使用它来使用firebase.auth()来验证用户。signInWithCustomToken(令牌) – orellabac

回答

1

如果你想signInWithCustomToken,你需要创建一个用你的私钥签名的JWT。在火力地堡管理软件开发工具包提供该功能:https://firebase.google.com/docs/auth/admin/create-custom-tokens

这将您的服务器上运行,然后您发送自定义的令牌给客户端并登录使用:

firebase.auth().signInWithCustomToken(token).catch(function(error) { 
    console.log("firebase.auth().signInWithCustomToken() Error: "); 
    console.log(error); 
}); 
+0

谢谢Bojeil,你是对的。我设法编写了一个JWT类来生成一个令牌,并且我可以在JavaScript中使用它。我用这篇文章作为参考:https://firebase.google.com/docs/auth/admin/create-custom-tokens#create_custom_tokens_using_a_third-party_jwt_library – orellabac

+0

很酷,很高兴我能够提供帮助。 – bojeil

相关问题