Reputation: 79
I am a beginner. I am trying to send via AJAX from the logged in user's state the token that was generated. However, when I run the below code, I get internal server error, so something is off. What am I missing?
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')
})
})
Upvotes: 1
Views: 512
Reputation: 901
There are two problems in your code
for server side you should get the value from request body
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)
})
})
and client side you should send it correctly
$.ajax(
{
url: '/auth',
type: 'POST',
data: { idToken : idToken },
success: function (response){
console.log('sent successfully')
console.log(uid)}
})
Upvotes: 2