Mete Kabak
Mete Kabak

Reputation: 332

How to set identifier on firebase authentication via custom token?

I just implemented the linkedin signup & login using firebase custom auth system through this https://firebase.google.com/docs/auth/admin/create-custom-tokens

Identifier is -

It`s working but identifier on firebase is null.

How should I send it? Should I update it after creating the user? I want to save it on create.

Thanks

Upvotes: 11

Views: 3892

Answers (2)

bojeil
bojeil

Reputation: 30808

Try this: On your server, before minting the custom token, you can create the user with the email:

// Create the user with email first.
admin.auth().createUser({uid: uid, email: linkedinEmail})
  .then(function(userRecord) {
    // This will return custom token for that user above.
    return admin.auth().createCustomToken(userRecord.uid);
  })
  .catch(function(error) {
    // Some error.
  });

Another option using client side code, is to set the email client side after signing in with custom token:

firebase.auth().signInWithCustomToken(customToken)
  .then(function(result) {
    return firebase.auth().currentUser.updateEmail(linkedinEmail);
  })
  .catch(function(error) {
    // Some error occurred.
  });

Upvotes: 17

Harkal
Harkal

Reputation: 1828

while creating custom token generate a unique UID at your own and save it in database and as when there is someone trying o login with details match the credentials in database and fetch the correct UID and create a custom token with it. now with the help of custom token you can login

have a look at the code below this is a well working code from my node.js project

const functions = require('firebase-functions');

const admin = require('firebase-admin');

module.exports = functions.https.onRequest((req, res) => {

//make a random and distinct uid
//and save it in database with the users credentials
//match them at the time of login

admin.auth().createCustomToken(uid)
  .then(function(customToken) {
      res.setHeader('Content-Type', 'application/json');
      var error = false;
      var result = {
          "Error": error,
          "CustomToken": customToken
      };
      res.send(JSON.stringify(result));
  })
  .catch(function(err) {
      res.setHeader('Content-Type', 'application/json');
      var error = true;
      var result = {
          "Error": error,
          "Message": err
      };
      res.send(JSON.stringify(result));
  });

});

Upvotes: -2

Related Questions