Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/Jesus-Puertos/h-ayuntamiento/llms.txt

Use this file to discover all available pages before exploring further.

Overview

The src/lib/supabase.ts file exports 30+ functions for interacting with the Supabase database and authentication system. All functions are fully typed with TypeScript.
These functions use the Supabase JavaScript client and handle errors consistently with { data, error } return values.

Client Initialization

import { createClient } from '@supabase/supabase-js';

const supabaseUrl = import.meta.env.PUBLIC_SUPABASE_URL || '';
const supabaseAnonKey = import.meta.env.PUBLIC_SUPABASE_ANON_KEY || '';

export const supabase = createClient(supabaseUrl, supabaseAnonKey);

Authentication Functions

getCurrentUser()

Get the currently authenticated user.
function getCurrentUser(): Promise<User | null>
Example:
const user = await getCurrentUser();
if (user) {
  console.log('Logged in as:', user.email);
}

signInWithEmail()

Sign in with email and password.
function signInWithEmail(
  email: string,
  password: string
): Promise<{
  data: AuthResponse | null;
  error: AuthError | null;
}>
email
string
required
User’s email address
password
string
required
User’s password
Example:
const { data, error } = await signInWithEmail('user@example.com', 'password123');
if (error) {
  console.error('Login failed:', error.message);
} else {
  console.log('Logged in:', data.user);
}

signUpWithEmail()

Create a new account with email and password.
function signUpWithEmail(
  email: string,
  password: string,
  name?: string
): Promise<{
  data: AuthResponse | null;
  error: AuthError | null;
}>
email
string
required
User’s email address
password
string
required
User’s password (min 6 characters)
name
string
User’s display name (defaults to email prefix)
Example:
const { data, error } = await signUpWithEmail(
  'newuser@example.com',
  'securePassword123',
  'Juan Pérez'
);

signInWithGoogle()

Initiate Google OAuth sign-in flow.
function signInWithGoogle(
  redirectPath?: string
): Promise<{
  data: AuthResponse | null;
  error: AuthError | null;
}>
redirectPath
string
Path to redirect after authentication. Default: /turismo?onboarding=1
Example:
const { data, error } = await signInWithGoogle('/perfil');
if (error) {
  console.error('Google sign-in failed:', error);
}

signOut()

Sign out the current user.
function signOut(): Promise<{
  error: AuthError | null;
}>
Example:
const { error } = await signOut();
if (!error) {
  window.location.href = '/';
}

User Profile Functions

getUserProfile()

Get user profile by ID.
function getUserProfile(
  userId: string
): Promise<{
  data: UserProfile | null;
  error: PostgrestError | null;
}>
userId
string
required
UUID of the user
Example:
const { data: profile, error } = await getUserProfile(user.id);
if (profile) {
  console.log('User:', profile.full_name);
}

upsertUserProfile()

Create or update user profile.
function upsertUserProfile(
  profile: Partial<UserProfile> & { id: string }
): Promise<{
  data: UserProfile | null;
  error: PostgrestError | null;
}>
profile
Partial<UserProfile> & { id: string }
required
Profile data (must include id)
Example:
const { data, error } = await upsertUserProfile({
  id: user.id,
  full_name: 'Juan Pérez',
  email: user.email,
  avatar_url: user.user_metadata.avatar_url,
  provider: 'google'
});

markOnboardingCompleted()

Mark user’s onboarding as completed.
function markOnboardingCompleted(
  userId: string
): Promise<{
  data: UserProfile | null;
  error: PostgrestError | null;
}>
Example:
await markOnboardingCompleted(user.id);

isOnboardingCompleted()

Check if user has completed onboarding.
function isOnboardingCompleted(userId: string): Promise<boolean>
Example:
const completed = await isOnboardingCompleted(user.id);
if (!completed) {
  window.location.href = '/turismo?onboarding=1';
}

getFullProfile()

Get complete user profile with all related data.
function getFullProfile(userId: string): Promise<{
  profile: UserProfile | null;
  preferences: UserPreferences | null;
  badges: UserBadge[];
  routes: UserRoute[];
  favorites: UserFavorite[];
}>
Returns: Object containing profile, preferences, badges, routes, and favorites. Example:
const fullProfile = await getFullProfile(user.id);
console.log('Badges:', fullProfile.badges.length);
console.log('Routes:', fullProfile.routes.length);
console.log('Favorites:', fullProfile.favorites.length);

Preferences Functions

saveUserPreferences()

Save or update user preferences (upsert).
function saveUserPreferences(
  preferences: UserPreferences
): Promise<{
  data: UserPreferences | null;
  error: PostgrestError | null;
}>
preferences
UserPreferences
required
User preferences object. See UserPreferences
Example:
const { data, error } = await saveUserPreferences({
  user_id: user.id,
  experiencia: ['aventura', 'naturaleza'],
  duracion: 'dia-completo',
  dificultad: 'moderado',
  grupo: 'familia',
  intereses: ['cascadas', 'miradores', 'senderismo']
});

getUserPreferences()

Get user preferences.
function getUserPreferences(
  userId: string
): Promise<{
  data: UserPreferences | null;
  error: PostgrestError | null;
}>
Example:
const { data: prefs, error } = await getUserPreferences(user.id);
if (prefs) {
  console.log('User prefers:', prefs.experiencia);
}

Route Functions

saveUserRoute()

Create a new user route.
function saveUserRoute(
  route: Omit<UserRoute, 'id' | 'created_at'>
): Promise<{
  data: UserRoute | null;
  error: PostgrestError | null;
}>
route
Omit<UserRoute, 'id' | 'created_at'>
required
Route data (id and created_at are auto-generated)
Example:
const { data, error } = await saveUserRoute({
  user_id: user.id,
  route_name: 'Mi Aventura',
  atractivos: ['la-pergola', 'cascada-de-atlahuitzia'],
  ticket_url: 'https://zongolica.gob.mx/ruta/abc123',
  share_code: 'abc123',
  badges: ['explorador']
});

getUserRoute()

Get a route by share code (for sharing).
function getUserRoute(
  shareCode: string
): Promise<{
  data: UserRoute | null;
  error: PostgrestError | null;
}>
shareCode
string
required
Unique share code for the route
Example:
const { data: route, error } = await getUserRoute('abc123');
if (route) {
  console.log('Route:', route.route_name);
  console.log('Attractions:', route.atractivos);
}

getUserRoutes()

Get all routes created by a user.
function getUserRoutes(
  userId: string
): Promise<{
  data: UserRoute[] | null;
  error: PostgrestError | null;
}>
Example:
const { data: routes, error } = await getUserRoutes(user.id);
console.log('User has', routes?.length, 'routes');

getLatestUserRoute()

Get the most recent route created by a user.
function getLatestUserRoute(userId: string): Promise<UserRoute | null>
Example:
const latestRoute = await getLatestUserRoute(user.id);
if (latestRoute) {
  console.log('Latest:', latestRoute.route_name);
}

Badge Functions

unlockBadge()

Unlock a badge for a user.
function unlockBadge(
  userId: string,
  badgeType: string
): Promise<{
  data: UserBadge | null;
  error: PostgrestError | null;
}>
userId
string
required
UUID of the user
badgeType
string
required
Badge identifier (e.g., “aventurero”, “explorador”)
Example:
const { data, error } = await unlockBadge(user.id, 'aventurero');
if (!error) {
  console.log('Badge unlocked!');
}

getUserBadges()

Get all badges unlocked by a user.
function getUserBadges(
  userId: string
): Promise<{
  data: UserBadge[] | null;
  error: PostgrestError | null;
}>
Example:
const { data: badges, error } = await getUserBadges(user.id);
console.log('User has', badges?.length, 'badges');

hasBadge()

Check if user has a specific badge.
function hasBadge(
  userId: string,
  badgeType: string
): Promise<boolean>
Example:
const hasAventurero = await hasBadge(user.id, 'aventurero');
if (!hasAventurero) {
  // Show badge unlock animation
  await unlockBadge(user.id, 'aventurero');
}

Favorites Functions

getUserFavorites()

Get all user favorites.
function getUserFavorites(
  userId: string
): Promise<{
  data: UserFavorite[] | null;
  error: PostgrestError | null;
}>
Example:
const { data: favorites, error } = await getUserFavorites(user.id);
const favoriteSlugs = favorites?.map(f => f.atractivo_slug) || [];

addFavorite()

Add an attraction to favorites.
function addFavorite(
  userId: string,
  atractivoSlug: string
): Promise<{
  data: UserFavorite | null;
  error: PostgrestError | null;
}>
userId
string
required
UUID of the user
atractivoSlug
string
required
Slug of the attraction (e.g., “cascada-de-atlahuitzia”)
Example:
const { data, error } = await addFavorite(user.id, 'la-pergola');
if (!error) {
  console.log('Added to favorites!');
}

removeFavorite()

Remove an attraction from favorites.
function removeFavorite(
  userId: string,
  atractivoSlug: string
): Promise<{
  error: PostgrestError | null;
}>
Example:
const { error } = await removeFavorite(user.id, 'la-pergola');
if (!error) {
  console.log('Removed from favorites');
}

isFavorite()

Check if an attraction is favorited.
function isFavorite(
  userId: string,
  atractivoSlug: string
): Promise<boolean>
Example:
const isFav = await isFavorite(user.id, 'cascada-de-atlahuitzia');
setHeartIcon(isFav ? 'filled' : 'outline');

toggleFavorite()

Toggle favorite status (add if not favorited, remove if favorited).
function toggleFavorite(
  userId: string,
  atractivoSlug: string
): Promise<boolean>
Returns: true if now favorited, false if removed. Example:
const nowFavorited = await toggleFavorite(user.id, 'la-pergola');
if (nowFavorited) {
  showToast('Añadido a favoritos');
} else {
  showToast('Eliminado de favoritos');
}

getFavoritesCount()

Get total number of favorites for a user.
function getFavoritesCount(userId: string): Promise<number>
Example:
const count = await getFavoritesCount(user.id);
console.log('User has', count, 'favorites');

Complete Usage Example

Here’s a complete example showing authentication and data operations:
import { 
  signInWithGoogle, 
  getCurrentUser,
  getFullProfile,
  saveUserPreferences,
  toggleFavorite
} from '@/lib/supabase';

// 1. Sign in with Google
await signInWithGoogle();

// 2. Get current user
const user = await getCurrentUser();
if (!user) {
  throw new Error('Not authenticated');
}

// 3. Get full profile
const profile = await getFullProfile(user.id);
console.log('Profile:', profile);

// 4. Save preferences
if (!profile.preferences) {
  await saveUserPreferences({
    user_id: user.id,
    experiencia: ['aventura', 'naturaleza'],
    duracion: 'dia-completo',
    dificultad: 'moderado',
    grupo: 'familia',
    intereses: ['cascadas', 'miradores']
  });
}

// 5. Toggle favorite
const isFav = await toggleFavorite(user.id, 'la-pergola');
console.log('Is favorited:', isFav);

Error Handling

All database functions return { data, error } tuples:
const { data, error } = await getUserProfile(user.id);

if (error) {
  console.error('Database error:', error.message);
  // Handle error (show toast, redirect, etc.)
  return;
}

if (!data) {
  console.log('No profile found');
  return;
}

// Success - use data
console.log('Profile:', data);