Skip to content

Commit

Permalink
chore: daily development
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangtao25 committed Dec 10, 2024
1 parent 5587f22 commit 9206c33
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
17 changes: 11 additions & 6 deletions packages/canyon-platform/app/login/LoginForm.tsx
Original file line number Diff line number Diff line change
@@ -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;
};

Expand All @@ -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 (
<Form
Expand All @@ -33,11 +38,11 @@ const LoginForm: FC<{
onFinishFailed={onFinishFailed}
>
<Form.Item<FieldType>
label="Username"
name="username"
rules={[{ required: true, message: "Please input your username!" }]}
label="Email"
name="email"
rules={[{ required: true, message: "Please input your email!" }]}
>
<Input placeholder={"Username or Email"} />
<Input placeholder={"Email"} />
</Form.Item>

<Form.Item<FieldType>
Expand Down
32 changes: 30 additions & 2 deletions packages/canyon-platform/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand All @@ -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",
Expand Down Expand Up @@ -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;
},
Expand Down

0 comments on commit 9206c33

Please sign in to comment.