// Register the login() function
$scope.login = function() {
$http.post('/login', {
email: $scope.user.email,
password: $scope.user.password
})
.success(function(response) {
// authentication OK
$scope.loginError = 0;
$rootScope.user = response.user;
$rootScope.$emit('loggedin');
if (response.redirect) {
if (window.location.href === response.redirect) {
//This is so an admin user will get full admin page
window.location.reload();
} else {
window.location = response.redirect;
}
} else {
// Redirect Here
$location.url('/');
$location.url('/articles'); // Will take you to the articles view after login
}
})
.error(function() {
$scope.loginerror = 'Authentication failed.';
});
};
그들이 당신이 할 수있는 로그인하지 않고 보안 경로를 액세스 할 때 다른 페이지로 리디렉션하는 사용자가 필요한 경우 /packages/articles/public/routes/articles.js의 코드를 참조하십시오.
// This function checks if the user is logged in and redirects to the login page.
var checkLoggedin = function($q, $timeout, $http, $location) {
// Initialize a new promise
var deferred = $q.defer();
// Make an AJAX call to check if the user is logged in
$http.get('/loggedin').success(function(user) {
// Authenticated
if (user !== '0') $timeout(deferred.resolve);
// Not Authenticated
else {
$timeout(deferred.reject);
$location.url('/login');
}
});
return deferred.promise;
};
// These are your defined routes.
$stateProvider
.state('all articles', {
url: '/articles',
templateUrl: 'articles/views/list.html',
// This resolve runs the checkLoggedin function as the route is accessed
// and redirects if the user isn't logged in.
resolve: {
loggedin: checkLoggedin
}
});
위대한 코드입니다. 사용자가 로그인하지 않고/app를 방문하면/auth/login으로 어떻게 보내나요? –
@MichaelCole auth'd 코드가 아니라면 리디렉션을 포함하도록 원래 답변을 편집했습니다. 희망이 도움이됩니다. – itaylorweb
누구나 더 나은 해결책이 있습니까? –