How to use Firebase Authentication with multiple login methods and role-based authentication?
P粉574695215
2023-09-05 12:18:10
<p>I am creating a react.js application with nodejs as backend and I want to use these authentication methods to log in using email/password, login using google and login using phone, now I want to keep the user while signing Uniqueness For any of these I will also get other details like when user registers using email/password then I will also get user phone number and I don't want user to use that phone number by logging in using phone Create another account method. </p>
<p>The second problem is that my application has 3 user roles: User, Sales, and Administrator. There is a different registration form for each user, when a user using email registers as a salesperson, I also want that user to be able to register as a user using the same email, now when I use firebase sdk in my React application, It will throw an error to the user as it already exists. </p>
<p>Firebase Authentication can solve this problem, I want the user to be unique, and I want role-based authentication to be enabled.
I use firestore as my database</p>
As doc, "You can allow users to log in to your app using multiple authentication providers by linking the authentication provider credentials to existing user accounts. Regardless of authentication, Users are identified by the same Firebase user ID as the provider they used to log in." See also the
linkWithCredential()
的文档a> andlinkWithPhoneNumber()
methods.You can use custom claims based on (you can add multiple claims, one per role) or Firestore documentation . However, you will implement the mechanism for user selection of roles (you mentioned "different registration forms" in your question) and the corresponding GUI display yourself in your application.