-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathauth.ts
More file actions
191 lines (169 loc) · 5.84 KB
/
auth.ts
File metadata and controls
191 lines (169 loc) · 5.84 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import NextAuth from "next-auth"
import "next-auth/jwt"
import GitHub from "next-auth/providers/github"
import Google from "next-auth/providers/google"
import { DrizzleAdapter } from "@auth/drizzle-adapter"
import Notion from "next-auth/providers/notion"
import { db } from "./db"
import { credits, workspaces, memberInWorkspace, workspaceInvitations } from "./db/schema"
import { eq } from "drizzle-orm"
// Utility functions
const createId = () => crypto.randomUUID();
const getNewUserInvitations = async (email: string) => {
// Get workspace invitations for this email
const invitations = await db
.select()
.from(workspaceInvitations)
.where(eq(workspaceInvitations.email, email));
return {
invitations: [], // For user invitations (if you have a separate user invitations table)
workspaceInvitations: invitations
};
};
const parseWorkspaceDefaultPlan = (email: string): 'FREE' | 'STARTER' | 'PRO' | 'ENTERPRISE' => {
// Check if admin email
const adminEmails = process.env.ADMIN_EMAIL?.split(",") || [];
if (adminEmails.includes(email)) {
return 'PRO';
}
return 'FREE';
};
const convertInvitationsToCollaborations = async (user: any, invitations: any[]) => {
// Implementation for converting invitations to collaborations
// This would typically involve creating collaboration records
console.log('Converting invitations to collaborations', { user, invitations });
};
const joinWorkspaces = async (user: any, userWorkspaceInvitations: any[]) => {
// Add user to workspaces based on invitations
for (const invitation of userWorkspaceInvitations) {
await db.insert(memberInWorkspace).values({
userId: user.id,
workspaceId: invitation.workspaceId,
role: invitation.role
}).onConflictDoNothing();
// Remove the invitation after joining
await db.delete(workspaceInvitations).where(eq(workspaceInvitations.id, invitation.id));
}
};
export const { handlers, auth, signIn, signOut } = NextAuth({
debug: !!process.env.AUTH_DEBUG,
theme: { logo: "https://authjs.dev/img/logo-sm.png" },
adapter: DrizzleAdapter(db),
providers: [
GitHub({
clientId: process.env.GITHUB_ID,
clientSecret: process.env.GITHUB_SECRET,
allowDangerousEmailAccountLinking: true,
}),
Notion({
clientId: process.env.AUTH_NOTION_ID,
clientSecret: process.env.AUTH_NOTION_SECRET,
redirectUri: process.env.AUTH_NOTION_REDIRECT_URI as string,
allowDangerousEmailAccountLinking: true,
token: {
url: "https://api.notion.com/v1/oauth/token",
async conform(response: Response) {
const body = await response.json();
body.refresh_token = '1234567890';
return new Response(JSON.stringify(body), response);
},
},
}),
Google({
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
allowDangerousEmailAccountLinking: true,
}),
],
basePath: "/api/auth",
session: { strategy: "jwt" },
callbacks: {
authorized({ request, auth }) {
const { pathname } = request.nextUrl
if (pathname === "/middleware-example") return !!auth
return true
},
jwt({ token, trigger, session, account }) {
if (trigger === "update") token.name = session.user.name
if (account?.provider === "keycloak") {
return { ...token, accessToken: account.access_token }
}
return token
},
async session({ session, token }) {
session.user = {
...session.user,
// @ts-expect-error
id: token.sub,
// @ts-expect-error
username: token?.user?.username || token?.user?.gh_username,
};
return session;
},
},
events: {
async createUser({ user }) {
if (!user.email) throw Error("Provider did not forward email but it is required");
const userData = {
id: user.id || createId(),
email: user.email as string,
name: user.name
};
const { invitations, workspaceInvitations } = await getNewUserInvitations(userData.email);
// Check if signup is disabled and user is not admin or invited
if (
process.env.DISABLE_SIGNUP &&
process.env.ADMIN_EMAIL?.split(",")?.every((email) => email !== userData.email) &&
invitations.length === 0 &&
workspaceInvitations.length === 0
) {
throw Error("New users are forbidden");
}
const workspaceId = createId();
let oneApiToken = "";
let tokenId = null;
// Create workspace only if user doesn't have workspace invitations
if (workspaceInvitations.length === 0) {
const newWorkspaceData = {
id: workspaceId,
name: userData.name ? `${userData.name}'s workspace` : "My workspace",
plan: parseWorkspaceDefaultPlan(userData.email),
oneApiToken,
tokenId,
};
// Create workspace
await db.insert(workspaces).values(newWorkspaceData);
// Add user as admin to the workspace
await db.insert(memberInWorkspace).values({
userId: userData.id,
workspaceId: workspaceId,
role: 'ADMIN'
});
// Initialize credits for the workspace
await db.insert(credits).values({
workspaceId: workspaceId,
totalCredits: 5, // Default credits for new workspaces
usedCredits: 0,
});
}
// Handle invitations
if (invitations.length > 0) {
await convertInvitationsToCollaborations(userData, invitations);
}
if (workspaceInvitations.length > 0) {
await joinWorkspaces(userData, workspaceInvitations);
}
}
},
experimental: { enableWebAuthn: true },
})
declare module "next-auth" {
interface Session {
accessToken?: string
}
}
declare module "next-auth/jwt" {
interface JWT {
accessToken?: string
}
}