1

I would like to do an app, where you can only register with an specific random unique token that I gave the people. So I gave them a token, for example: 8743-2342-6587 and they can go in the app on the register page, type in this token and also type in email and password like a basic authentication.

What's the best way to do this? Maybe I should do in Firestore a Collection of tokens that are free. On Registration I check if the token exists in that collection, if yes than set it to "not free" which could be a boolean value. And the user registers... Or is there a better way to do such things like that?

Fabian
  • 78
  • 9

1 Answers1

1

One way to do this would be to store all these tokens in a Firestore collections and when users enters a code, verify it in a Cloud function and then creating an account using Admin SDK. Then you can sign in user with client SDK. The flow would be like:

  1. User enters credentials to registration along with the token (if any)
  2. These credentials are sent to a Cloud Function which then creates user in Firebase Authentication with provided credentials
  3. If user had also provided any token, you can check if that exists in Firestore and then set the required boolean value either in a database or custom claims.

For example:

exports.registerUser = functions.https.onCall((data, context) => {
  const { email, password, token } = data; 

  const { user } = await admin.auth().createUser({email, password})

  // const token = {check if token exists in Firestore}

  if (token) {
    // set custom claims
  }
});

You can also just create an account first and then check if token exists directly without a Cloud function but that'll work only if the token is associated with an email so only that user can check it's existence (unless anyone is allowed to check it).

If a user must provided a token to create an account at first place, then I'd recommend using Cloud functions. You can also disable sign ups from Firebase Client SDKs if needed (requires Cloud Identity API).

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
  • Sounds really good. Thank you! One further Question, do you know how I can create a lot of unique tokens and save them in a Firestore Collection? The important point is, that every token is only allowed to exist one time. No duplicate token. – Fabian Jan 01 '22 at 15:35
  • @Fabian you can just keep adding more and more documents with `add()` method which will generate a random ID for you using a loop. You can write JS for it and run it locally using emulators using production DB. – Dharmaraj Jan 01 '22 at 15:37
  • The point is, that every token works as a license. So someone can buy 500 licenses and then in that moment I have to create 500 tokens that are not the same as the existing token and save them in the Firestore Collection "token" – Fabian Jan 01 '22 at 15:38
  • @Fabian I'm not sure how this `createTokens()` function will be triggered. But all you have to do is create multiple documents at once. Checkout [this gist](https://gist.github.com/DharmarajX24/63aba00ce9d011c267a952e51d6b14cb) for reference. – Dharmaraj Jan 01 '22 at 15:43
  • Thanks for that example! Looks good. One last Question, how is the db variable defined in your example? – Fabian Jan 01 '22 at 15:55
  • @Fabian `db` is Firestore instance. The code in the gist is using Firebase Web SDK (v9+). I've updated the gist explaining the same and you can also check [Firestore's documentation](https://firebase.google.com/docs/firestore/manage-data/add-data). – Dharmaraj Jan 01 '22 at 15:58