Skip to content

Instantly share code, notes, and snippets.

@WilliamAnputra
Created May 24, 2017 09:04
Show Gist options
  • Save WilliamAnputra/3c57230416769e51b4df66baa9f8dd35 to your computer and use it in GitHub Desktop.
Save WilliamAnputra/3c57230416769e51b4df66baa9f8dd35 to your computer and use it in GitHub Desktop.
Verify one time password request error ?
const admin = require('firebase-admin');
module.exports = (req, res) => {
if (!req.body.phone || !req.body.code) {
res.status(422).send({ error: 'must supply phone and code' });
}
const phone = String(req.body.phone).replace(/[^\d]/g, '');
const code = parseInt(req.body.code, 10);
//validate user by referring to firebase database
admin.auth().getUser(phone)
.then(() => {
const ref = admin.database().ref(`users/${phone}`);
ref.on('value', snapshot => {
ref.off();
const currentUser = snapshot.val();
if (currentUser.code !== code || !currentUser.codeValid) {
return res.status(422).send({ error: 'code is not valid' });
}
ref.update({ codeValid: false });
admin.auth().createCustomToken(phone)
.then(token => res.send({ token }));
});
})
.catch((error) => console.log(error));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment