I’m using FireStore db and Angular 14. I want to add a custom field (a checkbox isAdmin) for every new user registration.
This is my saveUser method:
saveUser(firstName: any, lastName: any, email: any, password: any) {
return from(
createUserWithEmailAndPassword(this.auth, email, password)
).pipe(
switchMap(({ user }) => updateProfile(user, { displayName: firstName }))
);
How can I add isAdmin with the rest of the fields ?
2
Answers
[EDIT]: I just realised that this is some firebase stuff; I should learn to read properly. I’ll leave it here because I took the effort but it’s not a method of yours at all, it’s a built-in thing with firebase to create users.
Without looking up firebase user creation and management, I imagine the answer will be along the lines that you attach properties to users after their creation, so you need to go through creating them before being able to set anything additional like an isAdmin flag.
Incorrect answer spam follows:
So, from your comment it sounds as though you just need to extend that method?
Assuming you have your checkbox as you say, and it’s saved in some fashion in your component, e.g.
Then when they hit save, that value just needs to be passed in…
And update your method(s) appropriately…
I can’t say what
createUserWithEmailAndPassword
does, but that needs updating to do whatever it does when creating the user:As a side note: I’d suggest refactoring to reduce the number of params down to just the one; create some
UserCreationModel
or something, e.g.Then all those methods look much cleaner:
I’d recommend not having an ‘isAdmin’ property on the user’s profile in Firebase’s Firestore. Use a Firebase cloud function to create what’s called, a user claim. Here is an example of my cloud function. I restrict only an admin to create other admins, of course, remove this part the first time you run it.
This will make your security rules work easier as well.
See here – https://firebase.google.com/docs/auth/admin/custom-claims