-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauth.ts
161 lines (145 loc) · 4.24 KB
/
auth.ts
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// auth.ts
import NextAuth, {
NextAuthOptions,
Session,
DefaultSession,
User,
Account,
Profile,
} from 'next-auth';
import GitHubProvider from 'next-auth/providers/github';
import GoogleProvider from 'next-auth/providers/google';
import { sha256 } from 'hash.js';
import { getServerSession } from 'next-auth/next';
import { headers, cookies as getCookies } from 'next/headers';
import { JWT } from 'next-auth/jwt';
import { nanoid } from 'nanoid';
// Define a custom type for GitHub profile
interface GitHubProfile extends Profile {
id: string;
avatar_url?: string;
picture?: string;
}
if (!process.env.GITHUB_ID) {
console.error(
'[GITHUB_ID] GitHub client ID is not set. Please check your environment variables.'
);
process.exit(1);
}
if (!process.env.GOOGLE_ID) {
console.error(
'[GOOGLE_ID] Google client ID is not set. Please check your environment variables.'
);
process.exit(1);
}
if (!process.env.GITHUB_SECRET) {
console.error(
'[GITHUB_SECRET] GitHub client secret is not set. Please check your environment variables.'
);
process.exit(1);
}
if (!process.env.GOOGLE_SECRET) {
console.error(
'[GOOGLE_SECRET] Google client secret is not set. Please check your environment variables.'
);
process.exit(1);
}
declare module 'next-auth' {
interface Session {
user: {
/** The user's id. */
id: string | null; // Allow null for anonymous users
} & DefaultSession['user'];
}
}
export const authOptions: NextAuthOptions = {
secret: process.env.NEXTAUTH_SECRET,
providers: [
GitHubProvider({
clientId: process.env.GITHUB_ID!,
clientSecret: process.env.GITHUB_SECRET!,
}),
GoogleProvider({
clientId: process.env.GOOGLE_ID!,
clientSecret: process.env.GOOGLE_SECRET!,
}),
],
callbacks: {
jwt({
token,
user,
account,
profile,
}: {
token: JWT;
user?: User;
account?: Account | null;
profile?: Profile;
}) {
if (account?.provider === 'google') {
// Generate a consistent userId for Google users based on their email
if (user?.email) {
token.id = sha256().update(user.email).digest('hex');
} else {
console.warn('Google authenticated user without an email.');
}
} else if (account?.provider === 'github' && profile) {
// Cast profile to GitHubProfile for GitHub-specific fields
const githubProfile = profile as GitHubProfile;
token.id = githubProfile.id;
token.image = githubProfile.avatar_url || githubProfile.picture;
}
return token;
},
session({
session,
token,
}: {
session: Session;
token: JWT;
}) {
if (token?.id) {
session.user.id = token.id as string; // Cast token.id as string
} else {
session.user.id = null; // Set id to null for anonymous users
}
return session;
},
},
pages: {
signIn: '/sign-in', // Custom sign-in page
},
};
// Initialize NextAuth
export default NextAuth(authOptions);
// Export GET and POST handlers
export const GET = (req: Request) => NextAuth(authOptions)(req);
export const POST = (req: Request) => NextAuth(authOptions)(req);
// Custom auth function
export async function auth() {
const session = await getServerSession(authOptions);
if (session) {
return session;
}
// Get the hostname from the request headers
const headersList = headers();
const host = headersList.get('host') || '';
const isMevMainDomain = host === 'mev.fyi' || host === `mev.fyi:${process.env.PORT}`;
if (isMevMainDomain) {
// Read 'anonymousId' from cookies
const cookieStore = getCookies();
const anonymousId = cookieStore.get('anonymousId')?.value;
if (anonymousId) {
// Return the session with the anonymousId
return { user: { id: anonymousId, name: 'Anonymous' } };
} else {
// This should not happen since middleware sets the cookie
// But as a fallback, generate a new anonymousId
const newAnonymousId = nanoid();
console.warn('anonymousId cookie missing for mev.fyi. Middleware should set it.');
return { user: { id: newAnonymousId, name: 'Anonymous' } };
}
}
// For other hosts (app.mev.fyi), enforce authentication
return null;
}