I am trying to create an API for my Firebase project using functions. The difficult part is that I am using TypeScript and I keep running into typing problems.
This is my sign up route:
app.post('/signup', (req: Request, res: Response) =>{
const newUser = {
email: req.body.email,
password: req.body.password,
confirmPassword: req.body.confirmPassword,
handle: req.body.handle
}
// TODO: validate data
db.doc(`/users/${newUser.handle}`).get()
.then( doc => {
// Duplicated handle
if(doc.exists){
return res.status(400).json({ handle: 'this handle is already taken'});
}
// Valid handle
else {
return firebase.auth().createUserWithEmailAndPassword(newUser.email, newUser.password);
}
})
.then( data => {
return data.user.getIdToken();
})
.then( token => {
return res.status(201).json({ token });
})
.catch( err => {
console.error(err);
return res.status(500).json({ error: err.code })
});
})
Errors:
- The doc in the first has the following error
Argument of type '(doc: DocumentSnapshot) => Response | Promise' is not assignable to parameter of type '(value: DocumentSnapshot) => UserCredential | PromiseLike'.
This is because I am returning a response status if the handle already exists, to avoid duplicates. From my understanding, this would go to the catch; and the valid handle would return a Promise that the next then would take. However, this is not working
- data.user.getIdToken() says that
Property 'user' does not exist on type 'DocumentSnapshot'. Tried delcaring a const for user before using it, but get the same message.
I have other functions for post and get from firestore working, but can't get the authentication ones to work.
Thanks for the help!