I have a function to signup users in my express API. Please have a look at signup route:
//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 doesn't show anything, if I send some JSON
message in my last else statement, that gets shown up in the postman.
It shows me error in powershell when I run => firebase serve
i functions: Beginning execution of "api"
Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information. at GoogleAuth.getApplicationDefaultAsync (D:\Projects\socialape\functions\node_modules\google-auth-library\build\src\auth\googleauth.js:161:19) at process._tickCallback (internal/process/next_tick.js:68:7) i functions: Finished "api" in ~1s