0

Firstly, apology if this question does not make sense. I am developing code for session management for my mean stack app. From last few days, i found lots of way to implement it which are using either cookies, sessions or http - headers. I tried to implement, but did not get success.

I successfully link the interceptor with my code. Code is listening to each req/res.

Here is some code:

app.js

angular.module('MyApp', [
    'ngMaterial',
    'ngMdIcons',
    'ui.router',
    'e3-core-ui.services', 
    'e3-core-ui.utils'  
])
.config(['$stateProvider', '$routeProvider','$httpProvider','$mdThemingProvider', '$mdIconProvider',  function($stateProvider, $routeProvider, $httpProvider, $mdThemingProvider, $mdIconProvider) { 
 $httpProvider.interceptors.push('YourHttpInterceptor');
...

Interceptor-code

angular.module('MyApp')

.factory('YourHttpInterceptor', ['$q', 
function($q, ) {
    return {        
        'request': function(config) {
            console.log("req"); 
            return config;
        },       

        // Optional method        
        'response': function(response) {
            // do something on response success
            console.log("inside the response ");
            return response;
        },

        // optional method 
        'responseError': function(rejection) {
            // Here you can do something in response error, like handle errors, present error messages etc.
            console.log("inside the response error ");
            return $q.reject(rejection);
        }
    };
}]);

I will be very thankful for your time and help.

jatinder bhola
  • 385
  • 1
  • 7
  • 23
  • what do you mean with "Maintain session using Interceptor"? you can still get a cookie from the server to store a session without using an interceptor, you better look at an auth library like passport – pedrommuller Jun 03 '16 at 20:20
  • @jack.the.ripper, I am trying to implement the session management inside the MEAN app. So that when session time out, user redirect to login page. Right now, after 20min redis disconnect from the app and stop showing data but the app user interface works. I want to implement something so that when redis disconnect and user try to fire any request it redirect to login page. Make sense?!! – jatinder bhola Jun 03 '16 at 21:01
  • it's more clear now, take a look to my answer – pedrommuller Jun 03 '16 at 21:37

1 Answers1

1

In Meanjs you have the authentication controller

mean/modules/users/client/controllers/authentication.client.controller.js

but if you want to use the authentication service in your interceptor, just be aware that injecting a dependency isn't that easy as doing it in a controller.

you'll need to use $injector

 var AuthService = $injector.get('Auth');

then you'll have to be sure your user is authenticated and check that in your request function, something like

 if (!Authentication.user) {
     $location.path('/'); // or a login page
 }
Community
  • 1
  • 1
pedrommuller
  • 15,741
  • 10
  • 76
  • 126