diff --git a/backend/go.mod b/backend/go.mod index 0d62b9f..ec1c530 100644 --- a/backend/go.mod +++ b/backend/go.mod @@ -8,6 +8,7 @@ require ( github.com/bytedance/sonic/loader v0.5.2 // indirect github.com/cloudwego/base64x v0.1.7 // indirect github.com/gabriel-vasile/mimetype v1.4.15 // indirect + github.com/gin-contrib/cors v1.7.7 // indirect github.com/gin-contrib/sse v1.1.1 // indirect github.com/gin-gonic/gin v1.12.0 // indirect github.com/go-playground/locales v0.14.1 // indirect diff --git a/backend/go.sum b/backend/go.sum index a98f9f3..1bade5b 100644 --- a/backend/go.sum +++ b/backend/go.sum @@ -10,6 +10,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/gabriel-vasile/mimetype v1.4.15 h1:05iP/CYtZ/w455R/KZM6rZ5ieAdh99UPtd+d3YzLmaI= github.com/gabriel-vasile/mimetype v1.4.15/go.mod h1:azpTcoLcDZRNgFou5j+APrqQx9HqVPWa6ijYQIIVswQ= +github.com/gin-contrib/cors v1.7.7 h1:Oh9joP463x7Mw72vhvJ61YQm8ODh9b04YR7vsOErD0Q= +github.com/gin-contrib/cors v1.7.7/go.mod h1:K5tW0RkzJtWSiOdikXloy8VEZlgdVNpHNw8FpjUPNrE= github.com/gin-contrib/sse v1.1.1 h1:uGYpNwTacv5R68bSGMapo62iLTRa9l5zxGCps4hK6ko= github.com/gin-contrib/sse v1.1.1/go.mod h1:QXzuVkA0YO7o/gun03UI1Q+FTI8ZV/n5t03kIQAI89s= github.com/gin-gonic/gin v1.12.0 h1:b3YAbrZtnf8N//yjKeU2+MQsh2mY5htkZidOM7O0wG8= diff --git a/backend/main.go b/backend/main.go index c05158c..ba1c332 100644 --- a/backend/main.go +++ b/backend/main.go @@ -9,6 +9,7 @@ import ( "github.com/firemex/backend/database" "github.com/firemex/backend/middleware" "github.com/firemex/backend/models" + "github.com/gin-contrib/cors" ) func main() { @@ -26,6 +27,14 @@ func main() { // 3. Initialize the Gin web framework router := gin.Default() + // 3.1 CORS Configuration + router.Use(cors.New(cors.Config{ + AllowOrigins: []string{"http://localhost:5173"}, + AllowMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE"}, + AllowHeaders: []string{"Origin", "Content-Type", "Authorization"}, + AllowCredentials: true, + })) + // 4. Test Route router.GET("/ping", func(c *gin.Context) { c.JSON(200, gin.H{"message": "pong! FiremeX API is running."}) diff --git a/frontend/src/pages/auth/Login.tsx b/frontend/src/pages/auth/Login.tsx index 9cb55d3..8dd9f1d 100644 --- a/frontend/src/pages/auth/Login.tsx +++ b/frontend/src/pages/auth/Login.tsx @@ -10,11 +10,14 @@ export function Login({ onNavigate }: Props) { const [password, setPassword] = useState('operator12345') const [name, setName] = useState('') const [showPassword, setShowPassword] = useState(false) + const [error, setError] = useState('') - const handleSubmit = (e: Event) => { + const handleSubmit = async (e: Event) => { e.preventDefault() + setError('') // Clear any old errors + if (isRegistering) { - // Save registration request to localStorage + // Keep your old registration mockup code here const savedPending = localStorage.getItem('firemex_pending_users') const pending = savedPending ? JSON.parse(savedPending) : [ { id: 'usr-101', name: 'Bob Johnson', email: 'bob@gmail.com', role: 'Operator', date: '2026-07-09' }, @@ -34,7 +37,30 @@ export function Login({ onNavigate }: Props) { setEmail('operator@gmail.com') setPassword('operator12345') } else { - onNavigate('/FiremeX/admin/dashboard') + try { + // 1. Send the email and password to the Go backend + const response = await fetch('http://localhost:8080/login', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ email, password }) + }) + + const data = await response.json() + + // 2. If the backend says the password is wrong + if (!response.ok) { + setError(data.error || 'Login failed') + return + } + + // 3. Success! Save the JWT token + localStorage.setItem('firemex_token', data.token) + + // 4. Go to the dashboard + onNavigate('/FiremeX/admin/dashboard') + } catch (err) { + setError('Network error. Is the backend running?') + } } } @@ -138,6 +164,8 @@ export function Login({ onNavigate }: Props) { + {error && (
{error}
)} + {/* Submit Button */}