everyone! I'm creating app with authorization. To store user data during session I use such service:
app.service('Session', function ($rootScope, $cookies) {
var Session = $rootScope.$new();
Session.createSession = function (accessTo, level, user_data) {
this.level = level;
this.accessTo = accessTo;
if(user_data) {
$cookies.isAuth = true;
} else {
$cookies.isAuth = false;
}
this.user = {
email : user_data.email,
fullname : user_data.fullname,
id : user_data.id,
level : user_data.level
}
if(accessTo && accessTo.length == 1 && !$cookies.account) {
this.account = accessTo[0].id;
$cookies.account = accessTo[0].id;
} else {
this.account = $cookies.account;
}
};
Session.destroy = function () {
this.level = null;
this.account = null;
this.user = null;
this.isAuth = false;
$cookies.account = null;
};
return Session;
});
In controller use:
Session.createSession(some, params, here);
Afteer that it put some data to rootscope, and i can show it in console, but when i try to see for example Session.user/.level etc. it doesn't work. What is wrong?
$rootScope? It's a service, so a singleton, so you can inject it in every controller you need and it will be the same instance.app.service('Session', function ($rootScope, $cookies) { this.Session = whatever....thiswould be a reference to the service itself which is a singleton.