Clerk

Native authentication with Clerk sign-in, sign-out, and session management

Clerk is a complete authentication and user management platform. Median's Clerk Native Plugin integrates the iOS SDK and Android SDK into your app to provide a native sign-in experience with session token management.

The plugin provides a native UI for sign-in and sign-up flows, retrieves JWT session tokens for authenticating API requests, and supports checking authentication status at any time.

Clerk and App Configuration

Clerk Configuration

Create a Clerk application in the Clerk Dashboard and obtain your Publishable Key (starts with pk_test_ or pk_live_).

App Configuration

The Clerk plugin requires the publishable key to be configured in the App Studio, on the Native Plugins tab under "Advanced Mode":

{
  "clerk": {
    "active": true,
    "publishableKey": "pk_test_..."
  }
}
📘

Android Auto-Initialization

On Android, the Clerk SDK is automatically initialized at app startup using the publishableKey from the server configuration. On iOS you must call median.clerk.initialize() from JavaScript before using other methods.

Implementation Guide

Initialize the Clerk SDK

Initialize the Clerk SDK with your publishable key. On iOS this is required before calling any other method. On Android this is optional if the key is already set in the server configuration.

↔️Median JavaScript Bridge

To initialize Clerk:

const result = await median.clerk.initialize({
  publishableKey: 'pk_test_...'
});

// result object
{
  success: true | false,
  error: {                // present on failure only
    code: "NOT_INITIALIZED",
    message: "Invalid or missing publishable key"
  }
}

Parameters

ParameterTypeDescription
publishableKeystringRequired. Your Clerk publishable API key.

Present Sign-In

Present a native sign-in UI for the user. The interface supports sign-in and sign-up flows. Once the user completes or dismisses the flow, the callback returns the current authentication status.

↔️Median JavaScript Bridge

To present the Clerk sign-in screen:

const result = await median.clerk.presentSignIn();

// result object
{
  state: "signedIn" | "signedOut",
  userId: "user_2abc...",           // present when signed in
  hasValidToken: true | false,
  token: "eyJ..."                   // present when hasValidToken is true
}
// Callback example
median.clerk.presentSignIn({
  callback: function(result) {
    if (result.state === 'signedIn') {
      console.log('User signed in:', result.userId);
      console.log('Session token:', result.token);
    }
  }
});

Sign Out

Sign the current user out and invalidate the session.

↔️Median JavaScript Bridge

To sign out:

const result = await median.clerk.signOut();

// result object
{
  success: true | false,
  error: {                // present on failure only
    code: "NOT_INITIALIZED" | "SDK_ERROR",
    message: "..."
  }
}

Get Auth Status

Retrieve the current authentication status, including the session token and user ID. Use this to check if a user is still signed in or to obtain a fresh JWT token for API requests.

↔️Median JavaScript Bridge

To get the current auth status:

const result = await median.clerk.getAuthStatus();

// result object
{
  state: "signedIn" | "signedOut",
  userId: "user_2abc...",           // present when signed in
  hasValidToken: true | false,
  token: "eyJ..."                   // present when hasValidToken is true
}
// Example: use token for API request
const status = await median.clerk.getAuthStatus();
if (status.state === 'signedIn' && status.hasValidToken) {
  fetch('https://api.example.com/data', {
    headers: { Authorization: 'Bearer ' + status.token }
  });
}

Error Codes

CodeDescription
NOT_INITIALIZEDClerk SDK has not been initialized. Call initialize() first.
SDK_ERRORAn unexpected error occurred within the Clerk SDK.