@convex-dev/better-auth 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +44 -0
- package/dist/commonjs/client/adapter.d.ts +4 -0
- package/dist/commonjs/client/adapter.d.ts.map +1 -0
- package/dist/commonjs/client/adapter.js +189 -0
- package/dist/commonjs/client/adapter.js.map +1 -0
- package/dist/commonjs/client/cors.d.ts +72 -0
- package/dist/commonjs/client/cors.d.ts.map +1 -0
- package/dist/commonjs/client/cors.js +281 -0
- package/dist/commonjs/client/cors.js.map +1 -0
- package/dist/commonjs/client/index.d.ts +302 -0
- package/dist/commonjs/client/index.d.ts.map +1 -0
- package/dist/commonjs/client/index.js +232 -0
- package/dist/commonjs/client/index.js.map +1 -0
- package/dist/commonjs/client/plugins/index.d.ts +3 -0
- package/dist/commonjs/client/plugins/index.d.ts.map +1 -0
- package/dist/commonjs/client/plugins/index.js +3 -0
- package/dist/commonjs/client/plugins/index.js.map +1 -0
- package/dist/commonjs/component/_generated/api.d.ts +12 -0
- package/dist/commonjs/component/_generated/api.d.ts.map +1 -0
- package/dist/commonjs/component/_generated/api.js +22 -0
- package/dist/commonjs/component/_generated/api.js.map +1 -0
- package/dist/commonjs/component/_generated/server.d.ts +64 -0
- package/dist/commonjs/component/_generated/server.d.ts.map +1 -0
- package/dist/commonjs/component/_generated/server.js +74 -0
- package/dist/commonjs/component/_generated/server.js.map +1 -0
- package/dist/commonjs/component/convex.config.d.ts +3 -0
- package/dist/commonjs/component/convex.config.d.ts.map +1 -0
- package/dist/commonjs/component/convex.config.js +4 -0
- package/dist/commonjs/component/convex.config.js.map +1 -0
- package/dist/commonjs/component/lib.d.ts +584 -0
- package/dist/commonjs/component/lib.d.ts.map +1 -0
- package/dist/commonjs/component/lib.js +323 -0
- package/dist/commonjs/component/lib.js.map +1 -0
- package/dist/commonjs/component/schema.d.ts +116 -0
- package/dist/commonjs/component/schema.d.ts.map +1 -0
- package/dist/commonjs/component/schema.js +68 -0
- package/dist/commonjs/component/schema.js.map +1 -0
- package/dist/commonjs/component/util.d.ts +394 -0
- package/dist/commonjs/component/util.d.ts.map +1 -0
- package/dist/commonjs/component/util.js +4 -0
- package/dist/commonjs/component/util.js.map +1 -0
- package/dist/commonjs/nextjs/index.d.ts +10 -0
- package/dist/commonjs/nextjs/index.d.ts.map +1 -0
- package/dist/commonjs/nextjs/index.js +23 -0
- package/dist/commonjs/nextjs/index.js.map +1 -0
- package/dist/commonjs/package.json +3 -0
- package/dist/commonjs/plugins/convex/client.d.ts +6 -0
- package/dist/commonjs/plugins/convex/client.d.ts.map +1 -0
- package/dist/commonjs/plugins/convex/client.js +7 -0
- package/dist/commonjs/plugins/convex/client.js.map +1 -0
- package/dist/commonjs/plugins/convex/index.d.ts +280 -0
- package/dist/commonjs/plugins/convex/index.d.ts.map +1 -0
- package/dist/commonjs/plugins/convex/index.js +253 -0
- package/dist/commonjs/plugins/convex/index.js.map +1 -0
- package/dist/commonjs/plugins/cross-domain/client.d.ts +123 -0
- package/dist/commonjs/plugins/cross-domain/client.d.ts.map +1 -0
- package/dist/commonjs/plugins/cross-domain/client.js +164 -0
- package/dist/commonjs/plugins/cross-domain/client.js.map +1 -0
- package/dist/commonjs/plugins/cross-domain/index.d.ts +81 -0
- package/dist/commonjs/plugins/cross-domain/index.d.ts.map +1 -0
- package/dist/commonjs/plugins/cross-domain/index.js +135 -0
- package/dist/commonjs/plugins/cross-domain/index.js.map +1 -0
- package/dist/commonjs/plugins/index.d.ts +3 -0
- package/dist/commonjs/plugins/index.d.ts.map +1 -0
- package/dist/commonjs/plugins/index.js +3 -0
- package/dist/commonjs/plugins/index.js.map +1 -0
- package/dist/commonjs/react/client.d.ts +31 -0
- package/dist/commonjs/react/client.d.ts.map +1 -0
- package/dist/commonjs/react/client.js +102 -0
- package/dist/commonjs/react/client.js.map +1 -0
- package/dist/commonjs/react/index.d.ts +9 -0
- package/dist/commonjs/react/index.d.ts.map +1 -0
- package/dist/commonjs/react/index.js +15 -0
- package/dist/commonjs/react/index.js.map +1 -0
- package/dist/commonjs/react-start/index.d.ts +10 -0
- package/dist/commonjs/react-start/index.d.ts.map +1 -0
- package/dist/commonjs/react-start/index.js +32 -0
- package/dist/commonjs/react-start/index.js.map +1 -0
- package/dist/esm/client/adapter.d.ts +4 -0
- package/dist/esm/client/adapter.d.ts.map +1 -0
- package/dist/esm/client/adapter.js +189 -0
- package/dist/esm/client/adapter.js.map +1 -0
- package/dist/esm/client/cors.d.ts +72 -0
- package/dist/esm/client/cors.d.ts.map +1 -0
- package/dist/esm/client/cors.js +281 -0
- package/dist/esm/client/cors.js.map +1 -0
- package/dist/esm/client/index.d.ts +302 -0
- package/dist/esm/client/index.d.ts.map +1 -0
- package/dist/esm/client/index.js +232 -0
- package/dist/esm/client/index.js.map +1 -0
- package/dist/esm/client/plugins/index.d.ts +3 -0
- package/dist/esm/client/plugins/index.d.ts.map +1 -0
- package/dist/esm/client/plugins/index.js +3 -0
- package/dist/esm/client/plugins/index.js.map +1 -0
- package/dist/esm/component/_generated/api.d.ts +12 -0
- package/dist/esm/component/_generated/api.d.ts.map +1 -0
- package/dist/esm/component/_generated/api.js +22 -0
- package/dist/esm/component/_generated/api.js.map +1 -0
- package/dist/esm/component/_generated/server.d.ts +64 -0
- package/dist/esm/component/_generated/server.d.ts.map +1 -0
- package/dist/esm/component/_generated/server.js +74 -0
- package/dist/esm/component/_generated/server.js.map +1 -0
- package/dist/esm/component/convex.config.d.ts +3 -0
- package/dist/esm/component/convex.config.d.ts.map +1 -0
- package/dist/esm/component/convex.config.js +4 -0
- package/dist/esm/component/convex.config.js.map +1 -0
- package/dist/esm/component/lib.d.ts +584 -0
- package/dist/esm/component/lib.d.ts.map +1 -0
- package/dist/esm/component/lib.js +323 -0
- package/dist/esm/component/lib.js.map +1 -0
- package/dist/esm/component/schema.d.ts +116 -0
- package/dist/esm/component/schema.d.ts.map +1 -0
- package/dist/esm/component/schema.js +68 -0
- package/dist/esm/component/schema.js.map +1 -0
- package/dist/esm/component/util.d.ts +394 -0
- package/dist/esm/component/util.d.ts.map +1 -0
- package/dist/esm/component/util.js +4 -0
- package/dist/esm/component/util.js.map +1 -0
- package/dist/esm/nextjs/index.d.ts +10 -0
- package/dist/esm/nextjs/index.d.ts.map +1 -0
- package/dist/esm/nextjs/index.js +23 -0
- package/dist/esm/nextjs/index.js.map +1 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/plugins/convex/client.d.ts +6 -0
- package/dist/esm/plugins/convex/client.d.ts.map +1 -0
- package/dist/esm/plugins/convex/client.js +7 -0
- package/dist/esm/plugins/convex/client.js.map +1 -0
- package/dist/esm/plugins/convex/index.d.ts +280 -0
- package/dist/esm/plugins/convex/index.d.ts.map +1 -0
- package/dist/esm/plugins/convex/index.js +253 -0
- package/dist/esm/plugins/convex/index.js.map +1 -0
- package/dist/esm/plugins/cross-domain/client.d.ts +123 -0
- package/dist/esm/plugins/cross-domain/client.d.ts.map +1 -0
- package/dist/esm/plugins/cross-domain/client.js +164 -0
- package/dist/esm/plugins/cross-domain/client.js.map +1 -0
- package/dist/esm/plugins/cross-domain/index.d.ts +81 -0
- package/dist/esm/plugins/cross-domain/index.d.ts.map +1 -0
- package/dist/esm/plugins/cross-domain/index.js +135 -0
- package/dist/esm/plugins/cross-domain/index.js.map +1 -0
- package/dist/esm/plugins/index.d.ts +3 -0
- package/dist/esm/plugins/index.d.ts.map +1 -0
- package/dist/esm/plugins/index.js +3 -0
- package/dist/esm/plugins/index.js.map +1 -0
- package/dist/esm/react/client.d.ts +31 -0
- package/dist/esm/react/client.d.ts.map +1 -0
- package/dist/esm/react/client.js +102 -0
- package/dist/esm/react/client.js.map +1 -0
- package/dist/esm/react/index.d.ts +9 -0
- package/dist/esm/react/index.d.ts.map +1 -0
- package/dist/esm/react/index.js +15 -0
- package/dist/esm/react/index.js.map +1 -0
- package/dist/esm/react-start/index.d.ts +10 -0
- package/dist/esm/react-start/index.d.ts.map +1 -0
- package/dist/esm/react-start/index.js +32 -0
- package/dist/esm/react-start/index.js.map +1 -0
- package/package.json +161 -0
- package/plugins/package.json +5 -0
- package/react/package.json +5 -0
- package/src/client/adapter.ts +236 -0
- package/src/client/cors.ts +403 -0
- package/src/client/index.ts +381 -0
- package/src/client/plugins/index.ts +2 -0
- package/src/component/_generated/api.d.ts +313 -0
- package/src/component/_generated/api.js +23 -0
- package/src/component/_generated/dataModel.d.ts +60 -0
- package/src/component/_generated/server.d.ts +149 -0
- package/src/component/_generated/server.js +90 -0
- package/src/component/convex.config.ts +5 -0
- package/src/component/lib.ts +391 -0
- package/src/component/schema.ts +74 -0
- package/src/component/util.ts +4 -0
- package/src/nextjs/index.ts +30 -0
- package/src/plugins/convex/client.ts +9 -0
- package/src/plugins/convex/index.ts +296 -0
- package/src/plugins/cross-domain/client.ts +209 -0
- package/src/plugins/cross-domain/index.ts +156 -0
- package/src/plugins/index.ts +2 -0
- package/src/react/client.tsx +184 -0
- package/src/react/index.tsx +38 -0
- package/src/react-start/index.ts +51 -0
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type Auth as ConvexAuth,
|
|
3
|
+
type DefaultFunctionArgs,
|
|
4
|
+
type Expand,
|
|
5
|
+
type FunctionReference,
|
|
6
|
+
GenericActionCtx,
|
|
7
|
+
type GenericDataModel,
|
|
8
|
+
GenericMutationCtx,
|
|
9
|
+
type GenericQueryCtx,
|
|
10
|
+
type HttpRouter,
|
|
11
|
+
httpActionGeneric,
|
|
12
|
+
internalMutationGeneric,
|
|
13
|
+
queryGeneric,
|
|
14
|
+
} from "convex/server";
|
|
15
|
+
import { type GenericId, Infer, v } from "convex/values";
|
|
16
|
+
import type { api } from "../component/_generated/api";
|
|
17
|
+
import schema from "../component/schema";
|
|
18
|
+
import { convexAdapter } from "./adapter";
|
|
19
|
+
import corsRouter from "./cors";
|
|
20
|
+
import { getByArgsValidator, updateArgsInputValidator } from "../component/lib";
|
|
21
|
+
import { betterAuth } from "better-auth";
|
|
22
|
+
import { omit } from "convex-helpers";
|
|
23
|
+
import { createCookieGetter } from "better-auth/cookies";
|
|
24
|
+
import { fetchQuery } from "convex/nextjs";
|
|
25
|
+
export { convexAdapter };
|
|
26
|
+
|
|
27
|
+
const createUserFields = omit(schema.tables.user.validator.fields, ["userId"]);
|
|
28
|
+
const createUserValidator = v.object(createUserFields);
|
|
29
|
+
const createUserArgsValidator = v.object({
|
|
30
|
+
input: v.object({
|
|
31
|
+
...createUserFields,
|
|
32
|
+
table: v.literal("user"),
|
|
33
|
+
}),
|
|
34
|
+
});
|
|
35
|
+
const updateUserArgsValidator = v.object({
|
|
36
|
+
input: updateArgsInputValidator("user"),
|
|
37
|
+
});
|
|
38
|
+
const deleteUserArgsValidator = v.object(getByArgsValidator);
|
|
39
|
+
|
|
40
|
+
const createSessionArgsValidator = v.object({
|
|
41
|
+
input: v.object({
|
|
42
|
+
table: v.literal("session"),
|
|
43
|
+
...schema.tables.session.validator.fields,
|
|
44
|
+
}),
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
export type EventFunction<T extends DefaultFunctionArgs> = FunctionReference<
|
|
48
|
+
"mutation",
|
|
49
|
+
"internal" | "public",
|
|
50
|
+
T
|
|
51
|
+
>;
|
|
52
|
+
|
|
53
|
+
export type AuthFunctions = {
|
|
54
|
+
createUser: FunctionReference<
|
|
55
|
+
"mutation",
|
|
56
|
+
"internal",
|
|
57
|
+
Infer<typeof createUserArgsValidator>
|
|
58
|
+
>;
|
|
59
|
+
deleteUser: FunctionReference<
|
|
60
|
+
"mutation",
|
|
61
|
+
"internal",
|
|
62
|
+
Infer<typeof deleteUserArgsValidator>
|
|
63
|
+
>;
|
|
64
|
+
updateUser: FunctionReference<
|
|
65
|
+
"mutation",
|
|
66
|
+
"internal",
|
|
67
|
+
Infer<typeof updateUserArgsValidator>
|
|
68
|
+
>;
|
|
69
|
+
createSession: FunctionReference<
|
|
70
|
+
"mutation",
|
|
71
|
+
"internal",
|
|
72
|
+
Infer<typeof createSessionArgsValidator>
|
|
73
|
+
>;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
export type PublicAuthFunctions = {
|
|
77
|
+
isAuthenticated: FunctionReference<"query", "public">;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
export class BetterAuth<UserId extends string = string> {
|
|
81
|
+
constructor(
|
|
82
|
+
public component: UseApi<typeof api>,
|
|
83
|
+
public config: {
|
|
84
|
+
authFunctions: AuthFunctions;
|
|
85
|
+
publicAuthFunctions?: PublicAuthFunctions;
|
|
86
|
+
verbose?: boolean;
|
|
87
|
+
}
|
|
88
|
+
) {}
|
|
89
|
+
|
|
90
|
+
async isAuthenticated(token?: string | null) {
|
|
91
|
+
if (!this.config.publicAuthFunctions?.isAuthenticated) {
|
|
92
|
+
throw new Error(
|
|
93
|
+
"isAuthenticated function not found. It must be a named export in convex/auth.ts"
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
return fetchQuery(
|
|
97
|
+
this.config.publicAuthFunctions.isAuthenticated,
|
|
98
|
+
{},
|
|
99
|
+
{ token: token ?? undefined }
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
async getHeaders(ctx: RunQueryCtx & { auth: ConvexAuth }) {
|
|
104
|
+
const identity = await ctx.auth.getUserIdentity();
|
|
105
|
+
if (!identity) {
|
|
106
|
+
return new Headers();
|
|
107
|
+
}
|
|
108
|
+
const session = await ctx.runQuery(this.component.lib.getCurrentSession);
|
|
109
|
+
return new Headers({
|
|
110
|
+
authorization: `Bearer ${session?.token}`,
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// TODO: use the proper id type for auth functions
|
|
115
|
+
async getAuthUserId(ctx: RunQueryCtx & { auth: ConvexAuth }) {
|
|
116
|
+
const identity = await ctx.auth.getUserIdentity();
|
|
117
|
+
if (!identity) {
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
return identity.subject as UserId;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Convenience function for getting the Better Auth user
|
|
124
|
+
async getAuthUser(ctx: RunQueryCtx & { auth: ConvexAuth }) {
|
|
125
|
+
const identity = await ctx.auth.getUserIdentity();
|
|
126
|
+
if (!identity) {
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
const doc = await ctx.runQuery(this.component.lib.getBy, {
|
|
130
|
+
table: "user",
|
|
131
|
+
field: "userId",
|
|
132
|
+
value: identity.subject,
|
|
133
|
+
});
|
|
134
|
+
if (!doc) {
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
// Type narrowing
|
|
138
|
+
if (!("emailVerified" in doc)) {
|
|
139
|
+
throw new Error("invalid user");
|
|
140
|
+
}
|
|
141
|
+
const { id: _id, ...user } = doc;
|
|
142
|
+
return user;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
async getIdTokenCookieName(
|
|
146
|
+
createAuth: (ctx: GenericActionCtx<any>) => ReturnType<typeof betterAuth>
|
|
147
|
+
) {
|
|
148
|
+
const auth = createAuth({} as any);
|
|
149
|
+
const createCookie = createCookieGetter(auth.options);
|
|
150
|
+
const cookie = createCookie("convex_jwt");
|
|
151
|
+
return cookie.name;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
createAuthFunctions<DataModel extends GenericDataModel>(opts: {
|
|
155
|
+
onCreateUser: (
|
|
156
|
+
ctx: GenericMutationCtx<DataModel>,
|
|
157
|
+
user: Infer<typeof createUserValidator>
|
|
158
|
+
) => Promise<UserId>;
|
|
159
|
+
onDeleteUser?: (
|
|
160
|
+
ctx: GenericMutationCtx<DataModel>,
|
|
161
|
+
id: UserId
|
|
162
|
+
) => void | Promise<void>;
|
|
163
|
+
onUpdateUser?: (
|
|
164
|
+
ctx: GenericMutationCtx<DataModel>,
|
|
165
|
+
user: Infer<typeof schema.tables.user.validator>
|
|
166
|
+
) => void | Promise<void>;
|
|
167
|
+
onCreateSession?: (
|
|
168
|
+
ctx: GenericMutationCtx<DataModel>,
|
|
169
|
+
session: Infer<typeof schema.tables.session.validator>
|
|
170
|
+
) => void | Promise<void>;
|
|
171
|
+
}) {
|
|
172
|
+
return {
|
|
173
|
+
isAuthenticated: queryGeneric({
|
|
174
|
+
args: v.object({}),
|
|
175
|
+
handler: async (ctx) => {
|
|
176
|
+
const identity = await ctx.auth.getUserIdentity();
|
|
177
|
+
return identity !== null;
|
|
178
|
+
},
|
|
179
|
+
}),
|
|
180
|
+
createUser: internalMutationGeneric({
|
|
181
|
+
args: createUserArgsValidator,
|
|
182
|
+
handler: async (ctx, args) => {
|
|
183
|
+
const userId = await opts.onCreateUser(ctx, args.input);
|
|
184
|
+
const input = { ...args.input, table: "user", userId };
|
|
185
|
+
return ctx.runMutation(this.component.lib.create, {
|
|
186
|
+
input,
|
|
187
|
+
});
|
|
188
|
+
},
|
|
189
|
+
}),
|
|
190
|
+
deleteUser: internalMutationGeneric({
|
|
191
|
+
args: deleteUserArgsValidator,
|
|
192
|
+
handler: async (ctx, args) => {
|
|
193
|
+
const doc = await ctx.runMutation(this.component.lib.deleteBy, args);
|
|
194
|
+
if (opts.onDeleteUser) {
|
|
195
|
+
await opts.onDeleteUser(ctx, doc.userId as UserId);
|
|
196
|
+
}
|
|
197
|
+
},
|
|
198
|
+
}),
|
|
199
|
+
updateUser: internalMutationGeneric({
|
|
200
|
+
args: updateUserArgsValidator,
|
|
201
|
+
handler: async (ctx, args) => {
|
|
202
|
+
const updatedUser = await ctx.runMutation(
|
|
203
|
+
this.component.lib.update,
|
|
204
|
+
args
|
|
205
|
+
);
|
|
206
|
+
// Type narrowing
|
|
207
|
+
if (!("emailVerified" in updatedUser)) {
|
|
208
|
+
throw new Error("invalid user");
|
|
209
|
+
}
|
|
210
|
+
if (opts.onUpdateUser) {
|
|
211
|
+
await opts.onUpdateUser(ctx, omit(updatedUser, ["id"]));
|
|
212
|
+
}
|
|
213
|
+
return updatedUser;
|
|
214
|
+
},
|
|
215
|
+
}),
|
|
216
|
+
createSession: internalMutationGeneric({
|
|
217
|
+
args: createSessionArgsValidator,
|
|
218
|
+
handler: async (ctx, args) => {
|
|
219
|
+
const session = await ctx.runMutation(
|
|
220
|
+
this.component.lib.create,
|
|
221
|
+
args
|
|
222
|
+
);
|
|
223
|
+
// Type narrowing
|
|
224
|
+
if (!("ipAddress" in session)) {
|
|
225
|
+
throw new Error("invalid session");
|
|
226
|
+
}
|
|
227
|
+
await opts.onCreateSession?.(ctx, session);
|
|
228
|
+
return session;
|
|
229
|
+
},
|
|
230
|
+
}),
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
registerRoutes(
|
|
235
|
+
http: HttpRouter,
|
|
236
|
+
createAuth: (ctx: GenericActionCtx<any>) => ReturnType<typeof betterAuth>,
|
|
237
|
+
opts?: {
|
|
238
|
+
path?: string;
|
|
239
|
+
allowedOrigins?: string[];
|
|
240
|
+
}
|
|
241
|
+
) {
|
|
242
|
+
const path = opts?.path ?? "/api/auth";
|
|
243
|
+
const options = createAuth({} as any).options;
|
|
244
|
+
const trustedOriginsOption: string[] = Array.isArray(options.trustedOrigins)
|
|
245
|
+
? options.trustedOrigins
|
|
246
|
+
: [];
|
|
247
|
+
|
|
248
|
+
const trustedOrigins = createAuth({} as any).options.plugins?.reduce(
|
|
249
|
+
(acc, plugin) => {
|
|
250
|
+
if (plugin.options?.trustedOrigins) {
|
|
251
|
+
acc.push(...plugin.options.trustedOrigins);
|
|
252
|
+
}
|
|
253
|
+
return acc;
|
|
254
|
+
},
|
|
255
|
+
[...trustedOriginsOption, options.baseURL].filter(Boolean) as string[]
|
|
256
|
+
);
|
|
257
|
+
// Reuse trustedOrigins as default for allowedOrigins
|
|
258
|
+
const allowedOrigins =
|
|
259
|
+
opts?.allowedOrigins ??
|
|
260
|
+
trustedOrigins?.map((origin) =>
|
|
261
|
+
// Strip trailing wildcards, unsupported for allowedOrigins
|
|
262
|
+
origin.endsWith("*") && origin.length > 1 ? origin.slice(0, -1) : origin
|
|
263
|
+
);
|
|
264
|
+
const requireEnv = (name: string) => {
|
|
265
|
+
const value = process.env[name];
|
|
266
|
+
if (value === undefined) {
|
|
267
|
+
throw new Error(`Missing environment variable \`${name}\``);
|
|
268
|
+
}
|
|
269
|
+
return value;
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
const authRequestHandler = httpActionGeneric(async (ctx, request) => {
|
|
273
|
+
const auth = createAuth(ctx);
|
|
274
|
+
const response = await auth.handler(request);
|
|
275
|
+
if (this.config?.verbose) {
|
|
276
|
+
console.log("response headers", response.headers);
|
|
277
|
+
}
|
|
278
|
+
return response;
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
const cors = corsRouter(http, {
|
|
282
|
+
allowedOrigins,
|
|
283
|
+
allowCredentials: true,
|
|
284
|
+
allowedHeaders: ["Authorization", "Content-Type", "Better-Auth-Cookie"],
|
|
285
|
+
verbose: this.config?.verbose,
|
|
286
|
+
exposedHeaders: ["Set-Better-Auth-Cookie"],
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
http.route({
|
|
290
|
+
path: "/.well-known/openid-configuration",
|
|
291
|
+
method: "GET",
|
|
292
|
+
handler: httpActionGeneric(async () => {
|
|
293
|
+
const url = `${requireEnv("CONVEX_SITE_URL")}/api/auth/convex/.well-known/openid-configuration`;
|
|
294
|
+
return Response.redirect(url);
|
|
295
|
+
}),
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
http.route({
|
|
299
|
+
path: `${path}/convex/.well-known/openid-configuration`,
|
|
300
|
+
method: "GET",
|
|
301
|
+
handler: authRequestHandler,
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
http.route({
|
|
305
|
+
path: `${path}/convex/jwks`,
|
|
306
|
+
method: "GET",
|
|
307
|
+
handler: authRequestHandler,
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
http.route({
|
|
311
|
+
pathPrefix: `${path}/callback/`,
|
|
312
|
+
method: "GET",
|
|
313
|
+
handler: authRequestHandler,
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
http.route({
|
|
317
|
+
path: `${path}/magic-link/verify`,
|
|
318
|
+
method: "GET",
|
|
319
|
+
handler: authRequestHandler,
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
http.route({
|
|
323
|
+
path: `${path}/verify-email`,
|
|
324
|
+
method: "GET",
|
|
325
|
+
handler: authRequestHandler,
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
http.route({
|
|
329
|
+
pathPrefix: `${path}/reset-password/`,
|
|
330
|
+
method: "GET",
|
|
331
|
+
handler: authRequestHandler,
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
cors.route({
|
|
335
|
+
pathPrefix: `${path}/`,
|
|
336
|
+
method: "GET",
|
|
337
|
+
handler: authRequestHandler,
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
cors.route({
|
|
341
|
+
pathPrefix: `${path}/`,
|
|
342
|
+
method: "POST",
|
|
343
|
+
handler: authRequestHandler,
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/* Type utils follow */
|
|
349
|
+
|
|
350
|
+
type RunQueryCtx = {
|
|
351
|
+
runQuery: GenericQueryCtx<GenericDataModel>["runQuery"];
|
|
352
|
+
};
|
|
353
|
+
|
|
354
|
+
export type OpaqueIds<T> =
|
|
355
|
+
T extends GenericId<infer _T>
|
|
356
|
+
? string
|
|
357
|
+
: T extends (infer U)[]
|
|
358
|
+
? OpaqueIds<U>[]
|
|
359
|
+
: T extends ArrayBuffer
|
|
360
|
+
? ArrayBuffer
|
|
361
|
+
: T extends object
|
|
362
|
+
? { [K in keyof T]: OpaqueIds<T[K]> }
|
|
363
|
+
: T;
|
|
364
|
+
|
|
365
|
+
export type UseApi<API> = Expand<{
|
|
366
|
+
[mod in keyof API]: API[mod] extends FunctionReference<
|
|
367
|
+
infer FType,
|
|
368
|
+
"public",
|
|
369
|
+
infer FArgs,
|
|
370
|
+
infer FReturnType,
|
|
371
|
+
infer FComponentPath
|
|
372
|
+
>
|
|
373
|
+
? FunctionReference<
|
|
374
|
+
FType,
|
|
375
|
+
"internal",
|
|
376
|
+
OpaqueIds<FArgs>,
|
|
377
|
+
OpaqueIds<FReturnType>,
|
|
378
|
+
FComponentPath
|
|
379
|
+
>
|
|
380
|
+
: UseApi<API[mod]>;
|
|
381
|
+
}>;
|
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
/**
|
|
3
|
+
* Generated `api` utility.
|
|
4
|
+
*
|
|
5
|
+
* THIS CODE IS AUTOMATICALLY GENERATED.
|
|
6
|
+
*
|
|
7
|
+
* To regenerate, run `npx convex dev`.
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type * as lib from "../lib.js";
|
|
12
|
+
import type * as util from "../util.js";
|
|
13
|
+
|
|
14
|
+
import type {
|
|
15
|
+
ApiFromModules,
|
|
16
|
+
FilterApi,
|
|
17
|
+
FunctionReference,
|
|
18
|
+
} from "convex/server";
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* A utility for referencing Convex functions in your app's API.
|
|
22
|
+
*
|
|
23
|
+
* Usage:
|
|
24
|
+
* ```js
|
|
25
|
+
* const myFunctionReference = api.myModule.myFunction;
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
declare const fullApi: ApiFromModules<{
|
|
29
|
+
lib: typeof lib;
|
|
30
|
+
util: typeof util;
|
|
31
|
+
}>;
|
|
32
|
+
export type Mounts = {
|
|
33
|
+
lib: {
|
|
34
|
+
create: FunctionReference<
|
|
35
|
+
"mutation",
|
|
36
|
+
"public",
|
|
37
|
+
{
|
|
38
|
+
input:
|
|
39
|
+
| {
|
|
40
|
+
createdAt: number;
|
|
41
|
+
email: string;
|
|
42
|
+
emailVerified: boolean;
|
|
43
|
+
image?: string;
|
|
44
|
+
name: string;
|
|
45
|
+
table: string;
|
|
46
|
+
twoFactorEnabled?: boolean;
|
|
47
|
+
updatedAt: number;
|
|
48
|
+
userId: string;
|
|
49
|
+
}
|
|
50
|
+
| {
|
|
51
|
+
createdAt: number;
|
|
52
|
+
expiresAt: number;
|
|
53
|
+
ipAddress?: string;
|
|
54
|
+
table: string;
|
|
55
|
+
token: string;
|
|
56
|
+
updatedAt: number;
|
|
57
|
+
userAgent?: string;
|
|
58
|
+
userId: string;
|
|
59
|
+
}
|
|
60
|
+
| {
|
|
61
|
+
accessToken?: string;
|
|
62
|
+
accessTokenExpiresAt?: number;
|
|
63
|
+
accountId: string;
|
|
64
|
+
createdAt: number;
|
|
65
|
+
idToken?: string;
|
|
66
|
+
password?: string;
|
|
67
|
+
providerId: string;
|
|
68
|
+
refreshToken?: string;
|
|
69
|
+
refreshTokenExpiresAt?: number;
|
|
70
|
+
scope?: string;
|
|
71
|
+
table: string;
|
|
72
|
+
updatedAt: number;
|
|
73
|
+
userId: string;
|
|
74
|
+
}
|
|
75
|
+
| {
|
|
76
|
+
backupCodes: string;
|
|
77
|
+
secret: string;
|
|
78
|
+
table: string;
|
|
79
|
+
userId: string;
|
|
80
|
+
}
|
|
81
|
+
| {
|
|
82
|
+
createdAt?: number;
|
|
83
|
+
expiresAt: number;
|
|
84
|
+
identifier: string;
|
|
85
|
+
table: string;
|
|
86
|
+
updatedAt?: number;
|
|
87
|
+
value: string;
|
|
88
|
+
}
|
|
89
|
+
| {
|
|
90
|
+
createdAt: number;
|
|
91
|
+
id?: string;
|
|
92
|
+
privateKey: string;
|
|
93
|
+
publicKey: string;
|
|
94
|
+
table: string;
|
|
95
|
+
};
|
|
96
|
+
},
|
|
97
|
+
any
|
|
98
|
+
>;
|
|
99
|
+
deleteAllForUser: FunctionReference<
|
|
100
|
+
"action",
|
|
101
|
+
"public",
|
|
102
|
+
{ table: string; userId: string },
|
|
103
|
+
any
|
|
104
|
+
>;
|
|
105
|
+
deleteAllForUserPage: FunctionReference<
|
|
106
|
+
"mutation",
|
|
107
|
+
"public",
|
|
108
|
+
{
|
|
109
|
+
paginationOpts?: {
|
|
110
|
+
cursor: string | null;
|
|
111
|
+
endCursor?: string | null;
|
|
112
|
+
id?: number;
|
|
113
|
+
maximumBytesRead?: number;
|
|
114
|
+
maximumRowsRead?: number;
|
|
115
|
+
numItems: number;
|
|
116
|
+
};
|
|
117
|
+
table: string;
|
|
118
|
+
userId: string;
|
|
119
|
+
},
|
|
120
|
+
any
|
|
121
|
+
>;
|
|
122
|
+
deleteBy: FunctionReference<
|
|
123
|
+
"mutation",
|
|
124
|
+
"public",
|
|
125
|
+
{
|
|
126
|
+
field: string;
|
|
127
|
+
table: string;
|
|
128
|
+
unique?: boolean;
|
|
129
|
+
value: string | number | boolean | Array<string> | Array<number> | null;
|
|
130
|
+
},
|
|
131
|
+
any
|
|
132
|
+
>;
|
|
133
|
+
deleteOldVerifications: FunctionReference<
|
|
134
|
+
"action",
|
|
135
|
+
"public",
|
|
136
|
+
{ currentTimestamp: number },
|
|
137
|
+
any
|
|
138
|
+
>;
|
|
139
|
+
deleteOldVerificationsPage: FunctionReference<
|
|
140
|
+
"mutation",
|
|
141
|
+
"public",
|
|
142
|
+
{
|
|
143
|
+
currentTimestamp: number;
|
|
144
|
+
paginationOpts?: {
|
|
145
|
+
cursor: string | null;
|
|
146
|
+
endCursor?: string | null;
|
|
147
|
+
id?: number;
|
|
148
|
+
maximumBytesRead?: number;
|
|
149
|
+
maximumRowsRead?: number;
|
|
150
|
+
numItems: number;
|
|
151
|
+
};
|
|
152
|
+
},
|
|
153
|
+
any
|
|
154
|
+
>;
|
|
155
|
+
getAccountByAccountIdAndProviderId: FunctionReference<
|
|
156
|
+
"query",
|
|
157
|
+
"public",
|
|
158
|
+
{ accountId: string; providerId: string },
|
|
159
|
+
any
|
|
160
|
+
>;
|
|
161
|
+
getAccountsByUserId: FunctionReference<
|
|
162
|
+
"query",
|
|
163
|
+
"public",
|
|
164
|
+
{ limit?: number; userId: string },
|
|
165
|
+
any
|
|
166
|
+
>;
|
|
167
|
+
getBy: FunctionReference<
|
|
168
|
+
"query",
|
|
169
|
+
"public",
|
|
170
|
+
{
|
|
171
|
+
field: string;
|
|
172
|
+
table: string;
|
|
173
|
+
unique?: boolean;
|
|
174
|
+
value: string | number | boolean | Array<string> | Array<number> | null;
|
|
175
|
+
},
|
|
176
|
+
any
|
|
177
|
+
>;
|
|
178
|
+
getByQuery: FunctionReference<
|
|
179
|
+
"query",
|
|
180
|
+
"public",
|
|
181
|
+
{
|
|
182
|
+
field: string;
|
|
183
|
+
table: string;
|
|
184
|
+
unique?: boolean;
|
|
185
|
+
value: string | number | boolean | Array<string> | Array<number> | null;
|
|
186
|
+
},
|
|
187
|
+
any
|
|
188
|
+
>;
|
|
189
|
+
getCurrentSession: FunctionReference<"query", "public", {}, any>;
|
|
190
|
+
getJwks: FunctionReference<"query", "public", { limit?: number }, any>;
|
|
191
|
+
listVerificationsByIdentifier: FunctionReference<
|
|
192
|
+
"query",
|
|
193
|
+
"public",
|
|
194
|
+
{
|
|
195
|
+
identifier: string;
|
|
196
|
+
limit?: number;
|
|
197
|
+
sortBy?: { direction: "asc" | "desc"; field: string };
|
|
198
|
+
},
|
|
199
|
+
any
|
|
200
|
+
>;
|
|
201
|
+
update: FunctionReference<
|
|
202
|
+
"mutation",
|
|
203
|
+
"public",
|
|
204
|
+
{
|
|
205
|
+
input:
|
|
206
|
+
| {
|
|
207
|
+
table: "account";
|
|
208
|
+
value: Record<string, any>;
|
|
209
|
+
where: {
|
|
210
|
+
field: string;
|
|
211
|
+
value:
|
|
212
|
+
| string
|
|
213
|
+
| number
|
|
214
|
+
| boolean
|
|
215
|
+
| Array<string>
|
|
216
|
+
| Array<number>
|
|
217
|
+
| null;
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
| {
|
|
221
|
+
table: "session";
|
|
222
|
+
value: Record<string, any>;
|
|
223
|
+
where: {
|
|
224
|
+
field: string;
|
|
225
|
+
value:
|
|
226
|
+
| string
|
|
227
|
+
| number
|
|
228
|
+
| boolean
|
|
229
|
+
| Array<string>
|
|
230
|
+
| Array<number>
|
|
231
|
+
| null;
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
| {
|
|
235
|
+
table: "verification";
|
|
236
|
+
value: Record<string, any>;
|
|
237
|
+
where: {
|
|
238
|
+
field: string;
|
|
239
|
+
value:
|
|
240
|
+
| string
|
|
241
|
+
| number
|
|
242
|
+
| boolean
|
|
243
|
+
| Array<string>
|
|
244
|
+
| Array<number>
|
|
245
|
+
| null;
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
| {
|
|
249
|
+
table: "user";
|
|
250
|
+
value: Record<string, any>;
|
|
251
|
+
where: {
|
|
252
|
+
field: string;
|
|
253
|
+
value:
|
|
254
|
+
| string
|
|
255
|
+
| number
|
|
256
|
+
| boolean
|
|
257
|
+
| Array<string>
|
|
258
|
+
| Array<number>
|
|
259
|
+
| null;
|
|
260
|
+
};
|
|
261
|
+
};
|
|
262
|
+
},
|
|
263
|
+
any
|
|
264
|
+
>;
|
|
265
|
+
updateTwoFactor: FunctionReference<
|
|
266
|
+
"mutation",
|
|
267
|
+
"public",
|
|
268
|
+
{
|
|
269
|
+
update: { backupCodes?: string; secret?: string; userId?: string };
|
|
270
|
+
userId: string;
|
|
271
|
+
},
|
|
272
|
+
any
|
|
273
|
+
>;
|
|
274
|
+
updateUserProviderAccounts: FunctionReference<
|
|
275
|
+
"mutation",
|
|
276
|
+
"public",
|
|
277
|
+
{
|
|
278
|
+
providerId: string;
|
|
279
|
+
update: {
|
|
280
|
+
accessToken?: string;
|
|
281
|
+
accessTokenExpiresAt?: number;
|
|
282
|
+
accountId?: string;
|
|
283
|
+
createdAt?: number;
|
|
284
|
+
idToken?: string;
|
|
285
|
+
password?: string;
|
|
286
|
+
providerId?: string;
|
|
287
|
+
refreshToken?: string;
|
|
288
|
+
refreshTokenExpiresAt?: number;
|
|
289
|
+
scope?: string;
|
|
290
|
+
updatedAt?: number;
|
|
291
|
+
userId?: string;
|
|
292
|
+
};
|
|
293
|
+
userId: string;
|
|
294
|
+
},
|
|
295
|
+
any
|
|
296
|
+
>;
|
|
297
|
+
};
|
|
298
|
+
};
|
|
299
|
+
// For now fullApiWithMounts is only fullApi which provides
|
|
300
|
+
// jump-to-definition in component client code.
|
|
301
|
+
// Use Mounts for the same type without the inference.
|
|
302
|
+
declare const fullApiWithMounts: typeof fullApi;
|
|
303
|
+
|
|
304
|
+
export declare const api: FilterApi<
|
|
305
|
+
typeof fullApiWithMounts,
|
|
306
|
+
FunctionReference<any, "public">
|
|
307
|
+
>;
|
|
308
|
+
export declare const internal: FilterApi<
|
|
309
|
+
typeof fullApiWithMounts,
|
|
310
|
+
FunctionReference<any, "internal">
|
|
311
|
+
>;
|
|
312
|
+
|
|
313
|
+
export declare const components: {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
/**
|
|
3
|
+
* Generated `api` utility.
|
|
4
|
+
*
|
|
5
|
+
* THIS CODE IS AUTOMATICALLY GENERATED.
|
|
6
|
+
*
|
|
7
|
+
* To regenerate, run `npx convex dev`.
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { anyApi, componentsGeneric } from "convex/server";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A utility for referencing Convex functions in your app's API.
|
|
15
|
+
*
|
|
16
|
+
* Usage:
|
|
17
|
+
* ```js
|
|
18
|
+
* const myFunctionReference = api.myModule.myFunction;
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export const api = anyApi;
|
|
22
|
+
export const internal = anyApi;
|
|
23
|
+
export const components = componentsGeneric();
|