# Modules.WebDialog.AuthenticationSession

Authenticate a user with a web service, even if the web service is run by a third party.

Availability
7.1.0

# Overview

The AuthenticationSession puts the user in control of whether they want to use their existing logged-in session from Safari. The app provides a URL that points to the authentication webpage. The page will be loaded in a secure view controller. From the webpage, the user can authenticate herself and grant access to the app. On completion, the service will send a callback URL with an authentication token, and this URL will be passed to the app by the callback.

The callback URL usually has a custom URL scheme. For the app to receive the callback URL, it needs to either register the custom URL scheme in its Info.plist, or set the scheme to scheme argument in the initializer.

If the user has already logged into the web service in Safari or other apps via the AuthenticationSession, it is possible to share the existing login information. An alert will be presented to get the user's consent for sharing their existing login information. If the user cancels the alert, the session will be canceled, and the callback will be called.

If the user taps Cancel when showing the login webpage for the web service, the session will be canceled, and the callback will be called as well.

The app can cancel the session by calling cancel(). This will also dismiss the window that is showing the web service's login page.

# Requirements

The AuthenticationSession API is available with the Titanium SDK starting with Release 7.1.0. This module only works with devices running iOS 11.0 and later. Please make sure you have at least Xcode 9 to build to the required sources.

# Getting Started

Create a new authentication session by providing a url and scheme, create an event-listener and start:

var WebDialog = require('ti.webdialog');

var authSession = WebDialog.createAuthenticationSession({
  url: 'https://example.com/oauth?callbackURL=myapp://',
  scheme: 'myapp://'
});

authSession.addEventListener('callback', function(e) {
  if (!e.success) {
      Ti.API.error('Error authenticating: ' + e.error);
      return;
  }
  
  Ti.API.info('Callback URL: ' + e.callbackURL);
});

authSession.start(); // Or cancel() to cancel it manually.

# Properties

# apiName READONLY

Availability
7.1.0
apiName :String

The name of the API that this proxy corresponds to.

The value of this property is the fully qualified name of the API. For example, Titanium.UI.Button returns Ti.UI.Button.


# bubbleParent

Availability
7.1.0
bubbleParent :Boolean

Indicates if the proxy will bubble an event to its parent.

Some proxies (most commonly views) have a relationship to other proxies, often established by the add() method. For example, for a button added to a window, a click event on the button would bubble up to the window. Other common parents are table sections to their rows, table views to their sections, and scrollable views to their views. Set this property to false to disable the bubbling to the proxy's parent.

Default: true


# scheme

Availability
7.1.0
scheme :String

The custom URL scheme that the app expects in the callback URL.


# url

Availability
7.1.0
url :String

The initial URL pointing to the authentication webpage. Only supports URLs with http:// or https:// schemes.

# Methods

# addEventListener

Availability
7.1.0
addEventListener(name, callback) void

Adds the specified callback as an event listener for the named event.

Parameters

Name Type Description
name String

Name of the event.

callback Callback<Titanium.Event>

Callback function to invoke when the event is fired.

Returns

Type
void

# applyProperties

Availability
7.1.0
applyProperties(props) void

Applies the properties to the proxy.

Properties are supplied as a dictionary. Each key-value pair in the object is applied to the proxy such that myproxy[key] = value.

Parameters

Name Type Description
props Dictionary

A dictionary of properties to apply.

Returns

Type
void

# cancel

Availability
7.1.0
cancel() void

Cancel an authentication-session.

If the view controller is already presented to load the webpage for authentication, it will be dismissed. Calling cancel on an already canceled session will have no effect.

Returns

Type
void

# fireEvent

Availability
7.1.0
fireEvent(name[, event]) void

Fires a synthesized event to any registered listeners.

Parameters

Name Type Description
name String

Name of the event.

event Dictionary

A dictionary of keys and values to add to the Titanium.Event object sent to the listeners.

Returns

Type
void

# removeEventListener

Availability
7.1.0
removeEventListener(name, callback) void

Removes the specified callback as an event listener for the named event.

Multiple listeners can be registered for the same event, so the callback parameter is used to determine which listener to remove.

When adding a listener, you must save a reference to the callback function in order to remove the listener later:

var listener = function() { Ti.API.info("Event listener called."); }
window.addEventListener('click', listener);

To remove the listener, pass in a reference to the callback function:

window.removeEventListener('click', listener);

Parameters

Name Type Description
name String

Name of the event.

callback Callback<Titanium.Event>

Callback function to remove. Must be the same function passed to addEventListener.

Returns

Type
void

# start

Availability
7.1.0
start() Boolean

Starts the AuthenticationSession instance after it is instantiated.

The start method can only be called once for an AuthenticationSession instance. This also means calling start on a canceled session will fail.

Returns

Returns true if the session starts successfully.

Type
Boolean

# Events

# callback

Availability
7.1.0

The callback which is called when the session is completed successfully or canceled by user.

Properties

Name Type Description
error String

The error-message returned in case something went wrong.

callbackURL String

The callback-URL returned in case the OAuth-flow succeeded.

source Object

Source object that fired the event.

type String

Name of the event fired.

bubbles Boolean

True if the event will try to bubble up if possible.

cancelBubble Boolean

Set to true to stop the event from bubbling.