Skip to content

Token-based authentication utilities for React Native / Expo apps

Notifications You must be signed in to change notification settings

paramsingh/vayu-auth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

vayu-auth

Token-based authentication utilities for React Native / Expo apps.

  • JWT token storage with automatic refresh
  • React Context provider for auth state
  • Configurable API endpoints
  • Works with vayu-storage for secure token storage

Installation

npm install vayu-auth vayu-storage expo-secure-store

Usage

1. Configure the AuthProvider

import { AuthProvider } from 'vayu-auth';

const authConfig = {
  apiBaseUrl: 'https://api.myapp.com',
  endpoints: {
    refresh: '/api/auth/refresh',
    logout: '/api/auth/logout',
    me: '/api/users/me',
  },
  // Optional: transform API response to your user type
  transformUser: (data) => ({
    id: data.id,
    email: data.email,
    name: data.first_name + ' ' + data.last_name,
  }),
};

export default function App() {
  return (
    <AuthProvider config={authConfig}>
      <YourApp />
    </AuthProvider>
  );
}

2. Use the auth hook

import { useAuth } from 'vayu-auth';

function ProfileScreen() {
  const { authState, logout } = useAuth();

  if (!authState.isAuthenticated) {
    return <LoginScreen />;
  }

  return (
    <View>
      <Text>Welcome, {authState.user?.name}</Text>
      <Button onPress={logout} title="Logout" />
    </View>
  );
}

3. Login after OAuth/authentication

import { useAuth } from 'vayu-auth';

function LoginScreen() {
  const { login } = useAuth();

  const handleLogin = async () => {
    // After your OAuth flow completes...
    const { token, expiresAt, sessionId, user } = await myOAuthFlow();

    await login(token, expiresAt, sessionId, user);
  };

  return <Button onPress={handleLogin} title="Login" />;
}

Using the auth service directly

import { createAuthService } from 'vayu-auth';

const authService = createAuthService({
  apiBaseUrl: 'https://api.myapp.com',
  endpoints: {
    refresh: '/api/auth/refresh',
    logout: '/api/auth/logout',
    me: '/api/users/me',
  },
});

// Get token for API calls
const token = await authService.getAuthToken();

// Check auth status
const isLoggedIn = await authService.isAuthenticated();

Configuration Options

interface AuthConfig {
  // Base URL for auth API endpoints
  apiBaseUrl: string;

  // Endpoint paths (relative to apiBaseUrl)
  endpoints: {
    refresh: string;  // POST - refresh token
    logout: string;   // POST - logout
    me: string;       // GET - fetch current user
  };

  // Storage key prefix (default: 'auth')
  storagePrefix?: string;

  // Seconds before expiry to trigger refresh (default: 60)
  refreshGracePeriod?: number;

  // Transform user data from API response
  transformUser?: (data: any) => any;
}

Expected API Response Formats

Refresh endpoint (POST)

{
  "jwt_token": "new.jwt.token",
  "expires_at": 1234567890,
  "session_id": "optional-session-id"
}

Me endpoint (GET)

Returns user data (transformed by transformUser if provided).

License

MIT

About

Token-based authentication utilities for React Native / Expo apps

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published