- App.tsx: full navigation (Auth stack + Main tabs with 5 screens) - Auth: LoginScreen, RegisterScreen, ForgotPasswordScreen - HomeScreen: dashboard with IoT metrics, weather widget, alerts, quick actions, sensors - MapScreen: interactive map with layer toggles (6 layers) - MarketplaceScreen: categories (6), products (5), search - ChatScreen: AI chat with quick prompts (4), bot responses - ProfileScreen: user info, stats, menu (9 items), logout - AlertsScreen: alert list with severity, acknowledge - SensorsScreen: sensor list with type filters (6 types), search - ZonesScreen: zone cards with stats - SettingsScreen: language picker (FR/EN/ES/DE), privacy, about - Stores: iotStore (sensors, zones, alerts), notificationStore, uiStore + i18n - Hooks: useSensors, useAlerts, useNotifications, useLocation - Components: Card, Button, LoadingSpinner, ErrorBoundary, Header - Services: iotService, notificationService (with axios API client) - Utils: formatters (temp, AQI, noise, dates), validators (email, password, IBAN) - Theme: colors.ts with full design system (Blue Ocean palette) - Ditto: fixed MongoDB connection, new JWT secrets, official gateway image
97 lines
2.9 KiB
TypeScript
97 lines
2.9 KiB
TypeScript
import { useAuthStore } from '../stores/authStore';
|
|
import type { LoginResponse, RegisterData, User } from '../stores/authStore';
|
|
|
|
const API_BASE_URL = process.env.EXPO_PUBLIC_API_URL ?? 'http://localhost:8000';
|
|
|
|
export const authService = {
|
|
async login(email: string, password: string): Promise<LoginResponse> {
|
|
const res = await fetch(`${API_BASE_URL}/api/auth/login`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ email, password }),
|
|
});
|
|
if (!res.ok) {
|
|
const body = await res.json().catch(() => ({}));
|
|
throw new Error(body.message ?? body.error ?? `Login failed (${res.status})`);
|
|
}
|
|
const data: LoginResponse = await res.json();
|
|
// Sync with store
|
|
useAuthStore.setState({
|
|
user: data.user,
|
|
accessToken: data.accessToken,
|
|
refreshToken: data.refreshToken,
|
|
isAuthenticated: true,
|
|
});
|
|
return data;
|
|
},
|
|
|
|
async register(data: RegisterData): Promise<LoginResponse> {
|
|
const res = await fetch(`${API_BASE_URL}/api/auth/register`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(data),
|
|
});
|
|
if (!res.ok) {
|
|
const body = await res.json().catch(() => ({}));
|
|
throw new Error(body.message ?? body.error ?? `Registration failed (${res.status})`);
|
|
}
|
|
const result: LoginResponse = await res.json();
|
|
useAuthStore.setState({
|
|
user: result.user,
|
|
accessToken: result.accessToken,
|
|
refreshToken: result.refreshToken,
|
|
isAuthenticated: true,
|
|
});
|
|
return result;
|
|
},
|
|
|
|
async logout(): Promise<void> {
|
|
const { accessToken } = useAuthStore.getState();
|
|
if (accessToken) {
|
|
try {
|
|
await fetch(`${API_BASE_URL}/api/auth/logout`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
Authorization: `Bearer ${accessToken}`,
|
|
},
|
|
});
|
|
} catch { /* ignore */ }
|
|
}
|
|
useAuthStore.setState({
|
|
user: null,
|
|
accessToken: null,
|
|
refreshToken: null,
|
|
isAuthenticated: false,
|
|
});
|
|
},
|
|
|
|
async refreshToken(): Promise<LoginResponse | null> {
|
|
const { refreshToken } = useAuthStore.getState();
|
|
if (!refreshToken) return null;
|
|
const res = await fetch(`${API_BASE_URL}/api/auth/refresh`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ refreshToken }),
|
|
});
|
|
if (!res.ok) {
|
|
useAuthStore.setState({
|
|
user: null,
|
|
accessToken: null,
|
|
refreshToken: null,
|
|
isAuthenticated: false,
|
|
error: 'Session expired',
|
|
});
|
|
return null;
|
|
}
|
|
const data: LoginResponse = await res.json();
|
|
useAuthStore.setState({
|
|
user: data.user,
|
|
accessToken: data.accessToken,
|
|
refreshToken: data.refreshToken,
|
|
isAuthenticated: true,
|
|
});
|
|
return data;
|
|
},
|
|
};
|