-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.config.mjs
143 lines (131 loc) · 5.38 KB
/
auth.config.mjs
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import TwitchProvider from "@auth/core/providers/twitch";
import { defineConfig } from "auth-astro";
import { EXTENDED_TWITCH_SCOPES, TWITCH_SCOPES } from "@/lib/Twitch";
import { client } from "@/db/client";
import { UsersTable } from "@/db/schema";
import { getUserSubscription } from "@/utils/user";
import { eq, or } from "drizzle-orm";
export default defineConfig({
providers: [
// Proveedor de Twitch
TwitchProvider({
clientId: import.meta.env.TWITCH_CLIENT_ID,
clientSecret: import.meta.env.TWITCH_CLIENT_SECRET,
authorization: {
params: {
scope: TWITCH_SCOPES.join(" "),
force_verify: true,
},
},
}),
],
callbacks: {
signIn: async ({ user, account, profile }) => {
console.log("signIn", user, account, profile);
if (account.provider === "twitch") {
if (!user.email) {
throw new Error("Email is required to sign in");
}
}
return true;
},
jwt: async ({ token, user, account, profile }) => {
if (user && account?.provider === "twitch") {
token.user = profile;
const email = profile?.email || null;
const username = user?.name?.toLowerCase();
try {
// Buscar si existe un usuario con el email o twitchId
const existingUser = await client
.select()
.from(UsersTable)
.where(or(
eq(UsersTable.twitchId, profile?.sub ?? ""),
email ? eq(UsersTable.email, email) : undefined
))
.limit(1);
if (existingUser.length > 0) {
// Si existe un usuario, vincular cuentas si es necesario
const userId = existingUser[0].id;
const twitchId = profile?.sub;
const twitchTier = await getUserSubscription(twitchId, account.access_token);
// Actualizar la cuenta existente con los detalles de Twitch
await client
.update(UsersTable)
.set({
twitchId,
avatar: profile?.picture,
username,
displayName: profile?.preferred_username ?? username,
twitchTier,
updatedAt: new Date(),
})
.where(eq(UsersTable.id, userId));
// @ts-ignore
token.user.twitchTier = twitchTier;
} else {
// Si no existe un usuario, crear uno nuevo
await client
.insert(UsersTable)
.values({
email,
displayName: profile?.preferred_username ?? username,
avatar: profile?.picture,
username,
twitchId: profile?.sub,
twitchTier: await getUserSubscription(profile?.sub, account.access_token),
});
}
} catch (error) {
console.error("Error managing user:", error);
}
}
return token;
},
session: async ({ session, token }) => {
try {
const userRecord = await client.query.UsersTable.findFirst({
where: eq(UsersTable.twitchId, token.user.sub),
columns: {
id: true,
admin: true,
twitchId: true,
twitchTier: true,
discordId: true,
coins: true,
username: true,
},
with: {
streamerWarsPlayer: {
columns: {
playerNumber: true,
}
}
}
});
if (userRecord) {
session.user.id = userRecord.id;
session.user.username = userRecord.username;
session.user.isAdmin = userRecord.admin;
session.user.twitchId = userRecord.twitchId;
session.user.tier = userRecord.twitchTier;
session.user.discordId = userRecord.discordId;
session.user.coins = userRecord.coins;
session.user.streamerWarsPlayerNumber = userRecord.streamerWarsPlayer?.playerNumber;
}
} catch (error) {
console.error("Error fetching user admin status:", error);
}
return {
...session,
user: {
...session.user,
},
};
},
},
pages: {
error: "/",
signIn: "/",
},
});