测试增加邮件认证

This commit is contained in:
sijinhui
2024-03-22 14:49:10 +08:00
parent 8efbed2c28
commit c84e223ca5
9 changed files with 95 additions and 30 deletions

View File

@@ -1,5 +1,6 @@
import { getServerSession, type NextAuthOptions } from "next-auth";
import GitHubProvider from "next-auth/providers/github";
import EmailProvider from "next-auth/providers/email";
import CredentialsProvider from "next-auth/providers/credentials";
import { PrismaAdapter } from "@next-auth/prisma-adapter";
import prisma from "@/lib/prisma";
@@ -27,6 +28,17 @@ export const authOptions: NextAuthOptions = {
};
},
}),
EmailProvider({
server: {
host: process.env.EMAIL_SERVER_HOST,
port: process.env.EMAIL_SERVER_PORT,
auth: {
user: process.env.EMAIL_SERVER_USER,
pass: process.env.EMAIL_SERVER_PASSWORD,
},
},
from: process.env.EMAIL_FROM,
}),
CredentialsProvider({
// The name to display on the sign in form (e.g. "Sign in with...")
name: "Credentials",
@@ -66,7 +78,7 @@ export const authOptions: NextAuthOptions = {
],
pages: {
signIn: `/login`,
verifyRequest: `/login`,
// verifyRequest: `/login`,
error: "/login", // Error code passed in query string as ?error=
},
adapter: PrismaAdapter(prisma),
@@ -99,7 +111,7 @@ export const authOptions: NextAuthOptions = {
session.user = {
...session.user,
// @ts-expect-error
id: token.sub,
id: token?.sub,
// @ts-expect-error
username: token?.user?.username || token?.user?.gh_username,
};

View File

@@ -1,4 +1,4 @@
import bcrypt from "bcryptjs";
import {get_encoding} from "tiktoken";
@@ -18,7 +18,6 @@ export async function fetcher<JSON = any>(
}
export const capitalize = (s: string) => {
if (typeof s !== "string") return "";
return s.charAt(0).toUpperCase() + s.slice(1);
};
@@ -61,3 +60,13 @@ export const toDateString = (date: Date) => {
export const random = (min: number, max: number) => {
return Math.floor(Math.random() * (max - min + 1) + min);
};
// 将明文处理为 hash
export function hashPassword(password: string) {
return bcrypt.hashSync(password, 10);
}
// 对比明文和 hash 是否一致
export function comparePassword(password: string, hashPassword: string) {
return bcrypt.compareSync(password, hashPassword);
}