This repository was archived by the owner on Feb 26, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.ts
More file actions
117 lines (114 loc) · 3.39 KB
/
auth.ts
File metadata and controls
117 lines (114 loc) · 3.39 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
import NextAuth from "next-auth";
import Credentials from "next-auth/providers/credentials";
import GitHub from "next-auth/providers/github";
import Google from "next-auth/providers/google";
import type { TierName } from "@/lib/types";
declare module "next-auth" {
interface User {
tier?: TierName;
apiKey?: string | null;
}
interface Session {
user: {
id: string;
email: string;
name?: string | null;
image?: string | null;
tier: TierName;
apiKey: string | null;
};
}
}
export const { handlers, signIn, signOut, auth } = NextAuth({
providers: [
Credentials({
name: "Email",
credentials: {
email: { label: "Email", type: "email" },
code: { label: "Code", type: "text" },
},
async authorize(credentials) {
const email = credentials?.email as string;
const code = credentials?.code as string;
if (!email || !code) return null;
const apiUrl = process.env.NEXT_PUBLIC_MIDOS_API_URL ?? "http://localhost:8420";
try {
const res = await fetch(`${apiUrl}/api/auth/verify-code`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ email, code }),
});
if (!res.ok) return null;
const user = await res.json();
return {
id: user.user_id,
email: user.email,
name: user.name ?? null,
tier: user.tier ?? "community",
apiKey: user.api_key ?? null,
};
} catch {
return null;
}
},
}),
GitHub({
clientId: process.env.GITHUB_ID ?? "",
clientSecret: process.env.GITHUB_SECRET ?? "",
}),
Google({
clientId: process.env.GOOGLE_ID ?? "",
clientSecret: process.env.GOOGLE_SECRET ?? "",
}),
],
session: {
strategy: "jwt",
maxAge: 30 * 24 * 60 * 60, // 30 days
},
pages: {
signIn: "/login",
newUser: "/dashboard",
},
callbacks: {
async signIn({ user, account }) {
// OAuth users: register/link with backend on first sign-in
if (account?.provider !== "credentials" && user.email) {
const apiUrl = process.env.NEXT_PUBLIC_MIDOS_API_URL ?? "http://localhost:8420";
try {
const res = await fetch(`${apiUrl}/api/auth/oauth`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
email: user.email,
name: user.name,
provider: account?.provider,
provider_id: account?.providerAccountId,
}),
});
if (res.ok) {
const data = await res.json();
user.tier = data.tier ?? "community";
user.apiKey = data.api_key ?? null;
user.id = data.user_id;
}
} catch {
// Allow sign-in even if backend link fails — user gets default tier
}
}
return true;
},
async jwt({ token, user }) {
if (user) {
token.tier = user.tier ?? "community";
token.apiKey = user.apiKey ?? null;
}
return token;
},
async session({ session, token }) {
session.user.id = token.sub!;
session.user.tier = (token.tier as TierName) ?? "community";
session.user.apiKey = (token.apiKey as string) ?? null;
return session;
},
},
});