Created
August 8, 2019 20:42
-
-
Save alan89/4b896658635142b870a66c201cad67a9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 functions = require('firebase-functions'); | |
const admin = require('firebase-admin'); | |
const axios = require('axios'); | |
admin.initializeApp(); | |
async function adminAuthEmailRequest(request) { | |
var token = await admin.apps[0].INTERNAL.getToken(); | |
var requestCopy = JSON.parse(JSON.stringify(request)); | |
requestCopy.headers = requestCopy.headers || {}; | |
var authHeader = 'Authorization'; | |
requestCopy.headers[authHeader] = 'Bearer ' + token.accessToken; | |
console.log(JSON.stringify(request)); | |
return axios(requestCopy); | |
} | |
async function initialEmailLookup(email) { | |
var token = await admin.apps[0].INTERNAL.idToken; | |
const request = { | |
url: 'https://www.googleapis.com/identitytoolkit/v3/relyingparty/getAccountInfo', | |
data: { | |
idToken: token, | |
initial_email: email, | |
}, | |
headers: { | |
'Content-Type': 'application/json', | |
}, | |
method: 'POST', | |
}; | |
const response = await adminAuthEmailRequest(request); | |
return response; | |
} | |
exports.checkInitialEmail = functions.https.onRequest((req,res) => { | |
initialEmailLookup(req.body.email) | |
.then(data => { | |
console.log(data.data); | |
res.status(200).send(data.data); | |
}) | |
.catch(err => { | |
console.log('ERROR:::::', err); | |
res.status(400).send(err); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment