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) => {
|
const onFinish: FormProps<FieldType>["onFinish"] = (values) => {
|
||||||
// setLoading(true);
|
// setLoading(true);
|
||||||
// console.log('-------------', values)
|
// console.log('-------------', values)
|
||||||
// @ts-expect-error
|
|
||||||
fetch(`/api/user/${session?.user?.id}`, {
|
fetch(`/api/user/${session?.user?.id}`, {
|
||||||
method: "PUT",
|
method: "PUT",
|
||||||
credentials: "include",
|
credentials: "include",
|
||||||
@ -61,31 +60,28 @@ export default function SetPasswordPage() {
|
|||||||
layout="vertical"
|
layout="vertical"
|
||||||
onFinish={onFinish}
|
onFinish={onFinish}
|
||||||
>
|
>
|
||||||
{
|
{status === "authenticated" && session?.user?.hasPassword && (
|
||||||
// @ts-expect-error
|
<Form.Item
|
||||||
status === "authenticated" && session?.user?.hasPassword && (
|
name="user[old_password]"
|
||||||
<Form.Item
|
label="Old password"
|
||||||
name="user[old_password]"
|
rules={[
|
||||||
label="Old password"
|
{
|
||||||
rules={[
|
validator: async (_, value) => {
|
||||||
{
|
if (!value) {
|
||||||
validator: async (_, value) => {
|
return Promise.reject(new Error("请填写该字段"));
|
||||||
if (!value) {
|
}
|
||||||
return Promise.reject(new Error("请填写该字段"));
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
]}
|
},
|
||||||
>
|
]}
|
||||||
<Input
|
>
|
||||||
prefix={<LockOutlined className="site-form-item-icon" />}
|
<Input
|
||||||
type="password"
|
prefix={<LockOutlined className="site-form-item-icon" />}
|
||||||
autoComplete="current-password"
|
type="password"
|
||||||
id="user_old_password"
|
autoComplete="current-password"
|
||||||
/>
|
id="user_old_password"
|
||||||
</Form.Item>
|
/>
|
||||||
)
|
</Form.Item>
|
||||||
}
|
)}
|
||||||
|
|
||||||
<Form.Item
|
<Form.Item
|
||||||
name="user[password]"
|
name="user[password]"
|
||||||
|
@ -127,7 +127,6 @@ export default function UserLoginCore() {
|
|||||||
// 手动获取一遍session
|
// 手动获取一遍session
|
||||||
getSession()
|
getSession()
|
||||||
.then((value) => {
|
.then((value) => {
|
||||||
// @ts-expect-error
|
|
||||||
if (!value?.user?.hasPassword) {
|
if (!value?.user?.hasPassword) {
|
||||||
if (result_url === "/") {
|
if (result_url === "/") {
|
||||||
result_url = "/login/set-password";
|
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 {createTransport} from "nodemailer";
|
||||||
import { comparePassword } from "@/lib/utils";
|
import { comparePassword } from "@/lib/utils";
|
||||||
import { randomBytes } from "crypto";
|
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;
|
const SECURE_COOKIES:boolean = !!process.env.SECURE_COOKIES;
|
||||||
|
|
||||||
let verificationTokens = new Map();
|
let verificationTokens = new Map();
|
||||||
@ -153,25 +156,23 @@ export const authOptions: NextAuthOptions = {
|
|||||||
if (user) {
|
if (user) {
|
||||||
token.user = user;
|
token.user = user;
|
||||||
} else {
|
} else {
|
||||||
const updateUser = await prisma.user.findUnique({ where: { id: token.sub }});
|
const updateUser: User | null = await prisma.user.findUnique({ where: { id: token.sub }});
|
||||||
// console.log('========', updateUser)
|
|
||||||
if (!updateUser || !updateUser.allowToLogin) {
|
if (!updateUser || !updateUser.allowToLogin) {
|
||||||
throw new Error('无法刷新令牌,用户状态不正确');
|
throw new Error('无法刷新令牌,用户状态不正确');
|
||||||
}
|
}
|
||||||
token.user = updateUser;
|
token.user = updateUser as User;
|
||||||
}
|
}
|
||||||
return token;
|
return token;
|
||||||
},
|
},
|
||||||
session: async ({ session, token }) => {
|
session: async ({ session, token }: {
|
||||||
|
session: Session,
|
||||||
|
token: JWT
|
||||||
|
}) => {
|
||||||
session.user = {
|
session.user = {
|
||||||
...session.user,
|
...session.user,
|
||||||
// @ts-expect-error
|
id: token?.sub ?? "",
|
||||||
id: token?.sub,
|
|
||||||
// @ts-expect-error
|
|
||||||
username: token?.user?.username || token?.user?.gh_username,
|
username: token?.user?.username || token?.user?.gh_username,
|
||||||
// @ts-expect-error
|
|
||||||
hasPassword: !!token?.user?.password,
|
hasPassword: !!token?.user?.password,
|
||||||
// @ts-expect-error
|
|
||||||
isAdmin: token?.user?.isAdmin,
|
isAdmin: token?.user?.isAdmin,
|
||||||
};
|
};
|
||||||
// console.log('555555555,', session, token)
|
// console.log('555555555,', session, token)
|
||||||
|
@ -1,21 +1,21 @@
|
|||||||
import { isName } from "@/lib/auth_list";
|
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 userId = session?.sub
|
||||||
const name = session?.email || session?.name
|
const name = session?.email || session?.name
|
||||||
return !!(name && isName(name) && userId);
|
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)
|
// console.log('-------', session, session?.user?.isAdmin)
|
||||||
return !!session?.user?.isAdmin;
|
return !!session?.user?.isAdmin;
|
||||||
// const name = session?.email || session?.name
|
// const name = session?.email || session?.name
|
||||||
// return !!(name && ADMIN_LIST.includes(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
|
const need_set_pwd = !session?.user?.password
|
||||||
return path === "/login/set-password" && need_set_pwd;
|
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 { 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 { VerifiedUser, VerifiedAdminUser, VerifiedNeedSetPassword } from "@/lib/auth_client";
|
import { VerifiedUser, VerifiedAdminUser } from "@/lib/auth_client";
|
||||||
import { CUS_JWT } from "@/lib/auth_type";
|
|
||||||
|
|
||||||
|
|
||||||
export default async function middleware(req: NextRequest) {
|
export default async function middleware(req: NextRequest) {
|
||||||
const url = req.nextUrl;
|
const url = req.nextUrl;
|
||||||
@ -17,8 +15,8 @@ export default async function middleware(req: NextRequest) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const session = await getToken({ req });
|
const session = await getToken({ req });
|
||||||
const isUser = await VerifiedUser(session as CUS_JWT);
|
const isUser = await VerifiedUser(session);
|
||||||
const isAdminUser = await VerifiedAdminUser(session as CUS_JWT);
|
const isAdminUser = await VerifiedAdminUser(session);
|
||||||
// console.log('----session', session, '---isUser', isUser, '---isAdmin', isAdminUser)
|
// console.log('----session', session, '---isUser', isUser, '---isAdmin', isAdminUser)
|
||||||
// 管理员页面的api接口还是要认证的
|
// 管理员页面的api接口还是要认证的
|
||||||
if (path.startsWith('/api/admin/')) {
|
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-- 需要修改密码', )
|
// console.log('-0-0-- 需要修改密码', )
|
||||||
// // return NextResponse.redirect(new URL("/login/set-password", req.url))
|
// // return NextResponse.redirect(new URL("/login/set-password", req.url))
|
||||||
// }
|
// }
|
||||||
|
@ -21,7 +21,8 @@
|
|||||||
],
|
],
|
||||||
"paths": {
|
"paths": {
|
||||||
"@/*": ["./*"]
|
"@/*": ["./*"]
|
||||||
}
|
},
|
||||||
|
"typeRoots": ["lib/types"]
|
||||||
},
|
},
|
||||||
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts", "app/calcTextareaHeight.ts"],
|
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts", "app/calcTextareaHeight.ts"],
|
||||||
"exclude": ["node_modules"]
|
"exclude": ["node_modules"]
|
||||||
|
Loading…
Reference in New Issue
Block a user