0

我有一个应用程序,允许用户自己注册、登录和添加内容。对于经过身份验证的用户,他们可以访问一些页面,否则会将他们引导到登录页面。但是,我不知道如何让用户在注册后自动登录(转到一个名为工具的页面)。如何将登录信息从注册页面传递到工具页面?

这是我的工具页面和登录页面的路线:

.config(['$routeProvider', function($routeProvider) {

  $routeProvider
    .when('/tools', {
        templateUrl: 'app/shared/nav.html',
        controller: 'NavCtrl',
        resolve: {
             // controller will not be loaded until $requireAuth resolves
            "currentAuth": ["$firebaseAuth", function ($firebaseAuth) {
                var firebaseObj = new Firebase("https://scurdangular.firebaseio.com/");
                var loginObj = $firebaseAuth(firebaseObj);
                return loginObj.$requireAuth();
            }]
        }
    })
    .otherwise({
        redirectTo: '/login'
    }); 
}])

这是注册控制器:

// Register controller
.controller('RegisterCtrl', ['$scope', '$firebaseArray', '$location', 'CommonProp', '$firebaseAuth', function($scope, $firebaseArray, $location, CommonProp, $firebaseAuth) {
$scope.signUp = function() {

    var firebaseObj = new Firebase("https://xxxxxx.firebaseio.com/");
    var auth = $firebaseAuth(firebaseObj);
    var loginObj = $firebaseAuth(firebaseObj);

    var email = $scope.user.username +'@whateverdomain.com';
    var password = $scope.user.password;

    // Sign up implementation 
    if (!$scope.regForm.$invalid) {
        console.log('Valid form submission');

        auth.$createUser({email, password})
               .then(function(userData) {

                    // login user after registration

                    loginObj.$authWithPassword({
                            email: email,
                            password: password
                     });
                     $location.path('/tools');
                     console.log('user creation success', userData.uid);
                            }, function(error) {
                                console.log(error);
                                $scope.regError = true;
                                $scope.regErrorMessage = error.message;
                            });

                    }

            });
        });
    }    
};}])
4

1 回答 1

2

就像$createUser(),$authWithPassword()返回一个承诺。当用户通过身份验证时,该承诺就实现了,也就是您要重定向他们的时间:

 loginObj.$authWithPassword({
     email: email,
     password: password
 }).then(function(authData) {
     $location.path('/tools');
 });
于 2015-08-24T17:00:06.253 回答