Skip to content

AndroidAuthSession

Launches a web authentication flow that returns to the app via a redirect URL.

Kodster.InAppWebBrowser.Android.AndroidAuthSession

Overview

AndroidAuthSession is a specialized in-app browser session designed for OAuth and authentication workflows.

  • Initialization is handled automatically during plugin startup.
  • Cleanup is registered automatically on Application.quitting.
  • On non-Android platforms, initialization is skipped and IsSupportedByBrowser remains false.

Compared to general-purpose AndroidBrowser, AndroidAuthSession:

  • Strips unnecessary features: Removes UI elements and features not needed for authentication.
  • Enhances security: Receives authentication data via direct callbacks instead of intents, eliminating the attack surface and code complexity of intent-based approaches. This direct data transfer between the Android API and your app prevents potential interference.

Perfect for:

  • OAuth 2.0 flows (Google, GitHub, Facebook, etc.)
  • Account linking
  • Any authorization flow with a redirect URI scheme

Availability and fallback:

  • If androidx.browser is lower than 1.9.0, AndroidAuthSession is unavailable and StartAsync returns a failure result.
  • If androidx.browser is 1.9.0+ and the browser doesn't support AuthTab, it automatically falls back to a regular in-app browser.
  • Use IsSupportedByBrowser to check whether the current browser natively supports AndroidAuthSession if you need to manually decide fallback behavior.

Properties

IsSupportedByBrowser

public static bool IsSupportedByBrowser { get; }

Whether AndroidAuthSession is supported by the current browser version.

Returns

  • true if supported by the current browser; otherwise false.

Remarks

  • If androidx.browser is lower than 1.9.0, this remains false.
  • If androidx.browser is 1.9.0 or higher, this reflects browser support for AndroidAuthSession.
  • Not required to check before calling StartAsync().
  • Use this property to manually decide fallback behavior (for example, launch with AndroidBrowser).

Methods

StartAsync

public static Task<AndroidAuthResult> StartAsync(
    string url,
    string redirectScheme,
    AndroidAuthSessionOptions options = null
)

Starts an authentication session for flows that result in a redirect with a custom URL scheme. Returns a task that completes with the session result.

Parameters

  • url (string): The URL to load in the authentication session.
  • redirectScheme (string): The scheme of the resulting redirect.
  • options (AndroidAuthSessionOptions, optional): Optional appearance and behavior customization for the session.

Returns

  • A task that resolves to the session result.

Throws

  • ArgumentException: Thrown when url is null, empty, whitespace, or not a valid absolute http/https URL.
  • ArgumentNullException: Thrown when redirectScheme is null, empty, or whitespace.

StartAsync

public static Task<AndroidAuthResult> StartAsync(
    string url,
    string redirectHost,
    string redirectPath,
    AndroidAuthSessionOptions options = null
)

Starts an authentication session for flows that result in a redirect with the HTTPS scheme. Returns a task that completes with the session result.

Parameters

  • url (string): The URL to load in the authentication session.
  • redirectHost (string): The host portion of the resulting https redirect.
  • redirectPath (string): The path portion of the resulting https redirect.
  • options (AndroidAuthSessionOptions, optional): Optional appearance and behavior customization for the session.

Returns

  • A task that resolves to the session result.

Throws

  • ArgumentException: Thrown when url is null, empty, whitespace, or not a valid absolute http/https URL.
  • ArgumentNullException: Thrown when redirectHost is null, empty, or whitespace.

Example

#if UNITY_ANDROID
using Kodster.InAppWebBrowser.Android;
using UnityEngine;
using System.Threading.Tasks;

public sealed class LoginManager : MonoBehaviour
{
    public async Task LoginWithGoogle()
    {
        var options = new AndroidAuthSessionOptionsBuilder()
            .WithColorScheme(AndroidColorScheme.System)
            .Build();

        var result = await AndroidAuthSession.StartAsync(
            "https://accounts.google.com/o/oauth2/v2/auth?...",
            "com.example.game",
            options
        );

        if (result.Code == AndroidAuthResultCode.Success)
        {
            Debug.Log("Authentication successful!");
        }
        else
        {
            Debug.Log($"Authentication failed: {result.Message}");
        }
    }
}
#endif