Skip to content

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

React Native Android AuthTab

A React Native module for Android Auth Tab authentication using the new AuthTabIntent API.

Installation

npm install react-native-android-authtab
# or
yarn add react-native-android-authtab

Usage

Basic Authentication Flow

import { openAuthTab } from 'react-native-android-authtab';

// Open an auth tab for OAuth authentication
try {
  const result = await openAuthTab(
    'https://example.com/oauth/authorize?client_id=...',
    'myapp-auth'
  );
  console.log('Auth redirect URI:', result.resultUri);
} catch (error) {
  console.error('Auth failed:', error.code, error.message);
}

Advanced Configuration with OAuth Parameters

You can construct the authentication URL with your OAuth parameters:

import { openAuthTab } from 'react-native-android-authtab';

const authUrl = new URL('https://auth.example.com/authorize');
authUrl.searchParams.set('client_id', 'your-client-id');
authUrl.searchParams.set('redirect_uri', 'myapp-auth://callback');
authUrl.searchParams.set('response_type', 'code');
authUrl.searchParams.set('scope', 'openid profile email');
authUrl.searchParams.set('state', 'random-state-string');

try {
  const result = await openAuthTab(authUrl.toString(), 'myapp-auth');
  // Parse the result URI to extract the authorization code
  const resultUrl = new URL(result.resultUri);
  const code = resultUrl.searchParams.get('code');
  console.log('Authorization code:', code);
} catch (error) {
  if (error.code === 'AUTH_CANCELED') {
    console.log('User canceled the authentication');
  }
}

Function Parameters

openAuthTab(url, redirectScheme)

Parameter Type Required Description
url string Yes The authentication URL to navigate to
redirectScheme string No The custom URI scheme for the redirect (e.g., "myapp-auth")

Return Value

On success, returns a promise that resolves with:

  • resultCode (number): The result code from the auth flow
  • resultUri (string): The redirect URI containing auth parameters

Error Codes

The promise may reject with the following error codes:

Error Code Description
AUTH_CANCELED User canceled the auth flow
AUTH_VERIFICATION_FAILED HTTPS redirect verification failed
AUTH_VERIFICATION_TIMED_OUT HTTPS redirect verification timed out
AUTH_LAUNCH_ERROR Failed to launch the auth tab
NO_ACTIVITY MainActivity is not available
NO_LAUNCHER AuthTab launcher is not registered

MainActivity Setup

Your MainActivity must register the auth tab launcher. Add the following to your MainActivity.kt:

import androidx.activity.result.ActivityResultLauncher
import android.content.Intent
import com.authtab.AuthTabModule

class MainActivity : ReactActivity() {
    var authTabLauncher: ActivityResultLauncher<Intent>? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        authTabLauncher = AuthTabModule.registerLauncher(this)
    }
}

Requirements

  • Android device with Chrome or compatible browser
  • React Native 0.60+
  • androidx.browser:browser:1.9.0 or higher

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages