Question

I use angular-http-auth for authentication in an angular-js app.

here is the login function inside the login controller :

$scope.login = function() {
    var credentials = Base64.encode($scope.username + ':' + $scope.password);
    var config = { headers: { 'Authorization': 'Basic ' + credentials } };          
    $http.get('url/to/json/user', config)
        .success(function() {
            $http.defaults.headers.common['Authorization'] = 'Basic ' + credentials;
            authService.loginConfirmed();
            console.log('login success');
        })
        .error(function() {
            console.log('login failed');
    });
}

(base64 is an encrypting service coming from here)

the problem: If the user is already logged-in and he opens a new tab or if he reloads the page, he has to log-in again.

How can is it possible to avoid that and to keep the session open if the user reloads the page or comes from an external link ?

Was it helpful?

Solution

You can use either cookies or the html5 datastore to save the credentials or the base64 string with the credentials. You can then load them from there and parse them to $http.defaults.headers.common['Authorization'] = 'Basic ' + credentials;

Hope it helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top