2

I made an one-page regular application using Angular.js, after a user signs in, if do a refresh in browser, the sign info is gone, sign in status is reset, I have to sign in again, how can I keep the log in status event doing a refresh in browser?

Thanks for your help

1
  • Keep a session ticket in a cookie? Commented Jul 1, 2013 at 15:44

1 Answer 1

7

You can use AngularJS service $cookieStore.

But in AngularJS 1.0.7- you cannot set the path or the cookie duration, you will need to implement your own service.

Some additional tips:

-for AngularJS 1.0.7- you need to download the library from here

-you need to inject the service $cookieStore in your controller/service

-you should create a global service that checks/sets/deletes the session cookie

Some starting point example on using a service:

app.service('global', function($cookieStore, $location, $filter) {
    var globalService = {};
    globalService.user = null;
    globalService.isAuth = function (){
        if (globalService.user == null) {
            globalService.user = $cookieStore.get('user');
        }
        return (globalService.user != null);
    };
    globalService.setUser = function(newUser) {
        globalService.user = newUser;
        if (globalService.user == null) $cookieStore.remove('user');
        else $cookieStore.put('user', globalService.user);
    };
    globalService.getUser = function() {
        return globalService.user;
    };
    return globalService;
});

After your login call setUser to save the user (maybe some token returned by the server) in a cookie. Then you should send the authentification token data for every request that requires a logged in user.

The token can be a PHP session (if using PHP) and the session can be restored on the server.

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

1 Comment

Thanks Kcsoft,can you please give a snippet of code?, Currently, I save the logged in user info in $rootScope. Is $rootScope reset when do a refresh in browser?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.