diff --git a/packages/canyon-platform/app/login/LoginForm.tsx b/packages/canyon-platform/app/login/LoginForm.tsx index 8de2c6c9..2658dbdc 100644 --- a/packages/canyon-platform/app/login/LoginForm.tsx +++ b/packages/canyon-platform/app/login/LoginForm.tsx @@ -1,13 +1,14 @@ // import { useRequest } from "ahooks"; import { FC } from "react"; import { Button, Form, Input } from "antd"; +import { signIn } from "next-auth/react"; const onFinishFailed = (errorInfo: any) => { console.log("Failed:", errorInfo); }; type FieldType = { companyname?: string; - username?: string; + email?: string; password?: string; }; @@ -19,9 +20,13 @@ const LoginForm: FC<{ console.log("Success:", values); // run({ // companyname: String(values.companyname), - // username: String(values.username), // password: String(values.password), // }); + + signIn("credentials", { + password: values.password, + email: values.email, + }); }; return (
- label="Username" - name="username" - rules={[{ required: true, message: "Please input your username!" }]} + label="Email" + name="email" + rules={[{ required: true, message: "Please input your email!" }]} > - + diff --git a/packages/canyon-platform/auth.ts b/packages/canyon-platform/auth.ts index 0db47b9e..4df5ed43 100644 --- a/packages/canyon-platform/auth.ts +++ b/packages/canyon-platform/auth.ts @@ -2,10 +2,33 @@ import NextAuth from "next-auth"; import GitLab from "next-auth/providers/gitlab"; import GitHub from "next-auth/providers/github"; import prisma from "@/lib/prisma"; +import Credentials from "next-auth/providers/credentials"; export const { handlers, auth } = NextAuth({ + // nginx 代理需要设置 trustHost trustHost: true, providers: [ + // 账号密码登录,用于测试 + Credentials({ + credentials: { + password: { label: "Password", type: "password" }, + email: { label: "Email", type: "email" }, + }, + async authorize(c) { + const u = await prisma.user.findFirst({ + where: { + email: c.email as string, + password: c.password as string, + }, + }); + if (!u) return null; + return { + id: u.id, + name: u.nickname, + email: u.email, + }; + }, + }), process.env.AUTH_GITLAB_ORIGIN ? GitLab({ authorization: `${process.env.AUTH_GITLAB_ORIGIN}/oauth/authorize?scope=read_user`, @@ -21,6 +44,10 @@ export const { handlers, auth } = NextAuth({ }, // 登陆的时候从gitlab获取用户信息 async signIn({ profile, account, user }) { + // 对于账号密码登录,不需要数据库处理 + if (account?.provider === "credentials") { + return true; + } const userTest: any = { accessToken: "accessToken", refreshToken: "refreshToken", @@ -52,8 +79,9 @@ export const { handlers, auth } = NextAuth({ }, jwt({ token, user, profile, account }) { if (user) { - // @ts-ignore - token.id = String(account.provider + "-" + profile.id); + token.id = profile + ? String(account?.provider + "-" + profile.id) + : user.id; } return token; },