How to use Firebase email address verification

前端 未结 2 1355
长发绾君心
长发绾君心 2021-01-15 06:29

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 i

相关标签:
2条回答
  • 2021-01-15 07:21

    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.

    0 讨论(0)
  • 2021-01-15 07:27

    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

    0 讨论(0)
提交回复
热议问题