Authenticate Cordova with AzureADv2 (oauth2) - Auth0
Cordova+ AzureADv2 (oauth2)

Setup your application's code

Install the Dependencies

The required dependencies for using Auth0 in a Cordova application are auth0.js and auth0-cordova. Install them with npm or yarn.

# installation with npm
npm install auth0-js @auth0/cordova --save

# installation with yarn
yarn add auth0-js @auth0/cordova

Add the following lines to your base code

var Auth0 = require('auth0-js');
var Auth0Cordova = require('@auth0/cordova');

function getAllBySelector(arg) {
  return document.querySelectorAll(arg);
}

function getBySelector(arg) {
  return document.querySelector(arg);
}

function getById(id) {
  return document.getElementById(id);
}

function getAllByClassName(className) {
  return document.getElementsByClassName(className);
}

function App() {
  this.auth0 = new Auth0.Authentication({
    domain: 'YOUR_AUTH0_DOMAIN',
    clientID: 'YOUR_CLIENT_ID'
  });
  this.login = this.login.bind(this);
  this.logout = this.logout.bind(this);
}

App.prototype.state = {
  authenticated: false,
  accessToken: false,
  currentRoute: '/',
  routes: {
    '/': {
      id: 'loading',
      onMount: function(page) {
        if (this.state.authenticated === true) {
          return this.redirectTo('/home');
        }
        return this.redirectTo('/login');
      }
    },
    '/login': {
      id: 'login',
      onMount: function(page) {
        if (this.state.authenticated === true) {
          return this.redirectTo('/home');
        }
        var loginButton = page.querySelector('.btn-login');
        loginButton.addEventListener('click', this.login);
      }
    },
    '/home': {
      id: 'profile',
      onMount: function(page) {
        if (this.state.authenticated === false) {
          return this.redirectTo('/login');
        }
        var logoutButton = page.querySelector('.btn-logout');
        var avatar = page.querySelector('#avatar');
        var profileCodeContainer = page.querySelector('.profile-json');
        logoutButton.addEventListener('click', this.logout);
        this.loadProfile(function(err, profile) {
          if (err) {
            profileCodeContainer.textContent = 'Error ' + err.message;
          }
          profileCodeContainer.textContent = JSON.stringify(profile, null, 4);
          avatar.src = profile.picture;
        });
      }
    }
  }
};

App.prototype.run = function(id) {
  this.container = getBySelector(id);
  this.resumeApp();
};

App.prototype.loadProfile = function(cb) {
  this.auth0.userInfo(this.state.accessToken, cb);
};

App.prototype.login = function(e) {
  e.target.disabled = true;

  var client = new Auth0Cordova({
    domain: 'YOUR_AUTH0_DOMAIN',
    clientId: 'YOUR_CLIENT_ID',
    packageIdentifier: 'YOUR_PACKAGE_ID' // found in config.xml
  });

  var options = {
    scope: 'openid profile',
    audience: 'https://YOUR_AUTH0_DOMAIN/userinfo'
  };
  var self = this;
  client.authorize(options, function(err, authResult) {
    if (err) {
      console.log(err);
      return (e.target.disabled = false);
    }
    localStorage.setItem('access_token', authResult.accessToken);
    self.resumeApp();
  });
};

App.prototype.logout = function(e) {
  localStorage.removeItem('access_token');
  this.resumeApp();
};

App.prototype.redirectTo = function(route) {
  if (!this.state.routes[route]) {
    throw new Error('Unknown route ' + route + '.');
  }
  this.state.currentRoute = route;
  this.render();
};

App.prototype.resumeApp = function() {
  var accessToken = localStorage.getItem('access_token');

  if (accessToken) {
    this.state.authenticated = true;
    this.state.accessToken = accessToken;
  } else {
    this.state.authenticated = false;
    this.state.accessToken = null;
  }

  this.render();
};

App.prototype.render = function() {
  var currRoute = this.state.routes[this.state.currentRoute];
  var currRouteEl = getById(currRoute.id);
  var element = document.importNode(currRouteEl.content, true);
  this.container.innerHTML = '';
  this.container.appendChild(element);
  currRoute.onMount.call(this, this.container);
};

module.exports = App;

Create an Application in Auth0 dashboard 

Create an Application in Auth0 dashboard

Install the Custom Social Connection extension under Extensions 

installing-custom-social-connections

List the app under the Installed Extensions tab. Click on the Custom Social Connections

Installed Extensions tab

Create a AzureADv2 (oauth2) connection under Custom Social Connections

Authorize the Custom Social Connections app

Configure your connection settings and enable your application under the Apps tab

Configure Social Connection Settings
GET STARTED IN MINUTES
Authentication Security and Availability

Authentication Built for Security & High Availability

Auth0 is the easiest way for developers to integrate enterprise-grade authentication and identity management to any app.

Industry Standard Compliance
Authentication Certifications and Compliance

SOC 2, HIPAA/BAA, EU/US Privacy Shield Framework, Open ID Certified.

Learn More
Built by Security Experts
uthentication Experts

Continuously reviewed and tested by external security experts. Strong password encryption and hashing algorithms, at rest and in transit.

Learn More
What Our Customers Say
Authentication Case Studies

Continuously reviewed and tested by external security experts. Strong password encryption and hashing algorithms, at rest and in transit.

Learn More
Availability & trust
Authentication Availability

Uptime time in 3 different regions.

Learn More
Host Anywhere
Auth0 Appliance

Deploy On-Premises or on Cloud on your data center or Auth0’s one.

Learn More
Auth0 Active Users

Get Auth0 free for 7,000 active users per month with unlimited logins

  • Single Sign On
  • Passwordless Login
  • Up to 2 Social Identity Providers
  • Multifactor Authentication
  • User Management
  • Extensibility (Run custom code on Auth)

Some of the companies that trust us every day

Atlassian - Authentication CustomerPBS - Authentication CustomerNVIDIA - Authentication CustomerMozilla - Authentication CustomerBluetooth - Authentication CustomerFinancial Times - Authentication CustomerJet - Authentication CustomerHMH - Authentication CustomerOptimizely - Authentication CustomerPolaris - Authentication CustomerHarper - Authentication CustomerNews Corp - Authentication Customer