Chapter 3: Frontend Integration
π― Objectivesβ
- Understand the provided test frontend
- Connect the frontend to the Spring Boot backend
- Test the contact form in a real environment
- Configure CORS for Codespaces
π Frontend Resourcesβ
Project Resources
| Resource | Link |
|---|---|
| Frontend Code | Frontend folder |
| Integration Guide | 27-guide-formulaire-frontend-codespaces.md |
π¨ Frontend Overviewβ
The provided frontend is a simple HTML/CSS/JavaScript application that allows you to:
- β Submit contact forms
- β View API responses
- β Test admin authentication
- β View the list of leads
Frontend Structureβ
frontend/
βββ index.html # Contact form page
βββ admin.html # Admin page (leads list)
βββ login.html # Login page
βββ css/
β βββ styles.css # Styles
βββ js/
βββ contact.js # Contact form logic
βββ admin.js # Admin logic
βββ auth.js # Authentication management
π Starting the Frontendβ
Option 1: With Live Server (VS Code / Codespaces)β
-
Install the Live Server extension in VS Code/Codespaces
-
Open the frontend folder:
cd frontend
-
Right-click on
index.htmlβ Open with Live Server -
The browser automatically opens on the frontend
Option 2: With Python (Simple HTTP Server)β
# In the frontend folder
cd frontend
# Python 3
python -m http.server 3000
# Access http://localhost:3000
Option 3: With Node.jsβ
# Install serve globally
npm install -g serve
# Start the server
cd frontend
serve -p 3000
βοΈ Configuration for Codespacesβ
CORS Issue in Codespacesβ
In GitHub Codespaces, the URLs are different:
- Backend:
https://xxx-8080.app.github.dev - Frontend:
https://xxx-3000.app.github.dev
Solution: Configure Codespaces URLsβ
-
Retrieve the Codespaces backend URL:
- When the Spring Boot application starts
- Note the public URL (e.g.,
https://abc123-8080.app.github.dev)
-
Modify the frontend JavaScript file:
// js/config.js or at the top of contact.js
// API URL configuration
const API_BASE_URL = 'https://YOUR-CODESPACE-8080.app.github.dev';
// OR for local development
// const API_BASE_URL = 'http://localhost:8080';
- Update the backend CORS configuration:
// SecurityConfig.java
@Bean
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOriginPatterns(Arrays.asList("*"));
configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "OPTIONS"));
configuration.setAllowedHeaders(Arrays.asList("*"));
configuration.setAllowCredentials(true);
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}
π Using the Contact Formβ
Main Page (index.html)β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β CONTACT US β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β Full Name * Company β
β βββββββββββββββββββββββββββ ββββββββββββββββββββββββ
β β Enter your name β β Your company ββ
β βββββββββββββββββββββββββββ ββββββββββββββββββββββββ
β β
β Email * Phone β
β βββββββββββββββββββββββββββ ββββββββββββββββββββββββ
β β your@email.com β β 514-555-1234 ββ
β βββββββββββββββββββββββββββ ββββββββββββββββββββββββ
β β
β Request Type * β
β βββββββββββββββββββββββββββββββββββββββββββββββββββ¬ββββ
β β Information Request β vββ
β βββββββββββββββββββββββββββββββββββββββββββββββββββ΄ββββ
β β
β Message * β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β Describe your request... ββ
β β ββ
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β
β βββββββββββββββββββββββ β
β β SEND β β
β βββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Submission Testβ
- Fill in all required fields (*)
- Click on Send
- Observe the response:
Success:
β
Message sent successfully!
Your request has been recorded. We will contact you soon.
Error:
β Error during sending
Please check the fields and try again.
π Admin Interfaceβ
Login (login.html)β
- Access
login.html - Enter the credentials:
- Email:
admin@test.com - Password:
password123(or as configured)
- Email:
- Click on Log in
Admin Dashboard (admin.html)β
Once logged in, you can:
- π View the list of all contacts/leads
- π Filter by status (NEW, CONTACTED, CONVERTED, LOST)
- βοΈ Change a lead's status
- ποΈ Delete a lead
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Admin Dashboard - Lead Management [Logout] β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β Filter: [All βΌ] [NEW] [CONTACTED] [CONVERTED] [LOST] β
β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β ID β Name β Email β Type β Status β β ββ
β ββββββΌβββββββββββββββΌββββββββββββββββββΌββββββββββΌβββββββββΌββββ€β
β β 1 β John Doe β john@test.com β INFO β NEW β βπββ
β β 2 β Mary Smith β mary@test.com β QUOTE β CONTACTβ βπββ
β β 3 β Peter Brown β peter@test.com β SUPPORT β NEW β βπββ
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β
β Total: 3 leads | New: 2 | In progress: 1 β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
π Frontend-Backend Communication Flowβ
π‘ API Calls from the Frontendβ
JavaScript to submit the formβ
// contact.js
async function submitContact(formData) {
try {
const response = await fetch(`${API_BASE_URL}/api/contact`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
fullName: formData.fullName,
email: formData.email,
company: formData.company,
phone: formData.phone,
requestType: formData.requestType,
message: formData.message
})
});
if (!response.ok) {
throw new Error('Error during submission');
}
const data = await response.json();
showSuccess('Message sent successfully!');
return data;
} catch (error) {
showError('Error: ' + error.message);
}
}
JavaScript for admin authenticationβ
// auth.js
async function login(email, password) {
const response = await fetch(`${API_BASE_URL}/api/auth/login`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ email, password })
});
const data = await response.json();
if (response.ok) {
// Store the JWT token
localStorage.setItem('token', data.token);
window.location.href = 'admin.html';
} else {
showError('Incorrect credentials');
}
}
JavaScript to retrieve leads (with token)β
// admin.js
async function getLeads() {
const token = localStorage.getItem('token');
const response = await fetch(`${API_BASE_URL}/api/admin/leads`, {
headers: {
'Authorization': `Bearer ${token}`
}
});
if (!response.ok) {
if (response.status === 401) {
// Token expired, redirect to login
window.location.href = 'login.html';
}
throw new Error('Error during loading');
}
return await response.json();
}
β οΈ Frontend Troubleshootingβ
CORS Errorβ
Access to fetch at 'http://localhost:8080/api/contact' has been blocked by CORS policy
Solution: Check the backend CORS configuration.
Token not sentβ
Symptom: 401 on admin endpoints even after login.
Solution: Ensure the token is stored and sent:
console.log('Token:', localStorage.getItem('token'));
Incorrect URLβ
Symptom: fetch failed or network error
Solution: Ensure that:
- The backend is running
- The URL in
API_BASE_URLis correct - For Codespaces, use the public
.app.github.devURL
β Validation Checklistβ
- Frontend accessible in the browser
- Contact form visible
- Contact submission successful
- Success response displayed
- Admin login functional
- Leads list visible after login
π Complete Guideβ
Detailed Guide
For all detailed instructions on frontend integration with Codespaces:
βοΈ Next Stepβ
Chapter 4: Additional Course β