userflow
https://cloudfunctions.net/createUser
로 라우팅되어 cloud functions 실행success: true
returnconst twilio = require('./twilio');
const admin = require('firebase-admin');
module.exports = function (req,res) {
if(!req.body.phone) {
return res.status(422).send({ error: '핸드폰 번호를 입력해주세요.' });
}
// 3. phone number sanitizing
const phone = String(req.body.phone).replace(/[^\d]/g,'');
// 4. create user info and get immediately
admin.auth().createUser({ uid: phone })
.then(() => admin.auth().getUser(phone))
.then(() => {
// 5. create code
const code = Math.floor(Math.random() * 8999 + 1000);
// 6. send message using twilio
twilio.messages.create({
body: '코드는 ' + code + '입니다.',
to: '+82' + phone.substr(1),
from: '+19382008436'
})
.then(() => {
// 7.store data
const ref = admin.firestore().collection('users');
ref.doc(phone).set({ code: code, codeValid: true },{ merge:true });
// ref.create({ code : code, codeValid: true})
})
.then(() => {
// 8. return success
res.send({ success: true })
})
})
.catch((err) => {
res.status(500).send({ error: err});
})
}