当前位置: 动力学知识库 > 问答 > 编程问答 >

laravel 5 - how to redirect to login page if not authentificated angularjs

问题描述:

first time for me use angularjs, i dont know how to redirect login page if not authenticated, if not authenticated user cant access "users" page and "menu_utama" on my state.

app.js

(function() {

'use strict';

angular

.module('authApp', ['ui.router', 'satellizer'])

.config(function($stateProvider, $urlRouterProvider, $authProvider) {

// function to check the authentication //

var Auth = ["$q", "authService", function ($q, authService) {

authService.fillAuthData;

alert("authasdnaksjdhnk");

if (authService.authentication.isAuth) {

return $q.when(authService.authentication);

} else {

return $q.reject({ authenticated: false });

}

}];

console.log(Auth);

alert("aweawe");

// Satellizer configuration that specifies which API

// route the JWT should be retrieved from

$authProvider.loginUrl = '/api/authenticate';

// Redirect to the auth state if any other states

// are requested other than users

$urlRouterProvider.otherwise('/auth');

$stateProvider

.state('auth', {

url: '/auth',

templateUrl: '../views/authView.html',

controller: 'AuthController as auth'

})

.state('users', {

url: '/users',

templateUrl: '../views/userView.html',

controller: 'UserController as user'

})

.state('menu_utama', {

url: '/menu_utama',

templateUrl: '../views/menuUtama.html',

controller: 'UserController as user',

resolve: {

auth: Auth

}

});

});

})();

authController.js

(function() {

'use strict';

angular

.module('authApp')

.controller('AuthController', AuthController);

function AuthController($auth, $state) {

var vm = this;

//console.log(vm);

vm.login = function() {

var credentials = {

email: vm.email,

password: vm.password

}

// Use Satellizer's $auth service to login

//console.log(credentials);

$auth.login(credentials).then(function(data) {

alert("woe");

// If login is successful, redirect to the users state di file app.js

$state.go('users', {});

}, function(error) {

vm.loginError = true;

vm.loginErrorText = error.data.error;

alert("Login gagal");

//because we returned the $http.get request in the $auth.login

//promise, we can chain the next promise to the end here

});

}

}

})();

UserController.js

(function() {

'use strict';

angular

.module('authApp')

.controller('UserController', UserController);

function UserController($http) {

var vm = this;

console.log($http);

vm.users;

vm.error;

vm.getUsers = function() {

alert("a");

// This request will hit the index method in the AuthenticateController

// on the Laravel side and will return the list of users

$http.get('api/authenticate').success(function(users) {

vm.users = users;

}).error(function(error) {

alert("c");

vm.error = error;

});

};

vm.getCoba = function() {

alert("woe");

}

}

})();

网友答案:

I don't know if you found a solution but...

you may change this:

var Auth = ["$q", "authService", function ($q, authService) {
        authService.fillAuthData;
        alert("authasdnaksjdhnk");
        if (authService.authentication.isAuth) {
            return $q.when(authService.authentication);
        } else {
            return $q.reject({ authenticated: false });
        }
    }];

for this: (I've added the $state.go('login') in the else statement instead of the $q.reject(reason))

var Auth = ['$q','$state','authService', function ($q, $state, authService) {
        var deferred = $q.defer();
        authService.fillAuthData;
        if (authService.authentication.isAuth) {
            deferred.resolve();
        } else {
            $state.go('login');
        }
        return deferred.promise;
    }];

Since you've added the Auth function as a resolve in you route for this controller, it will simply redirect every user not authenticated to 'login' when they try to navigate to your restricted controller

edit : you may add this to your '/user' route state to avoid unauthenticated user access

resolve: {
   auth: Auth
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: