kpg
kpg

Reputation: 7966

How to use Firebase email address verification

There appears to be an option to confirm email address in Firebase Web SDK3, see here (put your own project id in the link), but I can't find documentation for how to use it.

Anybody done it?

Upvotes: 2

Views: 5006

Answers (2)

KhoPhi
KhoPhi

Reputation: 9517

In short, below is basically how you'll approach this, in AngularJS:

// thecontroller.js
$scope.sendVerifyEmail = function() {
    console.log('Email sent, whaaaaam!');
    currentAuth.sendEmailVerification();
  }

// where currentAuth came from something like this:
// routerconfig

....
templateUrl: 'bla.html',
resolve: {
    currentAuth:['Auth', function(Auth) {
      return Auth.$requireSignIn() // this throws an AUTH_REQUIRED broadcast
    }]
  }
...

// intercept the broadcast like so if you want:

....

$rootScope.$on("$stateChangeError", function(event, toState, toParams, fromState, fromParams, error) {
      if (error === "AUTH_REQUIRED") {
        $state.go('login', { toWhere: toState });
       }
    });
....

// So user receives the email. How do you process the `oobCode` that returns?
// You may do something like this:

// catch the url with its mode and oobCode
.state('emailVerify', {
  url: '/verify-email?mode&oobCode',
  templateUrl: 'auth/verify-email.html',
  controller: 'emailVerifyController',
  resolve: {
    currentAuth:['Auth', function(Auth) {
      return Auth.$requireSignIn()
    }]
  }
})

// Then digest like so where each term is what they sound like:

.controller('emailVerifyController', ['$scope', '$stateParams', 'currentAuth', 'DatabaseRef',
  function($scope, $stateParams, currentAuth, DatabaseRef) {
    console.log(currentAuth);
    $scope.doVerify = function() {
      firebase.auth()
        .applyActionCode($stateParams.oobCode)
        .then(function(data) {
          // change emailVerified for logged in User
          console.log('Verification happened');
        })
        .catch(function(error) {
          $scope.error = error.message;
          console.log(error.message, error.reason)
        })
    };
  }
])

Explained more in the article Email Verification with Firebase 3.0 SDK

Upvotes: 0

Sam Quayle
Sam Quayle

Reputation: 537

Pretty well hidden but here you go: https://firebase.google.com/docs/reference/js/firebase.User#sendEmailVerification

Then you'll just need to check for emailVerified as part of your authentication flow.

Upvotes: 3

Related Questions