0

I'm making a simple social media app. Now I'm trying to provide security while registering users. I'd like something like. By the way, JUST code for when things go wrong, like if username exists in db, not when things go right for example: Auth.auth().crateUser()

if username already exists in db {
    (dont let user register.)
} else {
    (let user register)
}

if email is not valid {
    (dont let user register.)
} else {
   (let user register)
}

Something like that. I'm thinking maybe using alerts, etc.

Any answer is VERY appreciated...

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

1

You can tell Firebase Authentication to only allow a single user with each specific email address to register in your project. You do this by enabling the One account per email address setting in the authentication providers panel in the Firebase console.

If you mean with email validation that you want to ensure the user actually has access to the mail address that they enter, that is called email verification in Firebase. Firebase doesn't require that the user verifies their email address before they can sign in. But if your application requires that, you can tell Firebase to send a verification email from within the app. Then when the user clicks the link in that email, a property is set on their account that you can then check in your code (or in the server-side security rules if you're using the Realtime Database, Cloud Firestore, or Cloud Storage). For more on this flow, see the blog post Email Verification in Firebase Auth.

An alternative to this flow is to use Firebase's newer Email link provider. This prevents the need for the user to enter a password, instead using an email with a link for them to sign in. This automatically then also sets their email address to verified.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Nice!!! I'm doing the email thing and its working great! But, what could I do for no duplicate usernames? Or is that something that I have to handle with (like writing code to check for username fields) instead of firebase... –  Nov 14 '19 at 09:40
  • Firebase Authentication has no built-in support for a unique username. The typical approach involves storing user names in an external database (like the Realtime Database or Firestore) and ensuring uniqueness there. For (many) examples of this, see https://stackoverflow.com/search?q=%5Bfirebase-authentication%5D+unique+username (this is one of [my own answers](https://stackoverflow.com/a/35244705/209103) from that list). – Frank van Puffelen Nov 14 '19 at 14:40
  • Thanks! Your answer is very much appreciated –  Nov 16 '19 at 11:49