import { UserRole } from "./roles"; export interface InviteTokenPayload { email: string; role: UserRole; siteIds?: string[]; invitedBy: string; expiresAt: Date; } export function generateInviteToken(payload: InviteTokenPayload): string { // In a real implementation, this would use JWT or a secure token generation library // For now, we'll create a simple base64 encoded token const tokenData = { ...payload, timestamp: Date.now(), type: "invitation", }; return Buffer.from(JSON.stringify(tokenData)).toString("base64"); } export function verifyInviteToken(token: string): InviteTokenPayload | null { try { const decoded = Buffer.from(token, "base64").toString("utf-8"); const tokenData = JSON.parse(decoded); if (tokenData.type !== "invitation") { return null; } // Check if token has expired const expiresAt = new Date(tokenData.expiresAt); if (expiresAt < new Date()) { return null; } return { email: tokenData.email, role: tokenData.role, siteIds: tokenData.siteIds, invitedBy: tokenData.invitedBy, expiresAt, }; } catch (error) { console.error("Token verification error:", error); return null; } }