- internal/assignment: GEORADIUS miler assignment with retry/escalation, customer-side provider scoring, FCM notifications on assign - internal/notify: Firebase Admin SDK (FCM) client initialisation - internal/ws: WebSocket handlers for live parcel tracking and customer↔miler chat - middlewares: city gate (pincode prefix validation), internal API key auth, WebSocket JWT auth - controllers: InternalNotify + InternalReassign for machine-to-machine calls; pricing helpers wired into CreateCustomerBooking and CreateCRMBooking - routes: /internal/*, /ws/bookings/:id/track, /ws/bookings/:id/chat - models/users, models/doormile_pricing: new fields for device tokens, assignment state, pricing bands - seed_data.sql: initial pricing seed rows .env and Firebase service-account JSON intentionally excluded. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
37 lines
996 B
Go
37 lines
996 B
Go
package middlewares
|
|
|
|
import (
|
|
"doormile/config"
|
|
"doormile/utils"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
// WsChatAuth validates a JWT passed as a ?token= query parameter.
|
|
//
|
|
// WebSocket clients (both browsers and Flutter) cannot set the Authorization
|
|
// header during the initial HTTP upgrade handshake, so the token is sent as a
|
|
// query parameter instead. Fiber locals set here are forwarded into the
|
|
// WebSocket handler via the gofiber/websocket package.
|
|
func WsChatAuth(cfg *config.Config) fiber.Handler {
|
|
return func(c *fiber.Ctx) error {
|
|
token := c.Query("token")
|
|
if token == "" {
|
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
|
"error": "token query parameter is required",
|
|
})
|
|
}
|
|
|
|
claims, err := utils.ParseToken(token, cfg.JWTSecret)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
|
"error": "invalid or expired token",
|
|
})
|
|
}
|
|
|
|
c.Locals("userid", claims.UserID)
|
|
c.Locals("roleid", claims.RoleID)
|
|
return c.Next()
|
|
}
|
|
}
|