У меня есть приложение, которое позволяет пользователям регистрироваться, регистрироваться и добавлять контент самостоятельно. Для аутентифицированного пользователя они могут посещать некоторые страницы, иначе они направят их на страницу входа. Тем не менее, я не мог понять, как заставить пользователя автоматически войти (перейти на страницу под названием инструменты) после регистрации. Как я могу передать регистрационную информацию со страницы регистрации на страницу инструментов?
Вот мой маршрут для страницы инструментов и страницы входа:
.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;
});
}
});
});
}
};}])
Как и $createUser()
, $authWithPassword()
возвращает обещание. Это обещание выполняется, когда пользователь был аутентифицирован, и это время, когда вы хотите перенаправить их:
loginObj.$authWithPassword({
email: email,
password: password
}).then(function(authData) {
$location.path('/tools');
});