A React Native module for Android Auth Tab authentication using the new AuthTabIntent API.
npm install react-native-android-authtab
# or
yarn add react-native-android-authtabimport { 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);
}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');
}
}| 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") |
On success, returns a promise that resolves with:
resultCode(number): The result code from the auth flowresultUri(string): The redirect URI containing auth parameters
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 |
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)
}
}- Android device with Chrome or compatible browser
- React Native 0.60+
androidx.browser:browser:1.9.0or higher