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 thehtmltag - an
ng-controller="LoginModalCtrl"divcontaining 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?
var app = angular.module('authApp', [])?