Repository navigation
Expand file tree
/
Copy pathauth.ts
More file actions
140 lines (133 loc) · 5.03 KB
/
Copy pathauth.ts
File metadata and controls
140 lines (133 loc) · 5.03 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
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
import { readFileSync } from "node:fs";
import NextAuth from "next-auth";
import type { Provider } from "next-auth/providers";
import GitHub from "next-auth/providers/github";
import Google from "next-auth/providers/google";
import { DrizzleAdapter } from "@auth/drizzle-adapter";
import { and, eq } from "drizzle-orm";
import { db } from "@/db";
import { accounts, profiles, sessions, users, verificationTokens, auditLog } from "@/db/schema";
/**
* GitHub: any GitHub account may sign in, but only with an email GitHub has verified.
* Auth.js's GitHub provider takes the email without checking `verified`, so we check here.
* Without this, someone could sign up with another person's unverified address.
*/
async function githubEmailIsVerified(accessToken: string | undefined, email: string | null | undefined) {
if (!accessToken || !email) return false;
const res = await fetch("https://api.github.com/user/emails", {
headers: {
Authorization: `Bearer ${accessToken}`,
Accept: "application/vnd.github+json",
"User-Agent": "eris-vault",
},
cache: "no-store",
});
if (!res.ok) return false;
const list = (await res.json()) as { email: string; verified: boolean }[];
return list.some((e) => e.verified && e.email.toLowerCase() === email.toLowerCase());
}
async function isBlocked(provider: string, providerAccountId: string, email: string | null | undefined) {
const byAccount = await db
.select({ status: profiles.status })
.from(accounts)
.innerJoin(profiles, eq(profiles.userId, accounts.userId))
.where(and(eq(accounts.provider, provider), eq(accounts.providerAccountId, providerAccountId)))
.limit(1);
if (byAccount[0]?.status === "blocked") return true;
if (!email) return false;
const byEmail = await db
.select({ status: profiles.status })
.from(users)
.innerJoin(profiles, eq(profiles.userId, users.id))
.where(eq(users.email, email))
.limit(1);
return byEmail[0]?.status === "blocked";
}
const providers: Provider[] = [
GitHub({
clientId: process.env.AUTH_GITHUB_ID,
clientSecret: process.env.AUTH_GITHUB_SECRET,
}),
];
// Google is added once its keys exist on Railway. Google verifies emails, so a
// Google sign-in may join an existing account with the same email. GitHub may not.
if (process.env.AUTH_GOOGLE_ID && process.env.AUTH_GOOGLE_SECRET) {
providers.push(
Google({
clientId: process.env.AUTH_GOOGLE_ID,
clientSecret: process.env.AUTH_GOOGLE_SECRET,
allowDangerousEmailAccountLinking: true,
}),
);
}
/** AUTH_SECRET from the environment, else the one the server generated at boot (scripts/migrate.mjs). */
function authSecret(): string | undefined {
if (process.env.AUTH_SECRET) return process.env.AUTH_SECRET;
try {
return readFileSync(".auth-secret", "utf8").trim() || undefined;
} catch {
return undefined;
}
}
export const enabledProviders = providers.map((p) => (typeof p === "function" ? p().id : p.id));
export const { handlers, auth, signIn, signOut } = NextAuth({
adapter: DrizzleAdapter(db, {
usersTable: users,
accountsTable: accounts,
sessionsTable: sessions,
verificationTokensTable: verificationTokens,
}),
providers,
secret: authSecret(),
trustHost: true,
session: { strategy: "database" },
pages: { signIn: "/", error: "/auth-error" },
callbacks: {
async signIn({ user, account, profile }) {
if (!account) return false;
if (await isBlocked(account.provider, account.providerAccountId, user.email)) {
return "/blocked";
}
if (account.provider === "github") {
const ok = await githubEmailIsVerified(account.access_token, user.email);
if (!ok) return "/auth-error?error=GitHubEmailNotVerified";
}
if (account.provider === "google") {
const verified = (profile as { email_verified?: boolean } | undefined)?.email_verified;
if (!verified) return "/auth-error?error=GoogleEmailNotVerified";
}
return true;
},
session({ session, user }) {
if (session.user && user) session.user.id = user.id;
return session;
},
},
events: {
// Logged only when a sign-in is added to an account that already had one
// (the Linked sign-ins page); a brand new account is logged by createUser.
async linkAccount({ user, account }) {
if (!user.id) return;
const all = await db.select({ provider: accounts.provider }).from(accounts).where(eq(accounts.userId, user.id));
if (all.length < 2) return;
await db.insert(auditLog).values({
actorId: user.id,
actorRole: "system",
targetUserId: user.id,
action: "account.linked",
after: { provider: account.provider },
});
},
async createUser({ user }) {
if (!user.id) return;
await db.insert(profiles).values({ userId: user.id }).onConflictDoNothing();
await db.insert(auditLog).values({
actorId: user.id,
actorRole: "system",
targetUserId: user.id,
action: "account.created",
after: { name: user.name ?? null },
});
},
},
});