Migrating Angular 1.x Applications to from Lock v9 to Lock v11

Versionv11

This guide will help you migrating your Angular 1.x application from Lock 9 to Lock v11.

Update Lock

Update the Lock library using npm or yarn.

# installation with npm
npm install --save auth0-lock

# installation with yarn
yarn add auth0-lock

Once updated, you can add it to your build system or bring it in to your project with a script tag.

<script type="text/javascript" src="node_modules/auth0-lock/build/lock.js"></script>

If you do not want to use a package manager, you can retrieve Lock from Auth0's CDN.

<script src="https://cdn.auth0.com/js/lock/11.6.1/lock.min.js"></script>

Add the angular-lock library

Most Angular 1.x apps used the auth0-angular which is currently deprecated. This guide will use auth0-lock 3.0 to simplify using Lock v11.

Use npm to uninstall the old library and install the new one.

npm uninstall auth0-angular --save
npm install angular-lock --save

Then include it in your index.html file:

<!-- index.html -->
<script src="node_modules/angular-lock/dist/angular-lock.min.js"></script>

Update the app.js file

In your app.js file:

  • Add auth0.lock to your Angular module
  • Inject lockProvider in your configuration
  • Initialize a Lock instance in config()
  • Inject your authentication service in run() and call the method that listens for authentication events (we will implement changes in the authentication service next)
// app.js
(function() {
  'use strict';

  angular
    .module('app', ['ngRoute', 
//   'auth0' 
     'auth0.lock']) // Add auth0.lock
      .config(config)
      .run(run);

  config.$inject = [
    '$locationProvider',
    '$routeProvider',
 // 'authProvider',
    'lockProvider' // Inject lockProvider
  ];

  function config(
    $locationProvider,
    $routeProvider,
//  authProvider
    lockProvider // Provide lockProvider
  ) {
    $routeProvider
      .when('/', {
        controller: 'HomeController',
        templateUrl: 'app/home/home.html',
        controllerAs: 'vm'
      })
      .otherwise({
        redirectTo: '/'
  });

  $locationProvider.hashPrefix('');
  $locationProvider.html5Mode(true);

  // Configure Auth0 Lock instance
  // Read more about configuration here:
  // https://auth0.com/docs/libraries/lock/v11
  lockProvider.init({
     domain: 'YOUR_AUTH0_DOMAIN'),
     clientID: 'YOUR_CLIENT_ID',
     options: {
       autoclose: true,
       auth: {
         responseType: 'token id_token',
         audience: 'https://' + 'YOUR_AUTH0_DOMAIN' + '/userinfo',
         params: {
           scope: 'openid profile email'
         }
       }
     }
   });
  }

  // Inject authentication service and
  // run method to handle authentication
  run.$inject = ['authService'];

  function run(authService) {
    authService.handleAuthentication();
  }

}());

Update the authentication service

If you are using an authentication service, you will need to make a few minor changes there as well. Lock v11 provides events so you can execute functionality when the user is authenticated, when there is an authentication error, and so on. You can read more at the Lock v11 documentation.

// auth.service.js
(function() {
  'use strict';

  angular.module('app').service('authService', authService);

  // Inject 'lock'
  authService.$inject = ['lock', '$location'];

  function authService(lock, $location) {
 
    function login() {
      // Display the Lock widget using the
      // instance initialized in the app.js config
      lock.show();
    }

    function logout() {
      // Remove tokens and expiry time from localStorage
      localStorage.removeItem('access_token');
      localStorage.removeItem('id_token');
      localStorage.removeItem('expires_at');
      $location.path('/');
    }

    function handleAuthentication() {
      // Uncomment if you are not using HTML5Mode
      // lock.interceptHash();

      lock.on('authenticated', function(authResult) {
        if (authResult && authResult.accessToken && authResult.idToken) {
          console.log('Authenticated!', authResult);
          _setSession(authResult);
        }
      });
      lock.on('authorization_error', function(err) {
        console.log(err);
        alert(
          'Error: ' + err.error + '. Check the console for further details.'
        );
      });
    }

    function _setSession(authResult) {
      // Set the time that the Access Token will expire
      var expiresAt = JSON.stringify(
        authResult.expiresIn * 1000 + new Date().getTime()
      );
      // Save tokens and expiration to localStorage
      localStorage.setItem('access_token', authResult.accessToken);
      localStorage.setItem('id_token', authResult.idToken);
      localStorage.setItem('expires_at', expiresAt);
    }

    function isAuthenticated() {
      // Check whether the current time is
      // past the Access Token's expiry time
      var expiresAt = JSON.parse(localStorage.getItem('expires_at'));
        return new Date().getTime() < expiresAt;
    }

    return {
      login: login,
      logout: logout,
      handleAuthentication: handleAuthentication,
      isAuthenticated: isAuthenticated
    };
  }
})();

If you are not using a service, you can implement the handleAuthentication() method's functionality in your app's run() method, and implement the login(), logout(), and _setSession() functionality in the controller where your login is located.

Configure your Auth0 application for embedded login

When implementing embedded login, Lock v11 will use cross-origin calls inside hidden iframes to perform authentication. To make sure this can be done securely, Auth0 needs to know the domains where you will be hosting your applications.

Add the domain to the Allowed Web Origins field. You can find this field in the Application Settings area of your Dashboard.

Allowed Web Origins

Change calls to getProfile()

The deprecated getProfile() function was reimplemented in Lock 11. The previous implementation received an ID Token as a parameter and returned the user profile.

The new implementation requires an Access Token parameter instead.

Behavioral Changes in Lock v11

Usage in popup mode

When using popup mode in previous versions of Lock, a new browser window was opened and immediately closed in order to complete the authentication transaction. In Lock 11, that window is opened on a hidden iframe, providing a better user experience.

Last time you logged in with window with authorization code flow

Lock 11 will never show the Last time you logged in with window when using the Authorization Code Flow (that is, when specifying response_type='code'). It will always prompt for credentials.

Last time you logged in with window and redirects

The Last time you logged in with window will never do a redirect, even when the redirect option is set to true. Lock11 still emits the authenticated event and you should subscribe to that event to get the authentication result.

If you want to avoid showing the Lock dialog when there's an existing session in the server, you can use Auth0.js's checkSession() function.

Single sign on using IP ranges

In earlier versions of Lock, you could configure an IP range in an Active Directory/LDAP connection. You could then use that range to allow integrated Windows Authentication if the user's IP was within the range. When this was true, Lock would display a button that users could click and get redirected to the integrated authentication dialog.

SSO With Lock 10 and Windows IP Ranges

This functionality has been removed from embedded login using Lock 11. There is no IP detection, and the user will need to type user and password in Lock. It is still available when using Universal Login.

Default values

Lock 11 will default the scope parameter to 'openid profile email'. This is to make the Last time you logged in with window work correctly.

If you are running your website from http://localhost or http://127.0.0.1 and you do not specify the openid profile email scope when initializing Lock, you may get the following error in the browser console:

Consent required. When using `getSSOData`, the user has to be authenticated with the following scope: `openid profile email`

This will not happen when you run your application in production, or if you specify the required scope. You can read more about this scenario in the documentation on skipping consent for first-party applications.