2017-08-11 13 views
1

トークンを使用してFirebaseのカスタム認証方法を理解しようとしていますが、ドキュメントがすべて私にはわかりません。Firebase:REST経由でトークンを取得し、signInWithCustomTokenを使用する

1)まずバッシュでは、::私はJWTジェネレータで遊んでみました前に、私はjavascriptを経由してWebクライアントにログインするための認証応答トークンを使用する方法があるかどうかを確認したかった

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)私は検証のためにトークンの使用を試すためにこれらのそれぞれを試してみました:

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

私は明らかに間違っていますが、ほとんどのドキュメント:

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)それぞれについて、私はこれと同じエラーが出ますカスタムJWTトークンの作成について説明しました。私はまだ分かりません。

+0

あなたの正確な質問は何ですか? –

+0

私はREST APIを使ってfirebase.auth()でユーザーを認証するためにJavaScriptで使用できるトークンを生成したいと考えていました。signInWithCustomToken(トークン) – orellabac

答えて

1

signInWithCustomTokenにする場合は、秘密鍵で署名されたJWTを作成する必要があります。 Firebase管理SDKがその機能を提供: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

関連する問題