Skip to main content

Overview

Trazea uses Supabase authentication with role-based access control (RBAC). The authentication system provides hooks for login, logout, password management, and OAuth providers.

useLogin

Core authentication hook for managing user sessions.
import { useLogin } from '@/features/auth-login';

const { 
  login, 
  logout, 
  resetPassword, 
  updatePassword,
  loginWithGoogle,
  isLoading,
  isGoogleLoading 
} = useLogin();

Methods

login
(email: string, password: string) => Promise<void>
Authenticates user with email and password.
await login('[email protected]', 'password123');
logout
() => Promise<void>
Signs out the current user and clears session data.
await logout();
resetPassword
(email: string) => Promise<void>
Sends password recovery email to user.
await resetPassword('[email protected]');
updatePassword
(password: string) => Promise<void>
Updates user password (requires authenticated session).
await updatePassword('newSecurePassword123');
loginWithGoogle
() => Promise<void>
Initiates OAuth login flow with Google provider.
await loginWithGoogle();

State

isLoading
boolean
Loading state for email/password operations
isGoogleLoading
boolean
Loading state for Google OAuth operations

useUserStore

Zustand store for managing user session and permissions.
import { useUserStore } from '@/entities/user';

const sessionData = useUserStore((state) => state.sessionData);
const currentLocation = useUserStore((state) => state.currentLocation);
const hasPermission = useUserStore((state) => state.hasPermission);

Store State

sessionData
SessionData | null
Current user session including role and permissions
currentLocation
UserLocation | null
Selected warehouse/location context
isAuthenticated
boolean
Authentication status

Store Actions

setSessionData
(data: SessionData | null) => void
Sets user session data
setCurrentLocation
(location: UserLocation | null) => void
Sets active warehouse location
clearUser
() => void
Clears all user session data
hasPermission
(permission: string) => boolean
Checks if user has specific permission
hasRole
(roleName: string) => boolean
Checks if user has specific role
checkMenuPermission
(menu: string, permission: string) => boolean
Checks menu-specific permissions
const canEdit = checkMenuPermission('inventario', 'edit_register');
isUserApproved
() => boolean
Returns whether user is approved
isUserActive
() => boolean
Returns whether user is active

useSupabaseAuthListener

Auth state change listener hook.
import { useSupabaseAuthListener } from '@/entities/user';

// Initialize in app root
useSupabaseAuthListener();
Handles:
  • Session initialization on page load
  • Password recovery events
  • Sign out events
  • Token refresh

Types

SessionData

interface SessionData {
  user: UserData;
  locations: UserLocation[] | null;
}

interface UserData {
  id: string;
  email: string;
  nombre?: string;
  activo: boolean;
  aprobado: boolean;
  role: Role;
  locations?: UserLocation[];
}

interface UserLocation {
  id_localizacion: string;
  nombre: string;
}

AppPermissions

interface AppPermissions {
  menu: {
    registros: MenuPermissions;
    repuestos: MenuPermissions;
    solicitudes: MenuPermissions;
    inventario: MenuPermissions;
  };
  inventory?: {
    edit_product: boolean;
    create_product: boolean;
    delete_product: boolean;
    view_cost_price: boolean;
  };
}

interface MenuPermissions {
  show_view: boolean;
  edit_register?: boolean;
  create_register?: boolean;
}

Example Usage

import { useLogin } from '@/features/auth-login';
import { useUserStore } from '@/entities/user';

function LoginPage() {
  const { login, isLoading } = useLogin();
  const canViewInventory = useUserStore((state) => 
    state.checkMenuPermission('inventario', 'show_view')
  );
  
  const handleLogin = async (email: string, password: string) => {
    await login(email, password);
    if (canViewInventory) {
      navigate('/inventario');
    }
  };
  
  return (
    <form onSubmit={(e) => {
      e.preventDefault();
      handleLogin(email, password);
    }}>
      {/* Form fields */}
    </form>
  );
}

Build docs developers (and LLMs) love