diff --git a/app/app/(auth)/login/set-password/page.tsx b/app/app/(auth)/login/set-password/page.tsx index 2ee04158c..1e0f60ff9 100644 --- a/app/app/(auth)/login/set-password/page.tsx +++ b/app/app/(auth)/login/set-password/page.tsx @@ -35,7 +35,6 @@ export default function SetPasswordPage() { const onFinish: FormProps["onFinish"] = (values) => { // setLoading(true); // console.log('-------------', values) - // @ts-expect-error fetch(`/api/user/${session?.user?.id}`, { method: "PUT", credentials: "include", @@ -61,31 +60,28 @@ export default function SetPasswordPage() { layout="vertical" onFinish={onFinish} > - { - // @ts-expect-error - status === "authenticated" && session?.user?.hasPassword && ( - { - if (!value) { - return Promise.reject(new Error("请填写该字段")); - } - }, + {status === "authenticated" && session?.user?.hasPassword && ( + { + if (!value) { + return Promise.reject(new Error("请填写该字段")); + } }, - ]} - > - } - type="password" - autoComplete="current-password" - id="user_old_password" - /> - - ) - } + }, + ]} + > + } + type="password" + autoComplete="current-password" + id="user_old_password" + /> + + )} { - // @ts-expect-error if (!value?.user?.hasPassword) { if (result_url === "/") { result_url = "/login/set-password"; diff --git a/lib/auth.ts b/lib/auth.ts index b698f82f4..7562a60a8 100644 --- a/lib/auth.ts +++ b/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) diff --git a/lib/auth_client.ts b/lib/auth_client.ts index 143480963..5b65b200f 100644 --- a/lib/auth_client.ts +++ b/lib/auth_client.ts @@ -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; } diff --git a/lib/auth_type.ts b/lib/auth_type.ts deleted file mode 100644 index 28623eb73..000000000 --- a/lib/auth_type.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { JWT } from "next-auth/jwt"; -import { User } from "@prisma/client"; - -export type CUS_JWT = JWT & { - user: User, -} diff --git a/lib/types/next-auth.d.ts b/lib/types/next-auth.d.ts new file mode 100644 index 000000000..f062194b2 --- /dev/null +++ b/lib/types/next-auth.d.ts @@ -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; + }; + } +} diff --git a/middleware.ts b/middleware.ts index 1a8c7ff79..745b4314f 100644 --- a/middleware.ts +++ b/middleware.ts @@ -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)) // } diff --git a/tsconfig.json b/tsconfig.json index c73eef3e8..ce1a04b38 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -21,7 +21,8 @@ ], "paths": { "@/*": ["./*"] - } + }, + "typeRoots": ["lib/types"] }, "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts", "app/calcTextareaHeight.ts"], "exclude": ["node_modules"]