mirror of
https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web.git
synced 2025-10-03 00:26:40 +08:00
修复一部分类型错误,无影响
This commit is contained in:
parent
bf457c6853
commit
7c7cc91c05
@ -35,7 +35,6 @@ export default function SetPasswordPage() {
|
||||
const onFinish: FormProps<FieldType>["onFinish"] = (values) => {
|
||||
// setLoading(true);
|
||||
// console.log('-------------', values)
|
||||
// @ts-expect-error
|
||||
fetch(`/api/user/${session?.user?.id}`, {
|
||||
method: "PUT",
|
||||
credentials: "include",
|
||||
@ -61,9 +60,7 @@ export default function SetPasswordPage() {
|
||||
layout="vertical"
|
||||
onFinish={onFinish}
|
||||
>
|
||||
{
|
||||
// @ts-expect-error
|
||||
status === "authenticated" && session?.user?.hasPassword && (
|
||||
{status === "authenticated" && session?.user?.hasPassword && (
|
||||
<Form.Item
|
||||
name="user[old_password]"
|
||||
label="Old password"
|
||||
@ -84,8 +81,7 @@ export default function SetPasswordPage() {
|
||||
id="user_old_password"
|
||||
/>
|
||||
</Form.Item>
|
||||
)
|
||||
}
|
||||
)}
|
||||
|
||||
<Form.Item
|
||||
name="user[password]"
|
||||
|
@ -127,7 +127,6 @@ export default function UserLoginCore() {
|
||||
// 手动获取一遍session
|
||||
getSession()
|
||||
.then((value) => {
|
||||
// @ts-expect-error
|
||||
if (!value?.user?.hasPassword) {
|
||||
if (result_url === "/") {
|
||||
result_url = "/login/set-password";
|
||||
|
19
lib/auth.ts
19
lib/auth.ts
@ -9,6 +9,9 @@ import { isEmail, isName } from "@/lib/auth_list";
|
||||
import {createTransport} from "nodemailer";
|
||||
import { comparePassword } from "@/lib/utils";
|
||||
import { randomBytes } from "crypto";
|
||||
import { type Session } from "next-auth";
|
||||
import { type JWT } from "next-auth/jwt";
|
||||
|
||||
const SECURE_COOKIES:boolean = !!process.env.SECURE_COOKIES;
|
||||
|
||||
let verificationTokens = new Map();
|
||||
@ -153,25 +156,23 @@ export const authOptions: NextAuthOptions = {
|
||||
if (user) {
|
||||
token.user = user;
|
||||
} else {
|
||||
const updateUser = await prisma.user.findUnique({ where: { id: token.sub }});
|
||||
// console.log('========', updateUser)
|
||||
const updateUser: User | null = await prisma.user.findUnique({ where: { id: token.sub }});
|
||||
if (!updateUser || !updateUser.allowToLogin) {
|
||||
throw new Error('无法刷新令牌,用户状态不正确');
|
||||
}
|
||||
token.user = updateUser;
|
||||
token.user = updateUser as User;
|
||||
}
|
||||
return token;
|
||||
},
|
||||
session: async ({ session, token }) => {
|
||||
session: async ({ session, token }: {
|
||||
session: Session,
|
||||
token: JWT
|
||||
}) => {
|
||||
session.user = {
|
||||
...session.user,
|
||||
// @ts-expect-error
|
||||
id: token?.sub,
|
||||
// @ts-expect-error
|
||||
id: token?.sub ?? "",
|
||||
username: token?.user?.username || token?.user?.gh_username,
|
||||
// @ts-expect-error
|
||||
hasPassword: !!token?.user?.password,
|
||||
// @ts-expect-error
|
||||
isAdmin: token?.user?.isAdmin,
|
||||
};
|
||||
// console.log('555555555,', session, token)
|
||||
|
@ -1,21 +1,21 @@
|
||||
import { isName } from "@/lib/auth_list";
|
||||
import { CUS_JWT } from "@/lib/auth_type";
|
||||
import { type JWT } from "next-auth/jwt";
|
||||
|
||||
|
||||
export async function VerifiedUser(session: CUS_JWT | null) {
|
||||
export async function VerifiedUser(session: JWT | null) {
|
||||
const userId = session?.sub
|
||||
const name = session?.email || session?.name
|
||||
return !!(name && isName(name) && userId);
|
||||
}
|
||||
|
||||
export async function VerifiedAdminUser(session: CUS_JWT | null) {
|
||||
export async function VerifiedAdminUser(session: JWT | null) {
|
||||
// console.log('-------', session, session?.user?.isAdmin)
|
||||
return !!session?.user?.isAdmin;
|
||||
// const name = session?.email || session?.name
|
||||
// return !!(name && ADMIN_LIST.includes(name));
|
||||
}
|
||||
|
||||
export function VerifiedNeedSetPassword(path: string, session: CUS_JWT | null,) {
|
||||
export function VerifiedNeedSetPassword(path: string, session: JWT | null,) {
|
||||
const need_set_pwd = !session?.user?.password
|
||||
return path === "/login/set-password" && need_set_pwd;
|
||||
}
|
||||
|
@ -1,6 +0,0 @@
|
||||
import { JWT } from "next-auth/jwt";
|
||||
import { User } from "@prisma/client";
|
||||
|
||||
export type CUS_JWT = JWT & {
|
||||
user: User,
|
||||
}
|
45
lib/types/next-auth.d.ts
vendored
Normal file
45
lib/types/next-auth.d.ts
vendored
Normal file
@ -0,0 +1,45 @@
|
||||
// types/next-auth.d.ts
|
||||
import { DefaultSession, DefaultUser } from "next-auth";
|
||||
|
||||
|
||||
declare module "next-auth" {
|
||||
/**
|
||||
* 扩展 Session 接口,添加自定义的用户属性
|
||||
*/
|
||||
interface Session {
|
||||
user: {
|
||||
id: string;
|
||||
username?: string | null;
|
||||
hasPassword?: boolean | null;
|
||||
isAdmin?: boolean | null;
|
||||
} & DefaultSession["user"];
|
||||
}
|
||||
|
||||
/**
|
||||
* 扩展 User 接口,添加自定义属性
|
||||
* 注意:保持属性可选,以与 AdapterUser 兼容
|
||||
*/
|
||||
interface User extends DefaultUser {
|
||||
id: string;
|
||||
username?: string;
|
||||
gh_username?: string;
|
||||
password?: string;
|
||||
isAdmin?: boolean;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
declare module "next-auth/jwt" {
|
||||
/**
|
||||
* 扩展 JWT 接口,添加自定义的用户属性
|
||||
*/
|
||||
interface JWT {
|
||||
user?: {
|
||||
id: string;
|
||||
username?: string | null;
|
||||
gh_username?: string | null;
|
||||
password?: string | null;
|
||||
isAdmin?: boolean | null;
|
||||
};
|
||||
}
|
||||
}
|
@ -1,9 +1,7 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { getToken } from "next-auth/jwt";
|
||||
import { VerifiedUser, VerifiedAdminUser, VerifiedNeedSetPassword } from "@/lib/auth_client";
|
||||
import { CUS_JWT } from "@/lib/auth_type";
|
||||
|
||||
import { VerifiedUser, VerifiedAdminUser } from "@/lib/auth_client";
|
||||
|
||||
export default async function middleware(req: NextRequest) {
|
||||
const url = req.nextUrl;
|
||||
@ -17,8 +15,8 @@ export default async function middleware(req: NextRequest) {
|
||||
}
|
||||
|
||||
const session = await getToken({ req });
|
||||
const isUser = await VerifiedUser(session as CUS_JWT);
|
||||
const isAdminUser = await VerifiedAdminUser(session as CUS_JWT);
|
||||
const isUser = await VerifiedUser(session);
|
||||
const isAdminUser = await VerifiedAdminUser(session);
|
||||
// console.log('----session', session, '---isUser', isUser, '---isAdmin', isAdminUser)
|
||||
// 管理员页面的api接口还是要认证的
|
||||
if (path.startsWith('/api/admin/')) {
|
||||
@ -45,7 +43,7 @@ export default async function middleware(req: NextRequest) {
|
||||
);
|
||||
}
|
||||
|
||||
// if (VerifiedNeedSetPassword(path, session as CUS_JWT)) {
|
||||
// if (VerifiedNeedSetPassword(path, session)) {
|
||||
// console.log('-0-0-- 需要修改密码', )
|
||||
// // return NextResponse.redirect(new URL("/login/set-password", req.url))
|
||||
// }
|
||||
|
@ -21,7 +21,8 @@
|
||||
],
|
||||
"paths": {
|
||||
"@/*": ["./*"]
|
||||
}
|
||||
},
|
||||
"typeRoots": ["lib/types"]
|
||||
},
|
||||
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts", "app/calcTextareaHeight.ts"],
|
||||
"exclude": ["node_modules"]
|
||||
|
Loading…
Reference in New Issue
Block a user