Is there any way to do email confirmation for Firebase user creation and/or password reset?

前端 未结 9 2228
故里飘歌
故里飘歌 2020-11-28 04:03

Question says it all. In Firebase, how do I confirm email when a user creates an account, or, for that matter, do password reset via email.

I could ask more broadly:

9条回答
  •  被撕碎了的回忆
    2020-11-28 04:46

    As at 2016 July, you might not have to use the reset link etc. Just use the sendEmailVerification() and applyActionCode functions:

    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)
            })
        };
      }
    ])
    

    And ooh, with the above approach, I do not think there's any need keeping the verification of your user's email in your user data area. The applyActionCode changes the emailVerified to true from false.

    Email verification is important when users sign in with the local account. However, for many social authentications, the incoming emailVerified will be true already.

    Explained more in the article Email Verification with Firebase 3.0 SDK

提交回复
热议问题