I'm new to angularjs, I'm bit confused about Login Process.
Everytime I log in I'm redirected to the specific page. Which is set already in the code.
I just want to check if the user is logged in. If yes then redirect to --Home-- if not logged in redirect to --Login Again--
What should I use for this?
I heard about local storage. SessionStorage, I'm not familiar with them. Tell me which way I can manage Login?
Here is my controller for Login
app.controller('AngularLoginController', ['$scope', '$http', function($scope, $http) {
$scope.loginForm = function() {
$http.post("login.php", {
'email' :$scope.inputData.email,
'password':$scope.inputData.password
}).success(function(data) {
console.log(data);
if ( data == 'correct') {
window.location.href = 'welcome_dashboard.php';
}
else {
$scope.errorMsg = "Invalid Email and Password";
}
})
}
}]);
You can use $cookieStore of angular js
after successfull login you can put login true
$cookieStore.put('login', true);
and check in your dashboard controller if $cookieStore.get('login') undefined than redirect on login page
When you logout you need to remove this cookie $cookieStore.remove('login')
if you user routerprovider than use resolve it's easy to check login or not here one simple example of resolve
function ($routeProvider) {
$routeProvider.
when('/', {
controller: 'dashboardController',
templateUrl: 'app/views/dashboard.html',
resolve:{loggedIn:onlyLoggedIn}
})
}
var onlyLoggedIn = function ($location,$q,$cookieStore,$rootScope) {
var deferred = $q.defer();
if (typeof ($cookieStore.get("login")) === "undefined") {
deferred.reject();
window.location.href = 'login.html';
}else{
$rootScope.display = true;
deferred.resolve();
}
return deferred.promise;
};