1

I'm building an App that requires authentication and I need to check if the user is authenticated when changing routes before instantiating the route Controller, so I need a function to retrieve the logged user from the server with $http that I need to call in the resolve property of the 'when' and then pass the retrieved user to the controller. How can I declare the function?

This is my app.js

angular.module('MasterToolsApp', ['ui.bootstrap', 'ngRoute', 'ngSanitize', 'ngResource', 'ngAnimate', 'dialogs.main', 'toasty']);

angular.module('MasterToolsApp')
    .config(['$routeProvider', function($routeProvider) {

        $routeProvider
            .when('/', {
                redirectTo: '/login'
            })
            .when('/login', {
                templateUrl : 'dist/templates/login.html',
                controller  : 'LoginController',
                resolve     : ?
            })
            .when('/home', {
                templateUrl : 'dist/templates/home.html',
                controller  : 'HomeController',
                resolve     : ?
            })
            .when('/entries', {
                templateUrl : 'dist/templates/entries.html',
                controller  : 'EntriesController',
                resolve     : ?
            })
            .otherwise({ redirectTo: '/login' });

    }]);
jww
  • 97,681
  • 90
  • 411
  • 885
Gabriel Matusevich
  • 3,835
  • 10
  • 39
  • 58

1 Answers1

0

I found the answer in another question: Related Question

The resulting code is this:

angular.module('MasterToolsApp', ['ui.bootstrap', 'ngRoute', 'ngSanitize', 'ngResource', 'ngAnimate', 'dialogs.main', 'toasty']);

var Helpers = {
    checkAuthentication: function($http) {

        return $http(
            {
                url     : '/auth',
                method  : 'GET',
                headers     : {'Content-Type': 'application/x-www-form-urlencoded'},
                timeout     : 10000
            }
        );

    }
};

angular.module('MasterToolsApp')
    .config(['$routeProvider', function($routeProvider) {

        $routeProvider
            .when('/', {
                redirectTo: '/login'
            })
            .when('/login', {
                templateUrl : 'dist/templates/login.html',
                controller  : 'LoginController',
                resolve     : {
                    user: ['$http', function($http) {
                            return Helpers.checkAuthentication($http);
                        }
                    ]
                }
            })
            .when('/home', {
                templateUrl : 'dist/templates/home.html',
                controller  : 'HomeController',
                resolve     : {
                    user: ['$http', function($http) {
                        return Helpers.checkAuthentication($http);
                    }
                    ]
                }
            })
            .when('/entries', {
                templateUrl : 'dist/templates/entries.html',
                controller  : 'EntriesController',
                resolve     : {
                    user: ['$http', function($http) {
                        return Helpers.checkAuthentication($http);
                    }
                    ]
                }
            })
            .otherwise({ redirectTo: '/login' });

    }]);
Community
  • 1
  • 1
Gabriel Matusevich
  • 3,835
  • 10
  • 39
  • 58