Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send unique registration links with firebase?

I want to build an application where users just can register with "Invitation". Users already with account can send invitations to the specified e-mail addresses, then the app send an e-mail with a link to the registration where there's a token and just the specified e-mail address can be used.

So first, how to an "automated" e-mail from firebase, secondly, how to generate an unique registration token to the URL?

like image 868
Gergő Horváth Avatar asked Nov 09 '18 11:11

Gergő Horváth


2 Answers

For sending email from Firebase, you can use a Cloud Function. There is an official sample on this topic: https://github.com/firebase/functions-samples/tree/Node-8/email-confirmation

As said by Martin Zeitler you can use the push() method to generate a unique token and to create a record with the corresponding email. Then when the new user tries to register, you could check that his email corresponds to the token before registering him. You could do that via different ways: with Cloud Functions again, e.g. with an HTTPS Cloud Function (see https://firebase.google.com/docs/functions/http-events) or by creating a record in the database that triggers a Cloud Function (see https://firebase.google.com/docs/functions/database-events). In both cases, you would use the Admin SDK to register/create the user, see https://firebase.google.com/docs/reference/admin/node/admin.auth.Auth and https://firebase.google.com/docs/auth/admin/manage-users#create_a_user

Update following your comment

For example, you could have a form where you ask the invited user to enter his email and the unique token he has received (you could open and pre-fill this form through the link sent in the email). When this form is submitted it creates a node in the Realtime Database and this node creation would trigger a Cloud Function that:

  • Firstly, would check if the token and the email correspond (and probably check that they were not used before) and;
  • Secondly, if the previous check is ok, would register (i.e. create) the user to your Firebase app.

Concretely, let's imagine that upon form submission we create the following node:

- registrationRequests
    -UID
       -email: .....
       -token: .....

you could have a Cloud Function along the following lines:

exports.createInvitedUser = functions.database.ref('/registrationRequests/{requestId}')
    .onCreate((snap, context) => {
      const createdData = snap.val();
      const email = createdData.email;
      const token= createdData.token;

      //First action, verify email and token by reading the node of the database where you initially stored the email/token

     //Second action, register the user by using admin.auth().createUser({})
     //See https://firebase.google.com/docs/auth/admin/manage-users#create_a_user

    })
like image 99
Renaud Tarnec Avatar answered Oct 20 '22 09:10

Renaud Tarnec


think this might work with Firebase Invites .setDeepLink().

where the deep-link would have to deliver the custom token.

Sets the link into your app that is sent with invitations. Specify this to share specific content with the recipient or to otherwise present a custom experience when a user opens your app from an invitation.

only can provide a Java example:

public class DeepLinkActivity extends AppCompatActivity {

    @Override
    public void onResume() {
        super.onResume();
        Intent intent = getIntent();
        Uri uri = intent.getData();
        String token = uri.getQueryParameter("inviteToken");
        ...
    }
}

or with .getDynamicLink() method.

to create unique token, use .push(), which then also permits later use of .hasKey().

not certain if Invites is even available for Web JavaScript.

like image 43
Martin Zeitler Avatar answered Oct 20 '22 08:10

Martin Zeitler