-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathauth.ts
More file actions
87 lines (86 loc) · 2.82 KB
/
Copy pathauth.ts
File metadata and controls
87 lines (86 loc) · 2.82 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import NextAuth from "next-auth";
import { DrizzleAdapter } from "@auth/drizzle-adapter";
import Google from "next-auth/providers/google";
import Credentials from "next-auth/providers/credentials";
import { eq } from "drizzle-orm";
import bcrypt from "bcryptjs";
import { db } from "@/db";
import { users, accounts, sessions, verificationTokens } from "@/db/schema";
import { validateInitData } from "@/lib/telegram/validateInitData";
import { upsertTelegramUser } from "@/lib/auth/upsertTelegramUser";
export const { handlers, auth, signIn, signOut } = NextAuth({
adapter: DrizzleAdapter(db, {
usersTable: users,
accountsTable: accounts,
sessionsTable: sessions,
verificationTokensTable: verificationTokens,
}),
// Credentials sağlayıcıları için JWT oturum zorunlu
session: { strategy: "jwt" },
trustHost: true,
providers: [
Google,
// Telegram Mini App — initData ile (sürtünmesiz, birincil)
Credentials({
id: "telegram-miniapp",
name: "Telegram Mini App",
credentials: { initData: { label: "initData", type: "text" } },
async authorize(credentials) {
const initData = credentials?.initData;
if (typeof initData !== "string") return null;
const token = process.env.TELEGRAM_BOT_TOKEN;
if (!token) return null;
try {
const { user: tgUser, startParam } = validateInitData(initData, token);
const user = await upsertTelegramUser(tgUser, startParam);
return {
id: user.id,
name: user.name,
image: user.image,
};
} catch {
return null;
}
},
}),
// E-posta + şifre (web ikincil)
Credentials({
id: "password",
name: "E-posta",
credentials: {
email: { label: "E-posta", type: "email" },
password: { label: "Şifre", type: "password" },
},
async authorize(credentials) {
const email = credentials?.email;
const password = credentials?.password;
if (typeof email !== "string" || typeof password !== "string") {
return null;
}
const user = await db.query.users.findFirst({
where: eq(users.email, email.toLowerCase()),
});
if (!user?.passwordHash) return null;
const ok = await bcrypt.compare(password, user.passwordHash);
if (!ok) return null;
return { id: user.id, name: user.name, email: user.email, image: user.image };
},
}),
],
callbacks: {
// user.id'yi JWT ve session'a taşı
async jwt({ token, user }) {
if (user?.id) token.uid = user.id;
return token;
},
async session({ session, token }) {
if (token.uid && session.user) {
session.user.id = token.uid as string;
}
return session;
},
},
pages: {
signIn: "/login",
},
});