mirror of
https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web.git
synced 2025-10-01 23:56: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 { collectModelTable } from "../utils/model";
|
||||||
import { makeAzurePath } from "../azure";
|
import { makeAzurePath } from "../azure";
|
||||||
import { getIP } from "@/app/api/auth";
|
import { getIP } from "@/app/api/auth";
|
||||||
|
import { getSessionName } from "@/lib/auth";
|
||||||
|
|
||||||
const serverConfig = getServerSideConfig();
|
const serverConfig = getServerSideConfig();
|
||||||
|
|
||||||
@ -149,22 +150,22 @@ export async function requestLog(
|
|||||||
req: NextRequest,
|
req: NextRequest,
|
||||||
jsonBody: any,
|
jsonBody: any,
|
||||||
url_path: string,
|
url_path: string,
|
||||||
|
name?: string,
|
||||||
) {
|
) {
|
||||||
// LOG
|
// LOG
|
||||||
try {
|
try {
|
||||||
if (url_path.startsWith("mj/") && !url_path.startsWith("mj/submit/")) {
|
if (url_path.startsWith("mj/") && !url_path.startsWith("mj/submit/")) {
|
||||||
return;
|
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 baseUrl = "http://localhost:3000";
|
||||||
const ip = getIP(req);
|
const ip = getIP(req);
|
||||||
// 对其进行 Base64 解码
|
// 对其进行 Base64 解码
|
||||||
let h_userName = req.headers.get("x-request-name");
|
// let h_userName = req.headers.get("x-request-name");
|
||||||
if (h_userName) {
|
// if (h_userName) {
|
||||||
const buffer = Buffer.from(h_userName, "base64");
|
// const buffer = Buffer.from(h_userName, "base64");
|
||||||
h_userName = decodeURIComponent(buffer.toString("utf-8"));
|
// h_userName = decodeURIComponent(buffer.toString("utf-8"));
|
||||||
}
|
// }
|
||||||
|
let h_userName = await getSessionName();
|
||||||
console.log("[中文]", h_userName, baseUrl);
|
console.log("[中文]", h_userName, baseUrl);
|
||||||
const logData = {
|
const logData = {
|
||||||
ip: ip,
|
ip: ip,
|
||||||
|
@ -1,20 +1,8 @@
|
|||||||
import { NextRequest, NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
import prisma from "@/lib/prisma";
|
import prisma from "@/lib/prisma";
|
||||||
import { insertUser } from "@/lib/auth";
|
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";
|
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(
|
async function handle(
|
||||||
req: NextRequest,
|
req: NextRequest,
|
||||||
{ params }: { params: { path: string[] } },
|
{ params }: { params: { path: string[] } },
|
||||||
|
@ -8,10 +8,8 @@ import {
|
|||||||
} from "@/app/constant";
|
} from "@/app/constant";
|
||||||
import { prettyObject } from "@/app/utils/format";
|
import { prettyObject } from "@/app/utils/format";
|
||||||
import { NextRequest, NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
import { auth, getIP } from "../../auth";
|
import { auth } from "../../auth";
|
||||||
import { getToken } from "next-auth/jwt";
|
|
||||||
import { requestLog, requestOpenai } from "../../common";
|
import { requestLog, requestOpenai } from "../../common";
|
||||||
import { headers } from "next/headers";
|
|
||||||
|
|
||||||
const ALLOWD_PATH = new Set(Object.values({ ...OpenaiPath, ...AZURE_PATH }));
|
const ALLOWD_PATH = new Set(Object.values({ ...OpenaiPath, ...AZURE_PATH }));
|
||||||
|
|
||||||
@ -113,7 +111,7 @@ async function handle(
|
|||||||
export const GET = handle;
|
export const GET = handle;
|
||||||
export const POST = handle;
|
export const POST = handle;
|
||||||
|
|
||||||
export const runtime = "edge";
|
// export const runtime = "edge";
|
||||||
export const preferredRegion = [
|
export const preferredRegion = [
|
||||||
"arn1",
|
"arn1",
|
||||||
"bom1",
|
"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 GitHubProvider from "next-auth/providers/github";
|
||||||
import EmailProvider from "next-auth/providers/email";
|
import EmailProvider from "next-auth/providers/email";
|
||||||
import CredentialsProvider from "next-auth/providers/credentials";
|
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 prisma from "@/lib/prisma";
|
||||||
import { isEmail, isName } from "@/lib/auth_list";
|
import {isEmail, isName} from "@/lib/auth_list";
|
||||||
import { createTransport } from "nodemailer";
|
import {createTransport} from "nodemailer";
|
||||||
|
|
||||||
const SECURE_COOKIES:boolean = !!process.env.SECURE_COOKIES;
|
const SECURE_COOKIES:boolean = !!process.env.SECURE_COOKIES;
|
||||||
|
|
||||||
@ -157,6 +157,12 @@ export function getSession() {
|
|||||||
} | null>;
|
} | null>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getSessionName() {
|
||||||
|
const session = await getSession();
|
||||||
|
// console.log('in........',)
|
||||||
|
return session?.user?.email || session?.user?.name
|
||||||
|
}
|
||||||
|
|
||||||
// export function withSiteAuth(action: any) {
|
// export function withSiteAuth(action: any) {
|
||||||
// return async (
|
// return async (
|
||||||
// formData: FormData | null,
|
// formData: FormData | null,
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import type { NextRequest } from "next/server";
|
import type { NextRequest } from "next/server";
|
||||||
import { getToken } from "next-auth/jwt";
|
import { getToken } from "next-auth/jwt";
|
||||||
import { DENY_LIST, isName, ADMIN_LIST } from "@/lib/auth_list";
|
import { isName, ADMIN_LIST } from "@/lib/auth_list";
|
||||||
import {use} from "react";
|
|
||||||
|
|
||||||
export default async function middleware(req: NextRequest) {
|
export default async function middleware(req: NextRequest) {
|
||||||
const url = req.nextUrl;
|
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"))) {
|
// if (req.method == 'POST' && (path.startsWith("/api/openai/") || path.startsWith("/api/midjourney"))) {
|
||||||
// 重写header,添加用户名
|
// // 重写header,添加用户名
|
||||||
// console.log(session,'========')
|
// // console.log(session,'========')
|
||||||
const requestHeaders = new Headers(req.headers)
|
// const requestHeaders = new Headers(req.headers)
|
||||||
|
//
|
||||||
// 使用 encodeURIComponent 对特殊字符进行编码
|
// // 使用 encodeURIComponent 对特殊字符进行编码
|
||||||
// 将编码的 URI 组件转换成 Base64
|
// // 将编码的 URI 组件转换成 Base64
|
||||||
const encodeName = Buffer.from(encodeURIComponent(`${session?.name}`)).toString('base64');
|
// const encodeName = Buffer.from(encodeURIComponent(`${session?.name}`)).toString('base64');
|
||||||
|
//
|
||||||
requestHeaders.set('x-request-name', encodeName)
|
// requestHeaders.set('x-request-name', encodeName)
|
||||||
return NextResponse.next({
|
// return NextResponse.next({
|
||||||
request: {
|
// request: {
|
||||||
// New request headers
|
// // New request headers
|
||||||
headers: requestHeaders,
|
// headers: requestHeaders,
|
||||||
},
|
// },
|
||||||
})
|
// })
|
||||||
}
|
// }
|
||||||
|
|
||||||
return NextResponse.next()
|
return NextResponse.next()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user