fullauth / DOCS / React Native / Usage
GitHub

DOCUMENTATION / REACT NATIVE

React Native usage

Wrap the app in SessionProvider as shown in the installation guide. The helpers use the configured backend origin and store the mobile session token and CSRF token in AsyncStorage.

Sign in with credentials

Connect email and password to your app’s form values. The server-side credentials provider is responsible for verification.

screens/SignInButton.tsx

import { signIn } from '@fullauth/react-native';
import { Pressable, Text } from 'react-native';

const baseUrl = process.env.EXPO_PUBLIC_FULLAUTH_URL!;

export default function SignInButton({
  email,
  password,
}: {
  email: string;
  password: string;
}) {
  return (
    <Pressable
      onPress={() =>
        signIn({
          baseUrl,
          provider: 'credentials',
          credentials: { email, password },
        })
      }
    >
      <Text>Sign in</Text>
    </Pressable>
  );
}

Sign out

The React Native helper requires the backend baseUrl and removes the stored session token and CSRF token.

screens/SignOutButton.tsx

import { signOut } from '@fullauth/react-native';
import { Pressable, Text } from 'react-native';

const baseUrl = process.env.EXPO_PUBLIC_FULLAUTH_URL!;

export default function SignOutButton() {
  return (
    <Pressable onPress={() => signOut({ baseUrl })}>
      <Text>Sign out</Text>
    </Pressable>
  );
}

Read session state

screens/SessionStatus.tsx

import { useSession } from '@fullauth/react-native';
import { Text } from 'react-native';

export default function SessionStatus() {
  const { session, status } = useSession();

  return <Text>{session?.user?.email ?? status}</Text>;
}

Update session data

Updates are sent to the server and passed to callbacks.token. The application callback must validate and apply accepted values; the default callback does not merge arbitrary data.

screens/RenameSession.tsx

import { useSession } from '@fullauth/react-native';
import { Pressable, Text } from 'react-native';

export default function RenameSession() {
  const { update } = useSession();

  return (
    <Pressable onPress={() => update({ name: 'New name' })}>
      <Text>Update session</Text>
    </Pressable>
  );
}

Send authentication headers

authHeaders() returns the stored token and CSRF token. Forward them to a backend route that validates the session.

screens/profile.ts

import { authHeaders } from '@fullauth/react-native';

export async function loadPrivateProfile() {
  const baseUrl = process.env.EXPO_PUBLIC_FULLAUTH_URL!;
  const response = await fetch(
    baseUrl + '/api/profile',
    { headers: await authHeaders() },
  );
  return response.json();
}

Read the session in a Next.js route

The helper’s request overload reads the authentication headers in the current Next.js request context.

app/api/profile/route.ts

import { getServerSession } from '@fullauth/next/helpers';
import { NextResponse } from 'next/server';
import { authOptions } from '../auth/[...fullauth]/route';

export async function GET(request: Request) {
  const session = await getServerSession(authOptions.secret!, request);
  if (!session?.user) {
    return NextResponse.json({ message: 'Unauthorized' }, { status: 401 });
  }
  return NextResponse.json({ user: session.user });
}

Use with tRPC

This is a partial context example for an app that already uses tRPC. FullAuth does not include a tRPC adapter.

server/context.ts

import type { FetchCreateContextFnOptions } from '@trpc/server/adapters/fetch';
import { getServerSession } from '@fullauth/next/helpers';
import { authOptions } from '../api/auth/[...fullauth]/route';

export async function createContext(opts: FetchCreateContextFnOptions) {
  const session = await getServerSession(authOptions.secret!, opts.req);
  return { session };
}

export type Context = Awaited<ReturnType<typeof createContext>>;

Google OAuth in a React Native client is not established by the mobile callback URL alone. The included helper currently does not open or complete the OAuth redirect; see the provider limitations.