mirror of
https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web.git
synced 2025-10-01 23:56:39 +08:00
Initial commit with the content of temp branch
This commit is contained in:
parent
fc39af6dbf
commit
7611c58527
13
lib/auth.ts
13
lib/auth.ts
@ -5,7 +5,7 @@ import CredentialsProvider from "next-auth/providers/credentials";
|
||||
import {PrismaAdapter} from "@next-auth/prisma-adapter";
|
||||
import prisma from "@/lib/prisma";
|
||||
import { User } from "@prisma/client";
|
||||
import {ADMIN_LIST, isEmail, isName} from "@/lib/auth_list";
|
||||
import { isEmail, isName } from "@/lib/auth_list";
|
||||
import {createTransport} from "nodemailer";
|
||||
import { comparePassword, hashPassword } from "@/lib/utils";
|
||||
import {getCurStartEnd} from "@/app/utils/custom";
|
||||
@ -194,17 +194,6 @@ export async function getSessionName() {
|
||||
}
|
||||
}
|
||||
|
||||
export async function VerifiedUser() {
|
||||
const { name, session } = await getSessionName();
|
||||
const userId = session?.user?.id
|
||||
return !!(name && isName(name) && userId);
|
||||
}
|
||||
|
||||
export async function VerifiedAdminUser() {
|
||||
const { name, session } = await getSessionName();
|
||||
return !!(name && ADMIN_LIST.includes(name));
|
||||
}
|
||||
|
||||
export function validatePassword(password: string, hashPassword: string | null | undefined ): boolean | void {
|
||||
if (!hashPassword) {
|
||||
throw new Error("未设置密码");
|
||||
|
@ -1,10 +1,5 @@
|
||||
import {ADMIN_LIST, isName} from "@/lib/auth_list";
|
||||
import { JWT } from "next-auth/jwt";
|
||||
import { User } from "@prisma/client";
|
||||
|
||||
type CUS_JWT = JWT & {
|
||||
user: User,
|
||||
}
|
||||
import {isName} from "@/lib/auth_list";
|
||||
import { CUS_JWT } from "@/lib/auth_type";
|
||||
|
||||
|
||||
export async function VerifiedUser(session: CUS_JWT |null) {
|
||||
|
@ -4,12 +4,6 @@ export const DENY_LIST: string[] = [
|
||||
"suibian", "某某", "张三", "李四", "啊实打实", "官方回复电话", "笑死", "观化听风", "null", "undefined",
|
||||
"zhangsan",
|
||||
]
|
||||
export const ADMIN_LIST: string[] = [
|
||||
"sijinhui", "sijinhui@qq.com",
|
||||
"yuchuan", "于川",
|
||||
"jujujujuju",
|
||||
]
|
||||
|
||||
|
||||
export function isEmail(input: string): boolean {
|
||||
const emailRegex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
|
||||
|
6
lib/auth_type.ts
Normal file
6
lib/auth_type.ts
Normal file
@ -0,0 +1,6 @@
|
||||
import { JWT } from "next-auth/jwt";
|
||||
import { User } from "@prisma/client";
|
||||
|
||||
export type CUS_JWT = JWT & {
|
||||
user: User,
|
||||
}
|
@ -1,8 +1,9 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { getToken } from "next-auth/jwt";
|
||||
// import { isName, ADMIN_LIST } from "@/lib/auth_list";
|
||||
import { VerifiedUser, VerifiedAdminUser } from "@/lib/auth_client";
|
||||
import { CUS_JWT } from "@/lib/auth_type";
|
||||
|
||||
|
||||
export default async function middleware(req: NextRequest) {
|
||||
const url = req.nextUrl;
|
||||
@ -17,8 +18,8 @@ export default async function middleware(req: NextRequest) {
|
||||
|
||||
|
||||
const session = await getToken({ req });
|
||||
const isUser = await VerifiedUser(session);
|
||||
const isAdminUser = await VerifiedAdminUser(session);
|
||||
const isUser = await VerifiedUser(session as CUS_JWT);
|
||||
const isAdminUser = await VerifiedAdminUser(session as CUS_JWT);
|
||||
// console.log('----session', session, '---isUser', isUser, '---isAdmin', isAdminUser)
|
||||
// 管理员页面的api接口还是要认证的
|
||||
if (path.startsWith('/api/admin/')) {
|
||||
|
Loading…
Reference in New Issue
Block a user