|
| 1 | +import jwt from "jsonwebtoken"; |
| 2 | +import { debugLog } from "./utils/logger.mjs"; |
| 3 | +import { getRouteForAuth } from "./utils/db.mjs"; |
| 4 | + |
| 5 | +// Compare dates in America/New_York using Intl (works reliably in Node). |
| 6 | +function estDateStr(date) { |
| 7 | + const parts = new Intl.DateTimeFormat("en-US", { |
| 8 | + timeZone: "America/New_York", |
| 9 | + year: "numeric", |
| 10 | + month: "2-digit", |
| 11 | + day: "2-digit", |
| 12 | + }).formatToParts(date); |
| 13 | + const get = (type) => parts.find((p) => p.type === type)?.value; |
| 14 | + return `${get("year")}-${get("month")}-${get("day")}`; |
| 15 | +} |
| 16 | + |
| 17 | +// Socket.IO middleware: verifies the client's JWT, loads the route, and |
| 18 | +// authorizes the connecting user (student/driver/super admin, same-day only). |
| 19 | +export async function authenticateSocket(socket, next) { |
| 20 | + const { routeId, token } = socket.handshake.auth; |
| 21 | + try { |
| 22 | + debugLog( |
| 23 | + `Auth attempt for routeId: ${routeId} and token is ${token ? "present" : "missing"} `, |
| 24 | + ); |
| 25 | + if (!routeId || !token) { |
| 26 | + return next(new Error("Route ID or token missing")); |
| 27 | + } |
| 28 | + |
| 29 | + let decoded; |
| 30 | + try { |
| 31 | + const secret = process.env.NEXTAUTH_SECRET; |
| 32 | + decoded = jwt.verify(token, secret); |
| 33 | + debugLog("JWT decoded successfully:", decoded); |
| 34 | + } catch (error) { |
| 35 | + console.error("JWT verify failed:", error.message); |
| 36 | + return next(new Error("Invalid JWT token")); |
| 37 | + } |
| 38 | + |
| 39 | + const userId = decoded.userId; |
| 40 | + debugLog("User ID from token:", userId); |
| 41 | + |
| 42 | + const route = await getRouteForAuth(routeId); |
| 43 | + if (!route) { |
| 44 | + return next(new Error("Route not found")); |
| 45 | + } |
| 46 | + |
| 47 | + const routeDateStr = estDateStr(new Date(route.scheduledPickupTime)); |
| 48 | + const todayStr = estDateStr(new Date()); |
| 49 | + const isSameDay = routeDateStr === todayStr; |
| 50 | + debugLog( |
| 51 | + `Date check: route=${routeDateStr}, today=${todayStr}, same=${isSameDay}`, |
| 52 | + ); |
| 53 | + if (!isSameDay) { |
| 54 | + return next(new Error("Route is not scheduled for today")); |
| 55 | + } |
| 56 | + |
| 57 | + const isStudent = route.student?._id?.toString() === userId; |
| 58 | + const isDriver = route.driver?._id?.toString() === userId; |
| 59 | + const isSuperAdmin = decoded.type === "SuperAdmin"; |
| 60 | + |
| 61 | + if (!isStudent && !isDriver && !isSuperAdmin) { |
| 62 | + return next(new Error("User not authorized for this route")); |
| 63 | + } |
| 64 | + |
| 65 | + socket.routeId = routeId; |
| 66 | + socket.user = userId; |
| 67 | + socket.userType = decoded.type; |
| 68 | + socket.routeStudent = route.student; |
| 69 | + socket.routeDriver = route.driver; |
| 70 | + next(); |
| 71 | + } catch (error) { |
| 72 | + console.error(`Auth error for routeId ${routeId}:`, error); |
| 73 | + return next(new Error("Authentication failed")); |
| 74 | + } |
| 75 | +} |
0 commit comments