Xu ly dong bo nguoi dung thanh cong

This commit is contained in:
quangdn-ght
2025-06-26 00:47:54 +07:00
parent ee25e4ac82
commit 8af4d7a6d9
14 changed files with 113 additions and 66 deletions

View File

@@ -15,7 +15,6 @@ import { handle as siliconflowHandler } from "../../siliconflow";
import { handle as xaiHandler } from "../../xai";
import { handle as chatglmHandler } from "../../glm";
import { handle as proxyHandler } from "../../proxy";
import { handle as supabaseHandler } from "../../supabase";
async function handle(
req: NextRequest,
@@ -28,9 +27,6 @@ async function handle(
console.log(`[${params.provider} Route] params `, params);
switch (apiPath) {
case ApiPath.Supabase:
console.log("[Supabase Route] params ", params);
return supabaseHandler(req, { params });
case ApiPath.Azure:
return azureHandler(req, { params });
case ApiPath.Google:

View File

@@ -0,0 +1,21 @@
// /app/api/auth/check/route.ts
import { NextRequest } from "next/server";
import { checkAuth } from "../../supabase";
export async function GET(req: NextRequest) {
const user = await checkAuth(req);
console.log("[Auth] user ", user);
if (!user) {
return new Response(JSON.stringify({ authenticated: false }), {
status: 401,
headers: { "Content-Type": "application/json" },
});
}
return new Response(JSON.stringify({ authenticated: true, user }), {
status: 200,
headers: { "Content-Type": "application/json" },
});
}

View File

@@ -30,14 +30,17 @@ export async function requestOpenai(req: NextRequest) {
authHeaderName = "api-key";
} else {
// Nếu là OpenAI thường, giữ nguyên header Authorization
authValue = req.headers.get("Authorization") ?? "";
authHeaderName = "Authorization";
console.log("[Auth] ", authValue);
}
// Xử lý lại đường dẫn endpoint cho phù hợp với OpenAI/Azure
let path = `${req.nextUrl.pathname}`.replaceAll("/api/openai/", "");
console.log("[Proxy] mac dinh ", path);
// console.log("[Proxy] mac dinh ", path);
// Lấy baseUrl từ config, ưu tiên Azure nếu là request Azure
let baseUrl =

View File

@@ -1,23 +1,20 @@
import { createClient } from "@supabase/supabase-js";
import { NextRequest, NextResponse } from "next/server";
import cookie from "cookie";
import { NextRequest } from "next/server";
const SUPABASE_URL = process.env.SUPABASE_URL!;
const SUPABASE_ANON_KEY = process.env.SUPABASE_ANON_KEY!;
const AUTHEN_PAGE = process.env.AUTHEN_PAGE!;
export async function handle(
req: NextRequest,
{ params }: { params: { path: string[] } },
) {
// Parse cookies using the 'cookie' library
const cookies = cookie.parse(req.headers.get("cookie") || "");
const authToken = cookies["sb-zzgkylsbdgwoohcbompi-auth-token"];
export async function checkAuth(req: NextRequest) {
// Use NextRequest.cookies API
const authToken = req.cookies.get("sb-zzgkylsbdgwoohcbompi-auth-token")
?.value;
console.log("[Supabase] authToken", authToken);
// console.log("[Supabase] authToken", authToken);
if (!authToken) {
return NextResponse.redirect(AUTHEN_PAGE, 302);
// Không tìm thấy token xác thực
console.log("[Supabase] No auth token found");
return null;
}
const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY, {
@@ -31,21 +28,18 @@ export async function handle(
try {
const { data, error } = await supabase.auth.getUser();
console.log("[Supabase] user", data?.user);
if (error || !data?.user) {
return NextResponse.json(
{ error: error?.message || "Error fetching user data" },
{ status: 401 },
);
// Lỗi khi lấy thông tin người dùng
console.error("[Supabase] Error getting user:", error);
return null;
}
return NextResponse.json({ user: data.user }, { status: 200 });
// Đã xác thực thành công, trả về thông tin người dùng
console.log("[Supabase] Authenticated user:", data.user);
return data.user;
} catch (err) {
console.error("Error fetching user data from Supabase:", err);
return NextResponse.json(
{ error: "Internal server error" },
{ status: 500 },
);
// Lỗi khi lấy dữ liệu người dùng
console.error("[Supabase] Error fetching user data:", err);
return null;
}
}