Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions backend/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
9 changes: 9 additions & 0 deletions backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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."})
Expand Down
34 changes: 31 additions & 3 deletions frontend/src/pages/auth/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand All @@ -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?')
}
}
}

Expand Down Expand Up @@ -138,6 +164,8 @@ export function Login({ onNavigate }: Props) {
</div>
</div>

{error && ( <p class="text-sm font-semibold text-red-500 text-center mb-2 animate-pulse">{error}</p>)}

{/* Submit Button */}
<button
type="submit"
Expand Down
14 changes: 14 additions & 0 deletions frontend/src/routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ export function AppRoutes() {
)
}

// ROUTE GUARD: If they are trying to access ANY admin page but aren't logged in,
// kick them back to the login page immediately.
if (path.startsWith('/FiremeX/admin/')) {
if (!isLoggedIn()) {
navigate('/FiremeX/login')
return null // Don't render anything while we redirect
}
}

if (path === '/FiremeX/admin/dashboard') {
return (
<AdminLayout activePage="dashboard" onNavigate={navigate}>
Expand Down Expand Up @@ -112,6 +121,11 @@ export function AppRoutes() {
)
}


function isLoggedIn() {
return !!localStorage.getItem('firemex_token')
}

function normalizePath(pathname: string) {
if (!pathname || pathname === '/') return '/login'
return pathname.replace(/\/$/, '') || '/login'
Expand Down
Loading