> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mobile-starter.amisi.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Auth

> Implement adapter-based authentication with providers and hooks

# @amisi/auth

`@amisi/auth` provides an adapter-based authentication API.

## Adapter model

* Adapters implement a shared `AuthAdapter` contract.
* The active adapter is selected at runtime via `initializeAuth({ adapter, options })`.

## Initialize

Initialize once at app startup:

```ts theme={null}
import { initializeAuth } from '@amisi/auth';

await initializeAuth({
  adapter: 'firebase',
  options: {},
});
```

## AuthProvider

Wrap your app with the `AuthProvider` to provide authentication context throughout your app:

```tsx theme={null}
import { AuthProvider } from '@amisi/auth';

export default function App() {
  return (
    <AuthProvider
      config={{
        adapter: 'firebase',
        options: {
          // Firebase configuration
        },
      }}
    >
      <YourApp />
    </AuthProvider>
  );
}
```

The provider automatically:

* Initializes the auth adapter
* Listens for session changes
* Provides auth state to all child components
* Handles sign in, sign up, and sign out operations

## Hook usage

### useAuthContext

Access authentication state and operations from the provider:

```tsx theme={null}
import { useAuthContext } from '@amisi/auth';

export function ProfileScreen() {
  const { session, user, loading, isAuthenticated, signOut } = useAuthContext();

  if (loading) {
    return <LoadingSpinner />;
  }

  if (!isAuthenticated) {
    return <Navigate to="/sign-in" />;
  }

  return (
    <View>
      <Text>Welcome, {user?.displayName || user?.email}</Text>
      <Button onPress={signOut}>Sign Out</Button>
    </View>
  );
}
```

### Available properties

* `adapter: AuthAdapter | null` - Current auth adapter
* `session: AuthSession | null` - Current session
* `user: AuthUser | null` - Current user
* `loading: boolean` - Loading state
* `isAuthenticated: boolean` - Whether user is authenticated
* `signIn: (params) => Promise<AuthSession | null>` - Sign in method
* `signUp: (params) => Promise<AuthSession | null>` - Sign up method
* `signOut: () => Promise<void>` - Sign out method
* `getAccessToken: () => Promise<string | null>` - Get access token
* `refresh: () => Promise<void>` - Refresh session

## Hook alias usage

`useAuth` is an alias of `useAuthContext` and must be used inside `AuthProvider`:

```tsx theme={null}
import { useAuth } from '@amisi/auth';

export function Screen() {
  const { loading, session, signIn, signUp, signOut } = useAuth();

  return null;
}
```

## Sign in

```ts theme={null}
await signIn({
  type: 'email_password',
  email: 'test@example.com',
  password: 'password',
});
```

## Sign out

```ts theme={null}
await signOut();
```
