witoldsz/angular-http-auth

Did not work with requirejs

thomas-tran opened this issue · 1 comments

I tried to reference this module with requirejs however it keep throwing exception on unknown provider http-auth-interceptor.

Here is my main.js

requirejs.config({
baseUrl: 'app',
urlArgs: 'bust=v1' + (new Date().getTime()),
waitSeconds: 20, // make sure it is enough to load all scripts
paths: {
angular: '../lib/ionic/js/angular/angular.min',
angularAnimate: '../lib/ionic/js/angular/angular-animate.min',
angularSanitize: '../lib/ionic/js/angular/angular-sanitize.min',
angularFilter: '../lib/ionic/js/angular/angular-filter.min',
angularMocks: '../lib/ionic/js/angular/angular-mocks',
uiRouter: '../lib/ionic/js/angular-ui/angular-ui-router.min',
ionic: '../lib/ionic/js/ionic.min',
ionicAngular: '../lib/ionic/js/ionic-angular.min',
text: '../lib/ionic/js/text',
httpAuthInterceptor: '../lib/http-auth-interceptor'

},
shim: {
    angular : {exports : 'angular'},
    angularAnimate : {deps: ['angular']},
    angularSanitize: { deps: ['angular'] },
    angularFilter: { deps: ['angular'], exports: 'angular.filter' },
    angularMocks : {deps :['angular']},
    uiRouter : {deps: ['angular']},
    ionic :  {deps: ['angular'], exports : 'ionic'},
    httpAuthInterceptor: { deps: ['angular'] },
    ionicAngular: { deps: ['angular', 'ionic', 'uiRouter', 'angularAnimate', 'angularSanitize', 'angularFilter', 'httpAuthInterceptor', 'angularMocks'] },
},
priority: [
    'angular',
    'ionic'

],
deps: [
    'bootstrap'
]

});

and the authenticate service

define(['../module'],
function (module) {
'use strict';

        var name = 'AuthenticateService';

        var dependencies = ['$rootScope', '$http', 'http-auth-interceptor'];

        var service = function ($rootScope, $http, authService) {
            console.log(authService);
            return {

                fake: function(){
                    console.log('fake login');
                },

                login: function (user) {
                    $http.post('https://login', { user: user }, { ignoreAuthModule: true })
                         .success(function (data, status, headers, config) {

                    $http.defaults.headers.common.Authorization = data.authorizationToken;  // Step 1

                        // Need to inform the http-auth-interceptor that
                        // the user has logged in successfully.  To do this, we pass in a function that
                        // will configure the request headers with the authorization token so
                        // previously failed requests(aka with status == 401) will be resent with the
                        // authorization token placed in the header
                    authService.loginConfirmed(data, function (config) {  // Step 2 & 3
                            config.headers.Authorization = data.authorizationToken;
                            return config;
                        });
                    })
                    .error(function (data, status, headers, config) {
                        $rootScope.$broadcast('event:auth-login-failed', status);
                    });
                },

                logout: function (user) {
                    $http.post('https://logout', {}, { ignoreAuthModule: true })
                         .finally(function (data) {
                             delete $http.defaults.headers.common.Authorization;
                             $rootScope.$broadcast('event:auth-logout-complete');
                    });
                },

                loginCancelled: function () {
                    authService.loginCancelled();
                }
            }
        };

        module.factory(name, dependencies.concat(service));

    });

This module has no service called http-auth-interceptor. It's authService. Remember, when using DI array, the array strings are real service names, function params can be mapped to whatever you want.

var dependencies = ['$rootScope', '$http', 'authService'];

This should be closed. It's not an issue of angular-http-auth.