2026-03-26 22:20:33 +00:00
|
|
|
const API_BASE = process.env.NEXT_PUBLIC_API_URL || 'http://45.93.137.91/api';
|
|
|
|
|
|
|
|
|
|
async function apiFetch(endpoint, options = {}) {
|
|
|
|
|
const token = typeof window !== 'undefined' ? localStorage.getItem('token') : null;
|
|
|
|
|
|
|
|
|
|
const headers = {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
...(token && { Authorization: `Bearer ${token}` }),
|
|
|
|
|
...options.headers,
|
|
|
|
|
};
|
|
|
|
|
|
Implement login with email/phone + OTP verification flow
Login page:
- Email/phone tabs with auto-detect from input
- Calls LogInWithEmail or LogInWithPhoneNumber API
- On 206 (Partial Content): shows OTP step, sends OTP, then verifies
- On 200: stores JWT in localStorage, decodes user info
- OTP step with resend button and back navigation
- Console logs throughout all auth flows
API client:
- Added authFetch() for raw status code handling (200/206)
- Added loginWithEmail, loginWithPhone, sendEmailOTP, sendPhoneOTP,
verifyEmail, verifyPhone, isEmail, isPhoneNumber
- apiFetch now accepts 206 as non-error
2026-03-26 23:56:18 +00:00
|
|
|
console.log('[API] Request:', `${API_BASE}${endpoint}`, options.method || 'GET');
|
|
|
|
|
|
2026-03-26 22:20:33 +00:00
|
|
|
const res = await fetch(`${API_BASE}${endpoint}`, {
|
|
|
|
|
...options,
|
|
|
|
|
headers,
|
|
|
|
|
});
|
|
|
|
|
|
Implement login with email/phone + OTP verification flow
Login page:
- Email/phone tabs with auto-detect from input
- Calls LogInWithEmail or LogInWithPhoneNumber API
- On 206 (Partial Content): shows OTP step, sends OTP, then verifies
- On 200: stores JWT in localStorage, decodes user info
- OTP step with resend button and back navigation
- Console logs throughout all auth flows
API client:
- Added authFetch() for raw status code handling (200/206)
- Added loginWithEmail, loginWithPhone, sendEmailOTP, sendPhoneOTP,
verifyEmail, verifyPhone, isEmail, isPhoneNumber
- apiFetch now accepts 206 as non-error
2026-03-26 23:56:18 +00:00
|
|
|
console.log('[API] Response:', res.status, endpoint);
|
|
|
|
|
|
|
|
|
|
if (!res.ok && res.status !== 206) {
|
2026-03-26 22:20:33 +00:00
|
|
|
const text = await res.text().catch(() => '');
|
Implement login with email/phone + OTP verification flow
Login page:
- Email/phone tabs with auto-detect from input
- Calls LogInWithEmail or LogInWithPhoneNumber API
- On 206 (Partial Content): shows OTP step, sends OTP, then verifies
- On 200: stores JWT in localStorage, decodes user info
- OTP step with resend button and back navigation
- Console logs throughout all auth flows
API client:
- Added authFetch() for raw status code handling (200/206)
- Added loginWithEmail, loginWithPhone, sendEmailOTP, sendPhoneOTP,
verifyEmail, verifyPhone, isEmail, isPhoneNumber
- apiFetch now accepts 206 as non-error
2026-03-26 23:56:18 +00:00
|
|
|
console.error('[API] Error:', res.status, text);
|
2026-03-26 22:20:33 +00:00
|
|
|
throw new Error(`API ${res.status}: ${text || res.statusText}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const text = await res.text();
|
2026-03-26 22:46:57 +00:00
|
|
|
if (!text) return null;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const json = JSON.parse(text);
|
|
|
|
|
if (json && typeof json === 'object' && 'data' in json) {
|
|
|
|
|
return json.data;
|
|
|
|
|
}
|
|
|
|
|
return json;
|
|
|
|
|
} catch {
|
|
|
|
|
return text;
|
|
|
|
|
}
|
2026-03-26 22:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
Implement login with email/phone + OTP verification flow
Login page:
- Email/phone tabs with auto-detect from input
- Calls LogInWithEmail or LogInWithPhoneNumber API
- On 206 (Partial Content): shows OTP step, sends OTP, then verifies
- On 200: stores JWT in localStorage, decodes user info
- OTP step with resend button and back navigation
- Console logs throughout all auth flows
API client:
- Added authFetch() for raw status code handling (200/206)
- Added loginWithEmail, loginWithPhone, sendEmailOTP, sendPhoneOTP,
verifyEmail, verifyPhone, isEmail, isPhoneNumber
- apiFetch now accepts 206 as non-error
2026-03-26 23:56:18 +00:00
|
|
|
// Raw fetch for auth (no token, returns full response for status code handling)
|
|
|
|
|
async function authFetch(endpoint, body) {
|
|
|
|
|
console.log('[Auth] Request:', `${API_BASE}${endpoint}`);
|
|
|
|
|
|
|
|
|
|
const res = await fetch(`${API_BASE}${endpoint}`, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
body: JSON.stringify(body),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
console.log('[Auth] Response status:', res.status, endpoint);
|
|
|
|
|
|
|
|
|
|
const text = await res.text();
|
|
|
|
|
let data = null;
|
|
|
|
|
try {
|
|
|
|
|
data = text ? JSON.parse(text) : null;
|
|
|
|
|
if (data && typeof data === 'object' && 'data' in data) {
|
|
|
|
|
data = data.data;
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
data = text;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return { status: res.status, data, ok: res.ok || res.status === 206 };
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-26 22:20:33 +00:00
|
|
|
// ─── Rent Properties ───
|
|
|
|
|
|
|
|
|
|
export async function getRentProperties() {
|
2026-03-26 23:27:28 +00:00
|
|
|
return apiFetch('/RentProperties/GetRentProperties');
|
2026-03-26 22:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getRentProperty(id) {
|
2026-03-26 23:27:28 +00:00
|
|
|
const items = await apiFetch('/RentProperties/GetRentProperties');
|
|
|
|
|
if (!Array.isArray(items)) return items;
|
|
|
|
|
return items.find(p => p.id == id) || items[0];
|
2026-03-26 22:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getRentPropertyLocations(params = {}) {
|
|
|
|
|
const qs = new URLSearchParams();
|
|
|
|
|
if (params.maxOffset != null) qs.set('maxOffset', params.maxOffset);
|
|
|
|
|
if (params.minOffset != null) qs.set('minOffset', params.minOffset);
|
|
|
|
|
const query = qs.toString();
|
2026-03-26 22:46:57 +00:00
|
|
|
return apiFetch(`/RentProperties/GetRentPropertiesLocations${query ? `?${query}` : ''}`);
|
2026-03-26 22:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ─── Sale Properties ───
|
|
|
|
|
|
|
|
|
|
export async function getSaleProperties() {
|
2026-03-26 23:27:28 +00:00
|
|
|
return apiFetch('/SaleProperties/GetSaleProperties');
|
2026-03-26 22:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getSaleProperty(id) {
|
2026-03-26 23:27:28 +00:00
|
|
|
const items = await apiFetch('/SaleProperties/GetSaleProperties');
|
|
|
|
|
if (!Array.isArray(items)) return items;
|
|
|
|
|
return items.find(p => p.id == id) || items[0];
|
2026-03-26 22:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ─── Properties (generic) ───
|
|
|
|
|
|
|
|
|
|
export async function getProperty(id) {
|
|
|
|
|
return apiFetch(`/Properties/Get/${id}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ─── Recommendations ───
|
|
|
|
|
|
|
|
|
|
export async function getRecommendations() {
|
2026-03-26 22:46:57 +00:00
|
|
|
return apiFetch('/Recommendations/GetRecommendations');
|
2026-03-26 22:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getTopRecommendations(count = 10) {
|
2026-03-26 22:46:57 +00:00
|
|
|
return apiFetch(`/Recommendations/GetTopRecommendations?count=${count}`);
|
2026-03-26 22:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ─── Reservations ───
|
|
|
|
|
|
|
|
|
|
export async function getReservations() {
|
2026-03-26 22:46:57 +00:00
|
|
|
return apiFetch('/Reservations/GetReservations');
|
2026-03-26 22:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getReservation(id) {
|
2026-03-26 22:46:57 +00:00
|
|
|
return apiFetch(`/Reservations/GetReservation?id=${id}`);
|
2026-03-26 22:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function checkAvailability(propertyId, fromDate = null, toDate = null) {
|
|
|
|
|
const qs = new URLSearchParams();
|
|
|
|
|
if (fromDate) qs.set('fromDate', fromDate);
|
|
|
|
|
if (toDate) qs.set('toDate', toDate);
|
|
|
|
|
const query = qs.toString();
|
2026-03-26 22:46:57 +00:00
|
|
|
return apiFetch(`/Reservations/GetAvailable/${propertyId}${query ? `?${query}` : ''}`);
|
2026-03-26 22:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function bookReservation(data) {
|
2026-03-26 22:46:57 +00:00
|
|
|
return apiFetch('/Reservations/Book', {
|
2026-03-26 22:20:33 +00:00
|
|
|
method: 'POST',
|
|
|
|
|
body: JSON.stringify(data),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ─── Terms ───
|
|
|
|
|
|
|
|
|
|
export async function getTerms() {
|
2026-03-26 22:46:57 +00:00
|
|
|
return apiFetch('/Terms/GetTerms');
|
2026-03-26 22:20:33 +00:00
|
|
|
}
|
Implement login with email/phone + OTP verification flow
Login page:
- Email/phone tabs with auto-detect from input
- Calls LogInWithEmail or LogInWithPhoneNumber API
- On 206 (Partial Content): shows OTP step, sends OTP, then verifies
- On 200: stores JWT in localStorage, decodes user info
- OTP step with resend button and back navigation
- Console logs throughout all auth flows
API client:
- Added authFetch() for raw status code handling (200/206)
- Added loginWithEmail, loginWithPhone, sendEmailOTP, sendPhoneOTP,
verifyEmail, verifyPhone, isEmail, isPhoneNumber
- apiFetch now accepts 206 as non-error
2026-03-26 23:56:18 +00:00
|
|
|
|
|
|
|
|
// ─── Auth ───
|
|
|
|
|
|
|
|
|
|
export async function loginWithEmail(credential, password) {
|
|
|
|
|
return authFetch('/Auth/LogInWithEmail', {
|
|
|
|
|
credential,
|
|
|
|
|
password,
|
|
|
|
|
device: 0,
|
|
|
|
|
appVersion: '1.0',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function loginWithPhone(credential, password) {
|
|
|
|
|
return authFetch('/Auth/LogInWithPhoneNumber', {
|
|
|
|
|
credential,
|
|
|
|
|
password,
|
|
|
|
|
device: 0,
|
|
|
|
|
appVersion: '1.0',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function sendEmailOTP() {
|
|
|
|
|
console.log('[Auth] Sending email OTP...');
|
|
|
|
|
return apiFetch('/Auth/SendEmailOTP', { method: 'POST' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function sendPhoneOTP() {
|
|
|
|
|
console.log('[Auth] Sending phone OTP...');
|
|
|
|
|
return apiFetch('/Auth/SendPhoneNumberOTP', { method: 'POST' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function verifyEmail(code) {
|
|
|
|
|
console.log('[Auth] Verifying email with code:', code);
|
|
|
|
|
return authFetch(`/Auth/VerifyEmail?code=${encodeURIComponent(code)}`, {});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function verifyPhone(code) {
|
|
|
|
|
console.log('[Auth] Verifying phone with code:', code);
|
|
|
|
|
return authFetch(`/Auth/VerifyPhoneNumber?code=${encodeURIComponent(code)}`, {});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Helpers
|
|
|
|
|
export function isEmail(value) {
|
|
|
|
|
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function isPhoneNumber(value) {
|
|
|
|
|
return /^\+?\d{7,15}$/.test(value.replace(/[\s\-()]/g, ''));
|
|
|
|
|
}
|