0

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";
            }
        })
        }
}]);

2 Answers 2

1

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;

};
Sign up to request clarification or add additional context in comments.

3 Comments

You might not be logged in anymore. E.g. the session expired.
where to keep this onlyLoggedIn function? controller ?
what's issue you face now?
0

You'd only use localStorage or sessionStorage if you had a token, for example if you were authenticating using OAuth.

In your case you'll probably be using cookies and sessions. So for this scenario you'd want to have someway of checking if you're authenticated (as your session or cookie might have expired).

I'd create a new endpoint e.g. current_user.php that returns true or false (or even the current user details) if you're logged in or not. And then you'd want to call this before any requests that expect the user to be authenticated).

You'll also want to handle any requests that return data that require the user to be authenticated with a fail callback that also redirect the user should you get a 401 back.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.