This document describes the authentication system implemented in the Urmet application.
The application uses a fake Magento API authentication flow that executes before the app mounts. This ensures that user information and authentication tokens are available throughout the application.
Data has been organized into separate files based on user type:
dummyData/particulierWithoutZeno.ts- Data for standard users without Zeno servicedummyData/interneUrmet.ts- Data for internal Urmet usersdummy_data.ts- Re-exports fromparticulierWithoutZeno.tsfor backward compatibility
Location: services/magento.ts
The MagentoService class provides fake API calls to simulate Magento authentication:
-
Get Customer Token
- Path:
/rest/default/V1/integration/customer/token - Method: POST
- Body:
{ username: string, password: string } - Returns: JWT token string
- Path:
-
Get Customer Info
- Path:
/rest/default/V1/customers/me - Method: GET
- Headers:
Authorization: Bearer <token> - Returns: User information object
- Path:
interface UserInfo {
name: string;
userType: "particulierWithoutZeno" | "interneUrmet";
}import { MagentoService } from "../services/magento";
// Get token
const token = await MagentoService.getCustomerToken({
username: "user@example.com",
password: "password123"
});
// Get user info
const userInfo = await MagentoService.getCustomerInfo(token);
// Or use the convenience method
const authResponse = await MagentoService.authenticate({
username: "user@example.com",
password: "password123"
});
// Returns: { token: string, userInfo: UserInfo }Location: contexts/AuthContext.tsx
Provides React context for authentication state across the application.
interface AuthContextType {
userInfo: UserInfo | null;
token: string | null;
isLoading: boolean;
isAuthenticated: boolean;
logout: () => void;
}import { useAuth } from "../contexts/AuthContext";
function MyComponent() {
const { userInfo, token, isLoading, isAuthenticated, logout } = useAuth();
if (isLoading) {
return <div>Loading...</div>;
}
return (
<div>
<h1>Welcome {userInfo?.name}!</h1>
<p>User type: {userInfo?.userType}</p>
<button onClick={logout}>Logout</button>
</div>
);
}Location: pages/_app.tsx
The authentication provider wraps the entire application and handles authentication before mounting:
AuthProvideris the top-level component- Authentication happens in
useEffecton mount - Loading state is displayed while authenticating
- Once authenticated, the app renders normally
The current implementation uses hardcoded credentials:
// In contexts/AuthContext.tsx
const authResponse = await MagentoService.authenticate({
username: "leila@example.com",
password: "password123",
});Current user:
- Name: Leïla
- Type: particulierWithoutZeno
API calls include simulated network delays (300ms) to mimic real-world conditions.
To connect to a real Magento API:
- Uncomment the actual API calls in
services/magento.ts - Add environment variables for the API base URL:
NEXT_PUBLIC_MAGENTO_BASE_URL=https://www.urmet.fr - Implement a login page instead of hardcoded credentials
- Add token persistence using localStorage or cookies
- Implement token refresh logic
- Add error handling for network failures and invalid credentials
// services/magento.ts
static async getCustomerToken(credentials: LoginCredentials): Promise<string> {
const response = await fetch(
`${process.env.NEXT_PUBLIC_MAGENTO_BASE_URL}/rest/default/V1/integration/customer/token`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(credentials),
}
);
if (!response.ok) {
throw new Error("Authentication failed");
}
return await response.json();
}Components can conditionally render based on user type:
import { useAuth } from "../contexts/AuthContext";
function MyComponent() {
const { userInfo } = useAuth();
if (userInfo?.userType === "particulierWithoutZeno") {
return <ParticulierView />;
}
if (userInfo?.userType === "interneUrmet") {
return <InternalView />;
}
return null;
}- Created
dummyData/particulierWithoutZeno.ts - Created
dummyData/interneUrmet.ts - Modified
dummy_data.tsto re-export from particulierWithoutZeno - Created
services/magento.ts - Created
contexts/AuthContext.tsx - Modified
pages/_app.tsxto include AuthProvider - Modified
pages/index.tsxto use userInfo from context
Currently, the app will:
- Show "Chargement..." while authenticating (600ms total with both API calls)
- Display "Bonjour Leïla," on the home page
- Make userInfo and token available throughout the app via useAuth hook