Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- service('actionService',function ($window, sessionService) {
- this.redirectNotLogged = function () {
- if(!sessionService.get('id')){
- $window.location.href = "some/url";
- }
- };
- });
- service('sessionService',function ($window, userService) {
- this.initSession = function () {
- usersService.getUserProfile()
- .then(function(res){
- sessionService.setSession(res);
- });
- };
- this.setSession = function (data) {
- $window.localStorage['session'] = data;
- };
- this.get = function (key) {
- var session = JSON.parse($window.localStorage.session);
- return session.data[key];
- };
- });
- service('userService',function (actionService) {
- this.getUserProfile = function () {
- return $http()
- .success(function(res){
- return res;
- });
- };
- this.doSomethingElse = function () {
- actionService.redirectNotLogged();
- //if session user
- alert('you are logged in');
- };
- });
- controller('Ctrl',function(sessionService, userService){
- sessionService.initSession();
- userService.doSomethingElse();
- });
Advertisement
Add Comment
Please, Sign In to add comment