middle 2
This commit is contained in:
26
auth.ts
26
auth.ts
@@ -2,10 +2,14 @@
|
||||
import NextAuth from "next-auth";
|
||||
import Google from "next-auth/providers/google";
|
||||
import { PrismaClient } from "@/app/generated/prisma";
|
||||
import { randomBytes } from "crypto";
|
||||
|
||||
function generateRegisterCode(): string {
|
||||
return randomBytes(16).toString("hex");
|
||||
async function generateRegisterCode(): Promise<string> {
|
||||
// Library-free random (Edge 호환). 16바이트 hex
|
||||
let out = "";
|
||||
for (let i = 0; i < 16; i++) {
|
||||
const byte = Math.floor(Math.random() * 256);
|
||||
out += byte.toString(16).padStart(2, "0");
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
export const { handlers, auth, signIn, signOut } = NextAuth({
|
||||
@@ -16,18 +20,10 @@ export const { handlers, auth, signIn, signOut } = NextAuth({
|
||||
}),
|
||||
|
||||
],
|
||||
// callbacks 제거: 세션에 registerCode를 포함하지 않음
|
||||
|
||||
events: {
|
||||
async signIn({ user, account, profile }) {
|
||||
|
||||
console.log("[NextAuth] signIn user:", {
|
||||
id: (user as any)?.id,
|
||||
name: user?.name,
|
||||
email: user?.email,
|
||||
image: (user as any)?.image,
|
||||
provider: account?.provider,
|
||||
});
|
||||
|
||||
// Upsert User (by email)
|
||||
try {
|
||||
const prisma = new PrismaClient();
|
||||
@@ -41,7 +37,7 @@ export const { handlers, auth, signIn, signOut } = NextAuth({
|
||||
if (existing) {
|
||||
const registerCodeData = existing.RegisgerCode
|
||||
? {}
|
||||
: { RegisgerCode: generateRegisterCode() };
|
||||
: { RegisgerCode: await generateRegisterCode() };
|
||||
await prisma.user.update({
|
||||
where: { id: existing.id },
|
||||
data: {
|
||||
@@ -54,7 +50,7 @@ export const { handlers, auth, signIn, signOut } = NextAuth({
|
||||
data: {
|
||||
email,
|
||||
icon: (user as any)?.image ?? "",
|
||||
RegisgerCode: generateRegisterCode(),
|
||||
RegisgerCode: await generateRegisterCode(),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user