| import { authApiToken, authMiddleware } from "../../utils/auth.js"; | |
| import { addCorsHeaders } from "../../utils/cors.js"; | |
| import { AuthService } from "../../utils/authService.js"; | |
| export const onRequest = async (context: RouteContext): Promise<Response> => { | |
| const request = context.request; | |
| const env: Env = context.env; | |
| const authResponse = await authMiddleware(request, env); | |
| const apiResponse = await authApiToken(request, env); | |
| if (authResponse && apiResponse) { | |
| return addCorsHeaders(authResponse); | |
| } | |
| try { | |
| const { email } = await request.json() as any; | |
| if (!email) { | |
| throw new Error("Email is required"); | |
| } | |
| const authService = new AuthService(env); | |
| const result = await authService.loginMail(email); | |
| if (!result.success) { | |
| throw new Error(result.error); | |
| } | |
| return new Response( | |
| JSON.stringify({ | |
| success: true, | |
| message: "邮箱登录成功" | |
| }), | |
| { | |
| status: 200, | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| 'Access-Control-Allow-Origin': '*' | |
| } | |
| } | |
| ); | |
| } catch (error: any) { | |
| return new Response( | |
| JSON.stringify({ error: error.message }), | |
| { | |
| status: 500, | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| 'Access-Control-Allow-Origin': '*' | |
| } | |
| } | |
| ); | |
| } | |
| }; |