-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.ts
More file actions
69 lines (65 loc) · 2.06 KB
/
Copy pathauth.ts
File metadata and controls
69 lines (65 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import NextAuth from "next-auth";
import { PrismaAdapter } from "@auth/prisma-adapter";
import Resend from "next-auth/providers/resend";
import Credentials from "next-auth/providers/credentials";
import { db } from "@/lib/db";
import authConfig from "./auth.config";
import { slugify } from "@/lib/slug";
export const { handlers, auth, signIn, signOut } = NextAuth({
adapter: PrismaAdapter(db),
session: { strategy: "jwt" },
callbacks: {
async jwt({ token, user }) {
if (user) token["id"] = user.id;
return token;
},
async session({ session, token }) {
if (token["id"] && session.user) {
session.user.id = token["id"] as string;
}
return session;
},
},
events: {
async createUser({ user }) {
if (!user.id || !user.email) return;
const base = slugify(user.name ?? user.email.split("@")[0] ?? "board");
let slug = base || "board";
let suffix = 0;
while (await db.board.findUnique({ where: { slug } })) {
suffix += 1;
slug = `${base}-${suffix}`;
}
await db.board.create({
data: {
slug,
name: user.name ? `${user.name}'s board` : "My board",
ownerId: user.id,
},
});
},
},
...authConfig,
providers: [
...authConfig.providers,
Resend({ from: process.env["EMAIL_FROM"] }), // Resend needs PrismaAdapter — kept here only, not in auth.config
Credentials({
id: "demo",
credentials: {},
async authorize() {
if (process.env["DEMO_MODE"] !== "true") return null;
const demoEmail = "demo@feedbackflow.app";
let user = await db.user.findUnique({ where: { email: demoEmail } });
if (!user) {
user = await db.user.create({
data: { email: demoEmail, name: "Demo User", emailVerified: new Date() },
});
await db.board.create({
data: { slug: "demo", name: "Demo board", description: "Try FeedbackFlow with a pre-seeded board.", ownerId: user.id },
});
}
return user;
},
}),
],
});