mirror of
https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web.git
synced 2025-10-01 15:46:39 +08:00
初次优化认证
This commit is contained in:
parent
3368e88d16
commit
20b35078f0
@ -4,6 +4,7 @@ import { DEFAULT_MODELS, OPENAI_BASE_URL, GEMINI_BASE_URL } from "../constant";
|
||||
import { collectModelTable } from "../utils/model";
|
||||
import { makeAzurePath } from "../azure";
|
||||
import { getIP } from "@/app/api/auth";
|
||||
import { getSessionName } from "@/lib/auth";
|
||||
|
||||
const serverConfig = getServerSideConfig();
|
||||
|
||||
@ -149,22 +150,22 @@ export async function requestLog(
|
||||
req: NextRequest,
|
||||
jsonBody: any,
|
||||
url_path: string,
|
||||
name?: string,
|
||||
) {
|
||||
// LOG
|
||||
try {
|
||||
if (url_path.startsWith("mj/") && !url_path.startsWith("mj/submit/")) {
|
||||
return;
|
||||
}
|
||||
// const protocol = req.headers.get("x-forwarded-proto") || "http";
|
||||
//const baseUrl = process.env.NEXTAUTH_URL ?? "http://localhost:3000";
|
||||
const baseUrl = "http://localhost:3000";
|
||||
const ip = getIP(req);
|
||||
// 对其进行 Base64 解码
|
||||
let h_userName = req.headers.get("x-request-name");
|
||||
if (h_userName) {
|
||||
const buffer = Buffer.from(h_userName, "base64");
|
||||
h_userName = decodeURIComponent(buffer.toString("utf-8"));
|
||||
}
|
||||
// let h_userName = req.headers.get("x-request-name");
|
||||
// if (h_userName) {
|
||||
// const buffer = Buffer.from(h_userName, "base64");
|
||||
// h_userName = decodeURIComponent(buffer.toString("utf-8"));
|
||||
// }
|
||||
let h_userName = await getSessionName();
|
||||
console.log("[中文]", h_userName, baseUrl);
|
||||
const logData = {
|
||||
ip: ip,
|
||||
|
@ -1,20 +1,8 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import prisma from "@/lib/prisma";
|
||||
import { insertUser } from "@/lib/auth";
|
||||
// import { getTokenLength } from "@/app/utils/token";
|
||||
// import { Tiktoken } from "tiktoken/lite"
|
||||
// import cl100k_base from "tiktoken/encoders/cl100k_base.json"
|
||||
// import "tiktoken";
|
||||
// import { get_encoding } from "tiktoken";
|
||||
import { addHours, subMinutes } from "date-fns";
|
||||
import { getTokenLength } from "@/lib/utils";
|
||||
|
||||
// function getTokenLength(input: string): number {
|
||||
// const encoding = get_encoding("cl100k_base");
|
||||
// // console.log('tokens: ', input, encoding.countTokens())
|
||||
// return encoding.encode(input).length;
|
||||
// }
|
||||
|
||||
async function handle(
|
||||
req: NextRequest,
|
||||
{ params }: { params: { path: string[] } },
|
||||
|
@ -8,10 +8,8 @@ import {
|
||||
} from "@/app/constant";
|
||||
import { prettyObject } from "@/app/utils/format";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { auth, getIP } from "../../auth";
|
||||
import { getToken } from "next-auth/jwt";
|
||||
import { auth } from "../../auth";
|
||||
import { requestLog, requestOpenai } from "../../common";
|
||||
import { headers } from "next/headers";
|
||||
|
||||
const ALLOWD_PATH = new Set(Object.values({ ...OpenaiPath, ...AZURE_PATH }));
|
||||
|
||||
@ -113,7 +111,7 @@ async function handle(
|
||||
export const GET = handle;
|
||||
export const POST = handle;
|
||||
|
||||
export const runtime = "edge";
|
||||
// export const runtime = "edge";
|
||||
export const preferredRegion = [
|
||||
"arn1",
|
||||
"bom1",
|
||||
|
12
lib/auth.ts
12
lib/auth.ts
@ -2,10 +2,10 @@ import {getServerSession, type NextAuthOptions, Theme} from "next-auth";
|
||||
import GitHubProvider from "next-auth/providers/github";
|
||||
import EmailProvider from "next-auth/providers/email";
|
||||
import CredentialsProvider from "next-auth/providers/credentials";
|
||||
import { PrismaAdapter } from "@next-auth/prisma-adapter";
|
||||
import {PrismaAdapter} from "@next-auth/prisma-adapter";
|
||||
import prisma from "@/lib/prisma";
|
||||
import { isEmail, isName } from "@/lib/auth_list";
|
||||
import { createTransport } from "nodemailer";
|
||||
import {isEmail, isName} from "@/lib/auth_list";
|
||||
import {createTransport} from "nodemailer";
|
||||
|
||||
const SECURE_COOKIES:boolean = !!process.env.SECURE_COOKIES;
|
||||
|
||||
@ -157,6 +157,12 @@ export function getSession() {
|
||||
} | null>;
|
||||
}
|
||||
|
||||
export async function getSessionName() {
|
||||
const session = await getSession();
|
||||
// console.log('in........',)
|
||||
return session?.user?.email || session?.user?.name
|
||||
}
|
||||
|
||||
// export function withSiteAuth(action: any) {
|
||||
// return async (
|
||||
// formData: FormData | null,
|
||||
|
@ -1,8 +1,7 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { getToken } from "next-auth/jwt";
|
||||
import { DENY_LIST, isName, ADMIN_LIST } from "@/lib/auth_list";
|
||||
import {use} from "react";
|
||||
import { isName, ADMIN_LIST } from "@/lib/auth_list";
|
||||
|
||||
export default async function middleware(req: NextRequest) {
|
||||
const url = req.nextUrl;
|
||||
@ -60,23 +59,23 @@ export default async function middleware(req: NextRequest) {
|
||||
);
|
||||
}
|
||||
|
||||
if (req.method == 'POST' && (path.startsWith("/api/openai/") || path.startsWith("/api/midjourney"))) {
|
||||
// 重写header,添加用户名
|
||||
// console.log(session,'========')
|
||||
const requestHeaders = new Headers(req.headers)
|
||||
|
||||
// 使用 encodeURIComponent 对特殊字符进行编码
|
||||
// 将编码的 URI 组件转换成 Base64
|
||||
const encodeName = Buffer.from(encodeURIComponent(`${session?.name}`)).toString('base64');
|
||||
|
||||
requestHeaders.set('x-request-name', encodeName)
|
||||
return NextResponse.next({
|
||||
request: {
|
||||
// New request headers
|
||||
headers: requestHeaders,
|
||||
},
|
||||
})
|
||||
}
|
||||
// if (req.method == 'POST' && (path.startsWith("/api/openai/") || path.startsWith("/api/midjourney"))) {
|
||||
// // 重写header,添加用户名
|
||||
// // console.log(session,'========')
|
||||
// const requestHeaders = new Headers(req.headers)
|
||||
//
|
||||
// // 使用 encodeURIComponent 对特殊字符进行编码
|
||||
// // 将编码的 URI 组件转换成 Base64
|
||||
// const encodeName = Buffer.from(encodeURIComponent(`${session?.name}`)).toString('base64');
|
||||
//
|
||||
// requestHeaders.set('x-request-name', encodeName)
|
||||
// return NextResponse.next({
|
||||
// request: {
|
||||
// // New request headers
|
||||
// headers: requestHeaders,
|
||||
// },
|
||||
// })
|
||||
// }
|
||||
|
||||
return NextResponse.next()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user