Skip to main content

Scute React Native Integration Guide

Installation

First, install the required packages. The @scute/react-hooks package provides React hooks for authentication, while react-native-webview is needed for OAuth flows that require opening external authentication pages within your app.
For Expo projects, you might also need to install the WebView package through Expo’s CLI to ensure proper native module linking:

Setup

1. Initialize Scute Client

Create a client instance in scute.ts. This client will handle all authentication operations and API calls to your Scute backend. The configuration requires your app ID and base URL, which should be stored as environment variables for security.

2. Wrap Your App with AuthProvider

In your root layout component (_layout.tsx), wrap your entire app with the AuthContextProvider. This provider makes the authentication state and client instance available to all components in your app through React Context, allowing you to access user data and authentication methods anywhere in your component tree.

Complete Login Form Implementation

State Management

Set up the necessary state variables to manage the authentication flow. The identifier stores the user’s email or phone number, otp holds the verification code, showOtpForm controls which form is displayed, and oAuthUrl manages the OAuth WebView state. The SCUTE_MAGIC_PARAM is used to detect successful OAuth callbacks.

Email/Phone Login Form

This is the initial login form that users see when they’re not authenticated. It provides two authentication options: email/phone with OTP verification, and Google OAuth. The form only renders when neither the OTP form nor OAuth WebView is active. When users enter their identifier and tap “Sign in”, it sends an OTP to their email or phone and transitions to the verification form.

OTP Verification Form

This form appears after users request an OTP and allows them to enter the verification code they received. The numeric keyboard type makes it easier for users to input the code. When verification succeeds, the app signs in the user with the returned authentication payload and navigates to the profile screen. The back button allows users to return to the initial login form if needed.

OAuth WebView Implementation

WebView Configuration

Configure platform-specific user agents to ensure OAuth providers recognize your app as a legitimate mobile browser. Different OAuth providers may have different requirements or behaviors based on the user agent string, so using platform-appropriate values helps avoid authentication issues and improves compatibility.

Complete WebView Component

This WebView component handles the OAuth authentication flow by loading the OAuth provider’s authentication page. It monitors URL changes to detect when the OAuth flow completes and returns to your app with an authorization code. The component includes comprehensive error handling, cookie management for session persistence, and proper security settings. When the OAuth callback is detected (via the magic parameter), it verifies the token and signs in the user automatically.

Key WebView Properties Explained

Authentication State Management

Using Authentication Hooks

Use the useAuth hook to access the current authentication state and user information throughout your app. This hook provides reactive updates when the authentication state changes, automatically re-rendering components when users sign in or out. The example shows how to implement route protection by checking the session status and redirecting unauthenticated users to the login screen.

Environment Configuration

Create a .env file in your project root to store your Scute configuration securely. The EXPO_PUBLIC_ prefix makes these variables available in your React Native code while keeping them separate from your source code. Replace the placeholder values with your actual Scute app ID and the URL of your Scute backend instance.

Important Notes

  1. WebView Security: The WebView configuration includes security settings for OAuth flows
  2. URL Monitoring: onNavigationStateChange detects the OAuth callback with the magic parameter
  3. Error Handling: Implement proper error handling for network issues and authentication failures
  4. User Agent: Custom user agents improve compatibility with OAuth providers
  5. State Management: Use React state to manage form visibility and authentication flow
This implementation provides a complete authentication system with both email/phone OTP and OAuth support using React Native WebView.