0

I'm trying to get the little authentication app described in this article turned into a working example, roughly similar to this ember version.

The article snippets concatenated together make this:

// app.js

app.config(function ($httpProvider) {

  $httpProvider.interceptors.push(function ($timeout, $q, $injector) {
    var loginModal, $http, $state;

    // this trick must be done so that we don't receive
    // `Uncaught Error: [$injector:cdep] Circular dependency found`
    $timeout(function () {
      loginModal = $injector.get('loginModal');
      $http = $injector.get('$http');
      $state = $injector.get('$state');
    });

    return {
      responseError: function (rejection) {
        if (rejection.status !== 401) {
          return rejection;
        }

        var deferred = $q.defer();

        loginModal()
          .then(function () {
            deferred.resolve( $http(rejection.config) );
          })
          .catch(function () {
            $state.go('welcome');
            deferred.reject(rejection);
          });

        return deferred.promise;
      }
    };
  });

});

// LoginModalCtrl.js

app.controller('LoginModalCtrl', function ($scope, UsersApi) {

  this.cancel = $scope.$dismiss;

  this.submit = function (email, password) {
    UsersApi.login(email, password).then(function (user) {
      $scope.$close(user);
    });
  };

});

// loginModal.js

app.service('loginModal', function ($modal, $rootScope) {

  function assignCurrentUser (user) {
    $rootScope.currentUser = user;
    return user;
  }

  return function() {
    var instance = $modal.open({
      templateUrl: 'views/loginModalTemplate.html',
      controller: 'LoginModalCtrl',
      controllerAs: 'LoginModalCtrl'
    });

    return instance.result.then(assignCurrentUser);
  };

});

// routes.js

app.config(function ($stateProvider) {

  $stateProvider
    .state('welcome', {
      url: '/welcome',
      // ...
      data: {
        requireLogin: false
      }
    })
    .state('app', {
      abstract: true,
      // ...
      data: {
        requireLogin: true // this property will apply to all children of 'app'
      }
    })
    .state('app.dashboard', {
      // child state of `app`
      // requireLogin === true
    });

});

However, I don't know how to properly instantiate app, nor do I know what to include in the HTML or how to identify it for Angular.

Here is the JSbin I started with the above code where I added the following:

  • an ng-app="authApp" attribute to the html tag
  • an ng-controller="LoginModalCtrl" div containing the original author's form
  • a var app = angular.module("authApp", []); at the top of the JS.

The console is spitting errors I don't understand. Can someone show me the steps to bring this Angular app to life?

7
  • did you do var app = angular.module('authApp', [])? Commented Feb 16, 2015 at 19:09
  • yes I did. Is that what I was supposed to do? Commented Feb 16, 2015 at 19:13
  • well, the error in JSBin says that $stateProvider is unknown. I think you just did not include it in your html. Just download the angular-ui-router and include it in the html Commented Feb 16, 2015 at 19:21
  • Thanks for the advice. I pulled in angular-ui-router.js, but it made no difference. Commented Feb 16, 2015 at 19:34
  • first you should inject 'ui.router ' and 'ui.bootstrap' into to app module. second you inject 'UsersApi' to controller but there is no service called 'UsersApi' Commented Feb 16, 2015 at 20:10

1 Answer 1

1

tldr;

your first line should be

var app = angular.module("authApp", ['ui.router']);

and you need to download and include the UI Router module with something like:

<script src="app/js/angular-ui/angular-ui-router.min.js"></script>

a bit more detail:

the first console error coming up on the JSbin:

Error: [$injector:unpr] Unknown provider: $stateProvider

suggests you are calling $stateProvider in a place where angular does not know what $stateProvider is. $stateProvider comes from the Angular UI Router module. You need to include that module in your app to be able to call it down the line.

You need to download the UI-router module (https://github.com/angular-ui/ui-router), load it in the html file with something like

  <script src="app/js/angular-ui/angular-ui-router.min.js"></script>

and then include the module in the app by changing the first line to something like:

var app = angular.module("authApp", [
    'ui.router',
]);

That will knock out your first error. It looks like you'll have more errors after that, UsersApi is not defined as wickY26 mentions.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for reviving this thread with a nicely detailed answer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.