Created
February 10, 2020 21:17
-
-
Save torresalmonte/809be93e00e71423a26615a3e483c706 to your computer and use it in GitHub Desktop.
Testing Firebase Auth REST API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const https = require('https'); | |
const API_KEY = "YOUR_API_KEY"; | |
const email = "YOUR_EMAIL@EXAMPLE.COM"; | |
const pass = "YOUR_SECRET_PASSWORD"; | |
const signInUrl = `https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=${API_KEY}` | |
// login data; this will be sent on the POST request | |
// NOTE: the parameter 'returnSecureToken' must be true to get the idToken | |
const postData = { | |
email: email, | |
password: pass, | |
returnSecureToken: true | |
}; | |
// request parameter | |
const options = { | |
hostname: "identitytoolkit.googleapis.com", | |
port: 443, | |
path: `/v1/accounts:signInWithPassword?key=${API_KEY}`, | |
method: "POST", | |
headers: { | |
"Content-Type": "application/json" | |
} | |
}; | |
// sending the request | |
let req = https.request(options, res => { | |
console.log(); | |
// this is the response from the request; this is done 'asynchronously' | |
res.on("data", data => { | |
responseData = JSON.parse(data.toString()); | |
// if the login works, this is the idToken; | |
// you can echo the full 'responseData' to see what is returned | |
console.log(responseData.idToken); | |
}); | |
}); | |
req.on("error", err => { | |
console.error(err.message); | |
}); | |
// writing the POST data | |
req.write(Buffer.from(JSON.stringify(postData), 'utf8'), err => { | |
if (err) { | |
console.error(err.message); | |
} | |
}); | |
// finishing the request | |
req.end(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment