我具有在我的Express API中注册用户的功能。请查看注册路线:
//Route to SIGN UP
app.post('/signup', (req, res) => {
const newUser = {
email : req.body.email,
password : req.body.password,
confirmPassword: req.body.confirmPassword,
handle: req.body.handle
}
let errors = {};
if(isEmpty(newUser.email)) errors.email = 'Must not be empty';
else if (!isEmail(newUser.email)) errors.email = 'Must be a valid email address';
if(isEmpty(newUser.password)) errors.password = 'Must not be empty';
if(newUser.password !== newUser.confirmPassword) errors.confirmPassword = 'Passwords must match';
if(isEmpty(newUser.handle)) errors.handle = 'Must not be empty';
if(Object.keys(errors).length > 0) return res.status(400).json(errors);
let token, userId;
db.doc(`/users/${newUser.handle}`).get()
.then(doc => {
if(!doc.exists) {
return firebase.auth().createUserWithEmailAndPassword(newUser.email, newUser.password)
} else {
return res.status(400).json({ handle: 'this handle already exists'})
}
})
.then(data => {
userId = data.user.uid;
return data.user.getIdToken()
})
.then(idToken => {
token = idToken;
const userCredential = {
handle: newUser.handle,
email: newUser.email,
createdAt: new Date().toISOString(),
userId
}
db.doc(`/users/${newUser.handle}`).set(userCredential);
})
.then ( () => {
return res.status(201).json({ token })
})
.catch(err => {
if(err.code === 'auth/email-already-in-use') {
return res.status(400).json({ email: 'Email is already in use'})
} else {
return res.status(500).json(console.log(err));
}
}
)
})
//end of SIGN UP
Postman API不会显示任何内容,如果我在最后的else语句中发送了JSON
条消息,则会显示在邮递员中。
当我运行=> firebase serve时,它显示了Powershell中的错误
i函数:开始执行“ api”
错误:无法加载默认凭据。浏览至https://cloud.google.com/docs/authentication/getting-started了解更多信息。 在GoogleAuth.getApplicationDefaultAsync(D:\ Projects \ socialape \ functions \ node_modules \ google-auth-library \ build \ src \ auth \ googleauth.js:161:19) 在process._tickCallback(内部/进程/next_tick.js:68:7) 我的功能:在大约1秒内完成“ api”