2017-05-06 3 views
1

私は初心者です。私はAJAX経由で、ログインしたユーザーの状態から生成されたトークンを送信しようとしています。しかし、以下のコードを実行すると、内部サーバーエラーが発生するため、何かがオフになります。私は何が欠けていますか?AJAXコードに問題があります(Firebaseトークンをサーバーに送信しています)?

app.post('/auth', function(req,res,next){ 
    var idToken = 
    admin.auth().verifyIdToken(idToken) 
    .then(function(decodedToken) { 
    var uid = decodedToken.uid 
    console.log(uid) 
    res.send(uid) 
    }).catch(function(error) { 
    console.log(error.message) 
}) 
}) 

$("#sign-in").on('click', function(event) { 
    event.preventDefault() 
    var email = $("#email").val() 
    var password = $("#password").val() 
    firebaseAUTH.signInWithEmailAndPassword(email, password).then(function (user) { 
    console.log('user has signed in with e-mail address: '+user.email+' and user ID: '+user.uid) 
    //window.location = '/members-area' 
    firebaseAUTH.currentUser.getToken(true).then(function(idToken) { 
    $.ajax(
    { 
     url: '/auth', 
     type: 'POST', 
     data: idToken, 
     success: function (response){ 
     console.log('sent successfully') 
     console.log(uid)} 
    } 
) 
console.log(idToken) 
// Send token to your backend via HTTPS (JWT) 
}).catch(function(error) { 
// Handle error 
console.log(error.message) 
}) 
    }).catch(function(error) { 
    $('#errorbox').html('Error: '+error.message).css('color', 'red') 
    }) 
}) 
+0

それを送信する必要がありますリクエストボディ

app.post('/auth', function(req,res,next){ var idToken = req.body.idToken; admin.auth().verifyIdToken(idToken) .then(function(decodedToken) { var uid = decodedToken.uid console.log(uid) res.send(uid) }).catch(function(error) { console.log(error.message) }) }) 

と、クライアント側から値を取得する必要がありますエラーメッセージとは何ですか? – acdcjunior

+0

500内部エラー。クライアント側がそれをスローします – huzal12

答えて

2

サーバー側のためのあなたのコード内の二つの問題

はあなたが正しく

$.ajax(
    { 
     url: '/auth', 
     type: 'POST', 
     data: { idToken : idToken }, 
     success: function (response){ 
     console.log('sent successfully') 
     console.log(uid)} 
    }) 
+0

私の友人にありがとう – huzal12

+0

上記のコードを試しましたが、 "app"が定義されていないため、エラーが発生しました。 firebaseの初心者です。これを解決するために私を助けてください。 –

関連する問題