创建帐户后自动登录用户

Automatically logs user in after account creation

我有一个使用 Firebase 身份验证的应用程序,我想知道是否有办法:

  1. 用户创建账户后无需登录

  2. 用户应该在创建帐户后自动登录

这是为了提供更好的用户体验。

Here is the method I am using.

这是我的代码:

$scope.createUser = function(user) {
  if (user && user.email && user.name ) {
    if (user.password === user.confirm ) {
      auth.$createUser({
        name: user.name,
        email: user.email,
        password: user.password,
        confirmPass: user.confirm
      }).then(function (userData) {
        ref.child("users").child(userData.uid).set({
          name: user.name,
          email: user.email,
          displayName: user.name
        });
      }).catch(function (error) {
          console.log(error);
      });
    }
  }
};

这里是签到功能:

$scope.signIn = function (user) {
  if (user && user.email && user.pwdForLogin) {
    auth.$authWithPassword({
      email: user.email,
      password: user.pwdForLogin
    }).then(function (authData) {
      ref.child("users").child(authData.uid).once('value', function (snapshot) {
        var val = snapshot.val();
        $scope.$apply(function () {
          $rootScope.name = val;
          $scope.userDisplayInfoName = $rootScope.name.displayName;
          $scope.userDisplayInfoEmail = $rootScope.name.email;
        });
      });
    }).catch(function (error) {
        console.log(error);
    });
  }
};

下面是一个示例 UserService,它实现了一个 createUser 方法,该方法创建一个新用户,登录该新用户,最后将用户的 auth 对象绑定到 $rootScope.userData

用户服务

userModule.service('UserService', function($firebaseAuth, FBREF) {
  // variable to hold the currentUser's data
  var currentUser;

  // AngularFire authentication service
  var _authObj = $firebaseAuth(FBREF);
  
  self = {
    /**
     * `init` method that checks if a user is authenticated, and if so,
     * binds the user's auth data to `$rootScope.userData`.
     * @returns {Promise<any>} promise that resolves once auth data is bound.
     */
    init: function() {
      const auth = _authObj.$getAuth();
      // return early if there is no auth object.
      if (!auth) {
        return;
      }

      // create User from user factory, and get the user's data from Firebase.
      currentUser = User(auth);

      // return promise to bind `currentUser`'s data to `$rootScope.userData`.
      return currentUser
        .$bindTo($rootScope, 'userData')
        .then(function() {
          console.debug('Bound userData: ', currentUser);
        });
    },

    /**
     * `createUser` method that creates a new user with a email and password, 
     * then logs the user in with the same email and password, and finally, 
     * calls `self.init()` to bind user's auth object to `$rootScope.userData`.
     * @returns {Promise<any>} promise that resolves once the user is created,
     * `self.init` has been called, and the user's auth object is bound to
     * `$rootScope.userData` (i.e. promise from `self.init()` has resolved).
     */
    createUser: function(user, successCallback, errorCallback){
      // logout any currentUser before creating a new user
      self.logout();

      // first, create a new user with the email and password
      return _authObj.$createUser({
        email: user.email,
        password: user.password
      })

      // then, authenticate the new user with the password
      .then(function(userData) {
        return _authObj.$authWithPassword({
          email: user.email,
          password: user.password
        });
      })
      
      // finally, run any required initialization steps
      .then(function(authData) {
        self.init();
        if (successCallback && typeof successCallback === 'function') {
          successCallback();
        }
      })
      
      // catch an error if one is thrown
      .catch(function(error) {
        console.error(error);
        if (errorCallback && typeof successCallback === 'function') {
          errorCallback(error);
        }
      });
    }
  };

  return self;
});

控制器

控制器实现看起来像这样:

userModule.controller('UserCtrl', function($scope, $state, UserService) {
  var handleSuccess = function() {
    $state.go('stateAfterSuccessfulLogin');
  };

  var handleError = function(error) {
    console.error(error);
  };

  // FIXME: simple way to check inputs have values
  var isValid = function() {
    return $scope.incomingUser.email && $scope.incomingUser.password;
  };

  $scope.signupWithPassword = function() {
    if (!isValid()) {
      return handleError('Missing inputs');
    }

    UserService.createUser($scope.incomingUser, handleSuccess, handleError);
  };
});

资源