This project is deprecated and is not maintained anymore

If you have used lock-passwordless in the past, a migration guide to Lock with Passwordless Mode is available here. If you have any issues, please reach out to our amazing support team at https://support.auth0.com.

Lock Passwordless

NPM version Build status Dependency Status License Downloads

Auth0 Lock Passwordless is a professional looking dialog that allows users to log in by receiving a one-time password via email or text message. It also supports social providers.

This library has been designed to work in a browser. Lock libraries for iOS and Android which support passwordless authentication are also available.

Legacy Grants

We’re working to join lock and lock-passwordless in the same project so we can move faster with both projects. In the mean time, new customers using lock-passwordless outside of the Hosted Login Page will see an authorization error about unallowed claims. To fix this, you either start using the Hosted Login Page or you’ll have to enable those grants manually in your client. More info here: https://auth0.com/docs/clients/client-grant-types#information-for-existing-and-new-auth0-customers

Playground

You can try it out yourself online at the playground.

Usage

To send a one-time password via text message you initialize a new Auth0LockPasswordless object and invoke the sms method.

var clientID = "YOUR_AUTH0_APP_CLIENTID";
var domain = "YOUR_DOMAIN_AT.auth0.com";
document.getElementById("loginButton").onclick = function(e) {
  var lock = new Auth0LockPasswordless(clientID, domain);
  lock.sms(function(error, profile, id_token) {
    // This will be invoked when the user enters the one-time password he or she
    // received via text message. Here we just welcome the user, but usually you
    // want save the profile and id_token, and handle errors.
    if (!error) {
      alert("Hi " + profile.name);
    }
  });
};

You can also send the user a magic link by invoking the magiclink method instead.

var clientID = "YOUR_AUTH0_APP_CLIENTID";
var domain = "YOUR_DOMAIN_AT.auth0.com";
document.getElementById("loginButton").onclick = function(e) {
  var lock = new Auth0LockPasswordless(clientID, domain);
  lock.magiclink();
};

Once the user receives the email and clicks on this link, Auth0 will handle the authentication and redirect back to the application with the token as the hash location. You can parse the hash and retrieve the full user profile.

// parse hash on page load
$(document).ready(function(){
  var hash = lock.parseHash(window.location.hash);

  if (hash && hash.error) {
    alert('There was an error: ' + hash.error + '\n' + hash.error_description);
  } else if (hash && hash.id_token) {
    //use id_token for retrieving profile.
    localStorage.setItem('id_token', hash.id_token);
    //retrieve profile
    lock.getProfile(hash.id_token, function (err, profile) {
      if (err){
        //handle err
      } else {
        //use user profile
      }
    });
  }
});

Install

You can obtain Lock Passwordless from our CDN, from bower or from npm.

CDN

<!-- Latest minor release -->
<script src="http://cdn.auth0.com/js/lock-passwordless-2.2.min.js"></script>

<!-- Latest patch release (recommended for production) -->
<script src="http://cdn.auth0.com/js/lock-passwordless-2.2.3.min.js"></script>

Bower

bower install auth0-lock-passwordless
<script src="bower_components/auth0-lock-passwordless/build/lock-passwordless.min.js"></script>

NPM

npm install --save auth0-lock-passwordless

After installing the auth0-lock-passwordless module, you’ll need bundle it up. We have examples for browserify and webpack.

Finally, if you are targeting mobile audiences, it’s recommended that you add:

<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0"/>

API

new Auth0LockPasswordless(clientID, domain)

Initializes a new instance of Auth0LockPasswordless configured with your application clientID and your account’s domain at Auth0.

You can find this information at your application settings.

Example

var clientID = "YOUR_AUTH0_APP_CLIENTID";
var domain = "YOUR_DOMAIN_AT.auth0.com";
var lock = new Auth0LockPasswordless(clientID, domain);

.magiclink(options, callback)

Opens a dialog that asks the user for an email address. Once entered, it will send an email containing a magic link that allows the user to log in automatically.

Example

// invoke magiclink without options or callback
lock.magiclink();

// invoke magiclink with an option that prevents the user from closing the
// dialog
lock.magiclink({closable: false});

// invoke magiclink with a callback function that displays an alert when the
// email has been sent.
lock.magiclink(function(error, email) {
  if (!error) {
    alert("A magic link has been sent to " + email);
  }
});

// invoke magiclink with options and callback
lock.magiclink({closable: false}, function(error, email) {
  if (!error) {
    alert("A magic link has been sent to " + email);
  }
});

.emailcode(options, callback)

Opens a dialog that asks the user for an email address. Then, it will ask for a code that has been sent in an email to the given address. The code will be used as a one-time password to log in.

Example

// invoke emailcode without options or callback
lock.emailcode();

// invoke emailcode with an option that prevents the user from closing the
// dialog
lock.emailcode({closable: false});

// invoke emailcode with a callback function that displays an alert when the
// user has logged in.
lock.emailcode(function(error, profile, id_token, access_token, state, refresh_token) {
  if (!error) {
    alert("User has logged in");
  }
});

// invoke emailcode with options and callback
lock.emailcode({closable: false}, function(error, profile, id_token, access_token, state, refresh_token) {
  if (!error) {
    alert("User has logged in");
  }
});

// invoke emailcode in redirect mode
lock.emailcode({callbackURL: "http://mydomain/callback"});

.sms(options, callback)

Opens a dialog that asks the user for a phone number. Then, it will ask for a code that has been sent in a text message to the given number. The code will be used as a one-time password to log in.

Example

// invoke sms without options or callback
lock.sms();

// invoke sms with an option that prevents the user from closing the dialog
lock.sms({closable: false});

// invoke sms with a callback function that displays an alert when the user has
// logged in.
lock.sms(function(error, profile, id_token, access_token, state, refresh_token) {
  if (!error) {
    alert("User has logged in");
  }
});

// invoke sms with options and callback
lock.sms({closable: false}, function(error, profile, id_token, access_token, state, refresh_token) {
  if (!error) {
    alert("User has logged in");
  }
});

// invoke sms in redirect mode
lock.sms({callbackURL: "http://mydomain/callback"});

.social(options, callback)

Opens a dialog with buttons to authenticate with the specified social providers.

Example

// invoke social allowing to authenticate with Facebook and Twitter.
lock.social({
  connections: ["facebook", "twitter"]
});

.socialOrMagiclink(options, callback)

Opens a dialog that is a combination of social and magiclink. It will display buttons to authenticate with the specified social providers and at the same time will ask the user for an email address. When the email address is entered, it will send an email containing a magic link that allows the user to log in automatically.

Example

// invoke socialOrMagiclink allowing to authenticate with Facebook and Twitter.
lock.socialOrMagiclink({
  connections: ["facebook", "twitter"]
});

.socialOrEmailcode(options, callback)

Opens a dialog that is a combination of social and emailcode. It will display buttons to authenticate with the specified social providers and at the same time will ask the user for an email address. When the email address is entered, it will send a code that serves as a one-time password to log in.

Example

// invoke socialOrEmailcode allowing to authenticate with Facebook and Twitter.
lock.socialOrEmailcode({
  connections: ["facebook", "twitter"]
});

.socialOrSms(options, callback)

Opens a dialog that is a combination of social and sms. It will display buttons to authenticate with the specified social providers and at the same time will ask the user for a phone number. When the phone number is entered, it will send the code in a text message that serves as a one-time password to log in.

Example

// invoke socialOrSms specifying that buttons to authenticate with Facebook and
// Twitter should be displayed.
lock.socialOrSms({
  connections: ["facebook", "twitter"]
});

.close(callback)

Closes the dialog.

Example

// invoke close without a callback
lock.close();

// invoke close with a callback
lock.close(function() {
  alert("The Lock has been closed");
});

.destroy()

Removes the Lock from the DOM frees its resources. Once destroyed a Lock can’t be opened.

Example

lock.destroy();

.getProfile(token, callback)

Fetches the full user profile.

Example

lock.getProfile(id_token, function(error, profile) {
  if (!error) {
    alert("hello " + profile.name);
  }
});

.logout(query)

Log out an user.

.parseHash(hash)

Parses the hash containing access_token and id_token appended by Auth0 to the URL in redirect mode.

Example

lock.parseHash(window.location.hash);

Example

// invoke logout without query parameters
lock.logout();

// invoke logout with query parameters
lock.logout({ref: window.location.href});

Customization

The appearance of the widget and the mechanics of authentication can be customized with an options object which has one or more of the following properties. Each method that opens the dialog can take an options object as its first argument.

UI options

Authentication options

Other options

Example

var options = {
  container: "myContainer",
  icon: "/path/to/my/icon.png",
  closable: false,
  dict: {title: "My Company"},
  focusInput: false,
  gravatar: false
};

Dict Specification

A dict, short for dictionary, is an object that contains every piece of text the Lock needs to display. Different textual components are needed depending on what method you called to open the Lock. The following is an example of the dict used when the Lock is opened with the emailcode method:

{
  code: {
    codeInputPlaceholder: "Your code",
    footerText: "",
    headerText: "Please check your email ({email})<br />You've received a message from us<br />with your passcode."
  },
  confirmation: {
    success: "Thanks for signing in."
  },
  email: {
    emailInputPlaceholder: "yours@example.com",
    footerText: "",
    headerText: "Enter your email to sign in or sign up."
  },
  title: "Auth0",
  welcome: "Welcome {name}!"
}

When you open the Lock with emailcode or any other method available, you can override any value by providing a dict option.

lock.emailcode({
  email: {
    footerText: "You must agree to our <a href='/terms' target='_new'>terms of service</a>"
  },
  title: "My Company"
});

The previous code will change the title displayed in the header and will display a footer when the Lock is asking the user for the email.

As you can see from the examples, some keys are namespaced inside another object and some are not. In the first case, they are only used in a given screen, while in the latter can be used from any screen. Also, most of the values accept HTML tags. The exception are the the ones used as input placeholders. Finally, some strings can be interpolated, which means that they contain a placeholder which will be replaced before being displayed. For instance:

lock.emailcode({
  code: {
    headerText: "The code has been sent to {email}"
  }
});

Will cause the Lock to show the message “The code has been sent to someone@auth0.com” when asking for the verification code to a user that entered the email “someone@auth0.com”.

You can check the source code to see the actual dictionaries used by the Lock.

A popup window can be displayed instead of redirecting the user to a social provider website. While this has the advantage of preserving page state, it has some issues. Often times users have popup blockers that prevent the login page from even displaying. There are also known issues with mobile browsers. For example, in recent versions of Chrome on iOS, the login popup does not get closed properly after login. For these reasons, we encourage developers to avoid this mode, even with Single Page Apps.

If you nevertheless decide to use it, you can activate popup mode by passing the option popup: true when calling social, socialOrMagiclink, socialOrEmailcode, or socialOrSms. A callback will be invoked with the usual arguments as the following example shows.

lock.social({
  connections: ["facebook", "twitter"],
  popup: true
}, function(error, profile, id_token, access_token, state, refresh_token) {
  if (!error) {
    alert("User has logged in");
  }
});

More information can be found in Auth0’s documentation.

Callbacks and Errors

As a rule of thumb, all callbacks passed to a method that opens the Lock are invoked when the job of the Lock can be considered done. The first argument of the callback is reserved for an error object. If an error occurred, the callback will be invoked with just the error object. If no error occurred, the callback will be invoked with null as the first argument, followed by any number of arguments it needs.

All error objects have an error and a description property. The first will contain a synthetic code used to identify the error, and the later will contain a more readable description. They may contain other useful properties according to the situation.

See each method’s documentation for the specifics.

Browser Compatibility

We ensure browser compatibility in Chrome, Safari, Firefox and IE >= 10. We currently use zuul along with Saucelabs to run integration tests on each push.

Contributing

Clone the repo and run the following commands:

npm install
npm start

Changes to the source code will be automatically rebuilt. To see the result, point your favorite browser to http://localhost:3000/playground/.

However, testing changes that way can be cumbersome because there are HTTP request involved which are being rate-limited. If your changes are scoped to the UI, you will be better off running:

npm run design

And pointing your browser to http://localhost:3000/design/. It behaves just like npm start but requests to the Auth0 API will be simulated.

Running the tests

Tests can be run in PhantomJS or in a web browser with the following commands:

npm run test:phantom
npm run test:browser

Releasing a new version

Whenever a new commit is pushed to master, the CI will attempt to deploy a new release to Github and npm if the tests pass and there isn’t already a release for the version specified in the package.json file. See bin/deploy for the details.

There is also a convenient script to prepare a new release:

bin/version {patch,minor,major}

Issue Reporting

If you have found a bug or if you have a feature request, please report them at this repository issues section. Please do not report security vulnerabilities on the public GitHub issue tracker. The Responsible Disclosure Program details the procedure for disclosing security issues.

Author

Auth0

License

This project is licensed under the MIT license. See the LICENSE file for more info.