IWI
IWI

Reputation: 1604

Auth0 is not a consutructor - AngularJS and auth0

I just followed this guide to add auth0 (for the first time ever) to my angularJS application. After clicking the login button, and entering my credentials, the screen reloads (with a URL that has the token and id), and then reloads again, throwing the following exception:

TypeError: Auth0 is not a constructor
    at angular-lock.js:77
    at Scope.$broadcast (angular.js:17767)
    at angular.js:13523
    at Scope.$eval (angular.js:17444)
    at Scope.$digest (angular.js:17257)
    at Scope.$apply (angular.js:17552)
    at bootstrapApply (angular.js:1754)
    at Object.invoke (angular.js:4709)
    at doBootstrap (angular.js:1752)
    at bootstrap (angular.js:1772)
    at angularInit (angular.js:1657)
    at HTMLDocument.<anonymous> (angular.js:31468)
    at j (jquery-2.1.1.min.js:2)
    at Object.fireWith [as resolveWith] (jquery-2.1.1.min.js:2)
    at Function.ready (jquery-2.1.1.min.js:2)
    at HTMLDocument.I (jquery-2.1.1.min.js:2)

angular-lock.js:77 has the following code:

lock.interceptHash = function() {
        $rootScope.$on('$locationChangeStart', function(event, location) {

          if (/id_token=/.test(location) || /error=/.test(location)) {
            var auth0 = new Auth0(credentials);

Where the if statement is line 77. Does anyone know what caused this? Here is my code for the various auth0 components:

login.js

.controller('LoginCtrl', function (authService ,$scope, $rootScope, $state, $log, ServerRequest, $localStorage, $sessionStorage, uiGridConstants, $interval) {

            var vm = this, c = console;
            c.log('hit login controller');

            vm.authService = authService;

app.run.js

run.$inject = ['$rootScope', 'authService', 'lock', 'authManager'];

  function run($rootScope, authService, lock, authManager) {
    // // Put the authService on $rootScope so its methods
    // // can be accessed from the nav bar
     $rootScope.authService = authService;
    //
    // // Register the authentication listener that is
    // // set up in auth.service.js
     authService.registerAuthenticationListener();
    //
    // // Register the synchronous hash parser
    // // when using UI Router
    lock.interceptHash();
    authManager.checkAuthOnRefresh();
  }

auth.service.js

function authService(lock, authManager, $state) {

    function login() {
      lock.show();
    }

    function logout() {
      localStorage.removeItem('id_token');
      authManager.unauthenticate();
      $state.go('login');
    }

    // Set up the logic for when a user authenticates
    // This method is called from app.run.js
    function registerAuthenticationListener() {
      lock.on('authenticated', function (authResult) {
        localStorage.setItem('id_token', authResult.idToken);
        authManager.authenticate();
        $state.go('list.grid');
      });
    }

    return {
      login: login,
      logout: logout,
      registerAuthenticationListener: registerAuthenticationListener
    }
  }

Additionally (i dont really know if its related), when logged out, I can still access all of my app. I am a newb at this, so any direction would be greatly appreciated.

Upvotes: 0

Views: 624

Answers (2)

Andrew Lobban
Andrew Lobban

Reputation: 2137

The actual solution for me running:

"auth0-lock": "^10.11.0",
"angular-lock": "^1.0.5",
"auth0.js": "7.6.1"

I had to install Auth0.js v7 separately as a dependency; v8 had issues. Now Auth0 Lock widget works for me.

Additionally Auth0 provided me with this repo, where they are using Auth0-Lock 10.11.0 and Auth0.js 8.0.4:

https://github.com/auth0-samples/auth0-angularjs-samples/tree/master/01-Basic-Login

Upvotes: 1

Martin Dal
Martin Dal

Reputation: 21

Had the same problem. Documentation is not up to date. Fixed that particular problem by forcing "auth0-lock": "10.2.2" in the bower.json.

  1. Delete your /bower_components folder
  2. Edit the bower.json to use auth0-lock version 10.2.2
  3. do $ bower install from terminal
  4. Reload your application

Upvotes: 2

Related Questions