Created
May 24, 2017 09:04
-
-
Save WilliamAnputra/3c57230416769e51b4df66baa9f8dd35 to your computer and use it in GitHub Desktop.
Verify one time password request error ?
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 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