Steffi Steffi - 2 months ago 23
Javascript Question

How to store user session in Angular?

I have just started using AngularJS and I'm trying to store user session on my AngularApp.

First step to submit username and password works.
After that, I store the

retrieved from the service in the
The next page can display the

But after a refresh, the
is empty.

I'm trying to do an authentication system as simple as possible.

myApp.controller('loginController', ['$scope', '$rootScope', '$location', 'AuthService', '$route',
function ($scope, $rootScope, $location, AuthService, $route) {

$scope.login = function (credentials) {
AuthService.login(credentials).then(function (response) {
if( == "0"){
alert("Identifiant ou mot de passe incorrect");
else {
// is the JSON below
$rootScope.credentials =;


makes a


{user_id: 1, user_name: "u1", user_display_name: "Steffi"}


<div>Welcome {{ credentials.user_display_name }}!</div>

I tried a lot of tutorials, but I can't make session work.
I already used UserApp but it's not ok to me. I would like to create my own simple authentication.


$rootScope will always reset when the page refreshes, since it's a single-page app.

You need to use something that persists client-side, such as a cookie or sessionStorage (as they both have an expiration time). Take a look at the documentation for $cookieStore:$cookieStore

Remember, sensitive session information should be encrypted.