CoolFace
Apppublic

zhemima/msmail

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
auth.ts43 linesDownload Raw Back to mail
1import { authApiToken, authMiddleware } from "../../utils/auth.js";2import { addCorsHeaders } from "../../utils/cors.js";3import { AuthService } from "../../utils/authService.js";4 5export const onRequest = async (context: RouteContext): Promise<Response> => {6    const request = context.request;7    const env: Env = context.env;8 9    const authResponse = await authMiddleware(request, env);10    const apiResponse = await authApiToken(request, env);11    if (authResponse && apiResponse) {12        return addCorsHeaders(authResponse);13    }14 15    try {16        const { email } = await request.json() as any;17        if (!email) {18            throw new Error("Email is required");19        }20 21        const authService = new AuthService(env);22        const result = await authService.authenticateEmail(email);23 24        if (!result.success) {25            throw new Error(result.error);26        }27 28        return new Response(29            JSON.stringify({ success: true }),30            {31                status: 200,32                headers: { 'Content-Type': 'application/json' }33            }34        );35 36    } catch (error: any) {37        return new Response(38            JSON.stringify({ error: error.message }),39            { status: 500 }40        );41    }42};43