better-auth-invite-plugin 0.1.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 +21 -0
- package/README.md +629 -0
- package/dist/body.d.ts +24 -0
- package/dist/body.js +137 -0
- package/dist/client.d.ts +5 -0
- package/dist/client.js +10 -0
- package/dist/hooks.d.ts +6 -0
- package/dist/hooks.js +122 -0
- package/dist/index.d.ts +258 -0
- package/dist/index.js +41 -0
- package/dist/routes.d.ts +168 -0
- package/dist/routes.js +324 -0
- package/dist/schema.d.ts +76 -0
- package/dist/schema.js +37 -0
- package/dist/schemas.d.ts +24 -0
- package/dist/schemas.js +127 -0
- package/dist/types.d.ts +184 -0
- package/dist/types.js +14 -0
- package/dist/utils.d.ts +82 -0
- package/dist/utils.js +425 -0
- package/package.json +48 -0
package/dist/routes.d.ts
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { type NewInviteOptions } from "./types";
|
|
2
|
+
import * as z from "zod";
|
|
3
|
+
export declare const createInvite: (options: NewInviteOptions) => import("better-auth", { with: { "resolution-mode": "import" } }).StrictEndpoint<"/invite/create", {
|
|
4
|
+
method: "POST";
|
|
5
|
+
body: z.ZodObject<{
|
|
6
|
+
role: z.ZodString;
|
|
7
|
+
email: z.ZodOptional<z.ZodEmail>;
|
|
8
|
+
tokenType: z.ZodOptional<z.ZodEnum<{
|
|
9
|
+
token: "token";
|
|
10
|
+
code: "code";
|
|
11
|
+
custom: "custom";
|
|
12
|
+
}>>;
|
|
13
|
+
redirectToSignUp: z.ZodOptional<z.ZodString>;
|
|
14
|
+
redirectToSignIn: z.ZodOptional<z.ZodString>;
|
|
15
|
+
maxUses: z.ZodOptional<z.ZodNumber>;
|
|
16
|
+
expiresIn: z.ZodOptional<z.ZodNumber>;
|
|
17
|
+
redirectToAfterUpgrade: z.ZodOptional<z.ZodString>;
|
|
18
|
+
shareInviterName: z.ZodOptional<z.ZodBoolean>;
|
|
19
|
+
senderResponse: z.ZodOptional<z.ZodEnum<{
|
|
20
|
+
token: "token";
|
|
21
|
+
url: "url";
|
|
22
|
+
}>>;
|
|
23
|
+
senderResponseRedirect: z.ZodOptional<z.ZodEnum<{
|
|
24
|
+
signUp: "signUp";
|
|
25
|
+
signIn: "signIn";
|
|
26
|
+
}>>;
|
|
27
|
+
}, z.core.$strip>;
|
|
28
|
+
use: ((inputContext: import("better-auth", { with: { "resolution-mode": "import" } }).MiddlewareInputContext<import("better-auth", { with: { "resolution-mode": "import" } }).MiddlewareOptions>) => Promise<{
|
|
29
|
+
session: {
|
|
30
|
+
session: Record<string, any> & {
|
|
31
|
+
id: string;
|
|
32
|
+
createdAt: Date;
|
|
33
|
+
updatedAt: Date;
|
|
34
|
+
userId: string;
|
|
35
|
+
expiresAt: Date;
|
|
36
|
+
token: string;
|
|
37
|
+
ipAddress?: string | null | undefined;
|
|
38
|
+
userAgent?: string | null | undefined;
|
|
39
|
+
};
|
|
40
|
+
user: Record<string, any> & {
|
|
41
|
+
id: string;
|
|
42
|
+
createdAt: Date;
|
|
43
|
+
updatedAt: Date;
|
|
44
|
+
email: string;
|
|
45
|
+
emailVerified: boolean;
|
|
46
|
+
name: string;
|
|
47
|
+
image?: string | null | undefined;
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
}>)[];
|
|
51
|
+
metadata: {
|
|
52
|
+
openapi: {
|
|
53
|
+
operationId: string;
|
|
54
|
+
description: string;
|
|
55
|
+
responses: {
|
|
56
|
+
"200": {
|
|
57
|
+
description: string;
|
|
58
|
+
content: {
|
|
59
|
+
"application/json": {
|
|
60
|
+
schema: {
|
|
61
|
+
type: "object";
|
|
62
|
+
properties: {
|
|
63
|
+
status: {
|
|
64
|
+
type: string;
|
|
65
|
+
};
|
|
66
|
+
message: {
|
|
67
|
+
type: string;
|
|
68
|
+
};
|
|
69
|
+
token: {
|
|
70
|
+
type: string;
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
required: string[];
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
}, {
|
|
82
|
+
status: boolean;
|
|
83
|
+
message: string;
|
|
84
|
+
}>;
|
|
85
|
+
/**
|
|
86
|
+
* Can be used calling it form better auth
|
|
87
|
+
* Ex: auth.api.activateInvite()
|
|
88
|
+
*/
|
|
89
|
+
export declare const activateInvite: (options: NewInviteOptions) => import("better-auth", { with: { "resolution-mode": "import" } }).StrictEndpoint<"/invite/activate", {
|
|
90
|
+
method: "POST";
|
|
91
|
+
body: z.ZodObject<{
|
|
92
|
+
callbackURL: z.ZodString;
|
|
93
|
+
token: z.ZodString;
|
|
94
|
+
}, z.core.$strip>;
|
|
95
|
+
metadata: {
|
|
96
|
+
openapi: {
|
|
97
|
+
operationId: string;
|
|
98
|
+
description: string;
|
|
99
|
+
parameters: {
|
|
100
|
+
name: string;
|
|
101
|
+
in: "path";
|
|
102
|
+
required: true;
|
|
103
|
+
description: string;
|
|
104
|
+
schema: {
|
|
105
|
+
type: "string";
|
|
106
|
+
};
|
|
107
|
+
}[];
|
|
108
|
+
responses: {
|
|
109
|
+
"200": {
|
|
110
|
+
description: string;
|
|
111
|
+
content: {
|
|
112
|
+
"application/json": {
|
|
113
|
+
schema: {
|
|
114
|
+
type: "object";
|
|
115
|
+
properties: {
|
|
116
|
+
token: {
|
|
117
|
+
type: string;
|
|
118
|
+
};
|
|
119
|
+
};
|
|
120
|
+
};
|
|
121
|
+
};
|
|
122
|
+
};
|
|
123
|
+
};
|
|
124
|
+
};
|
|
125
|
+
};
|
|
126
|
+
};
|
|
127
|
+
}, void>;
|
|
128
|
+
/**
|
|
129
|
+
* Only used for invite links
|
|
130
|
+
*/
|
|
131
|
+
export declare const activateInviteCallback: (options: NewInviteOptions) => import("better-auth", { with: { "resolution-mode": "import" } }).StrictEndpoint<"/invite/:token", {
|
|
132
|
+
method: "GET";
|
|
133
|
+
query: z.ZodObject<{
|
|
134
|
+
callbackURL: z.ZodString;
|
|
135
|
+
}, z.core.$strip>;
|
|
136
|
+
metadata: {
|
|
137
|
+
openapi: {
|
|
138
|
+
operationId: string;
|
|
139
|
+
description: string;
|
|
140
|
+
parameters: {
|
|
141
|
+
name: string;
|
|
142
|
+
in: "path";
|
|
143
|
+
required: true;
|
|
144
|
+
description: string;
|
|
145
|
+
schema: {
|
|
146
|
+
type: "string";
|
|
147
|
+
};
|
|
148
|
+
}[];
|
|
149
|
+
responses: {
|
|
150
|
+
"200": {
|
|
151
|
+
description: string;
|
|
152
|
+
content: {
|
|
153
|
+
"application/json": {
|
|
154
|
+
schema: {
|
|
155
|
+
type: "object";
|
|
156
|
+
properties: {
|
|
157
|
+
token: {
|
|
158
|
+
type: string;
|
|
159
|
+
};
|
|
160
|
+
};
|
|
161
|
+
};
|
|
162
|
+
};
|
|
163
|
+
};
|
|
164
|
+
};
|
|
165
|
+
};
|
|
166
|
+
};
|
|
167
|
+
};
|
|
168
|
+
}, void>;
|
package/dist/routes.js
ADDED
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.activateInviteCallback = exports.activateInvite = exports.createInvite = void 0;
|
|
37
|
+
const api_1 = require("better-auth/api");
|
|
38
|
+
const types_1 = require("./types");
|
|
39
|
+
const utils_1 = require("./utils");
|
|
40
|
+
const body_1 = require("./body");
|
|
41
|
+
const z = __importStar(require("zod"));
|
|
42
|
+
const createInvite = (options) => {
|
|
43
|
+
return (0, api_1.createAuthEndpoint)("/invite/create", {
|
|
44
|
+
method: "POST",
|
|
45
|
+
body: body_1.createInviteBodySchema,
|
|
46
|
+
use: [api_1.sessionMiddleware],
|
|
47
|
+
metadata: {
|
|
48
|
+
openapi: {
|
|
49
|
+
operationId: "createInvitation",
|
|
50
|
+
description: "Create an invitation",
|
|
51
|
+
responses: {
|
|
52
|
+
"200": {
|
|
53
|
+
description: "Success",
|
|
54
|
+
content: {
|
|
55
|
+
"application/json": {
|
|
56
|
+
schema: {
|
|
57
|
+
type: "object",
|
|
58
|
+
properties: {
|
|
59
|
+
status: {
|
|
60
|
+
type: "boolean",
|
|
61
|
+
},
|
|
62
|
+
message: {
|
|
63
|
+
type: "string",
|
|
64
|
+
},
|
|
65
|
+
token: {
|
|
66
|
+
type: "string",
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
required: ["status", "message"],
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
}, async (ctx) => {
|
|
78
|
+
const inviterUser = ctx.context.session.user;
|
|
79
|
+
const { email, role } = ctx.body;
|
|
80
|
+
const { tokenType, redirectToAfterUpgrade, redirectToSignUp, redirectToSignIn, maxUses, expiresIn, shareInviterName, senderResponse, senderResponseRedirect, } = (0, utils_1.resolveInvitePayload)(ctx.body, options);
|
|
81
|
+
if (!(0, utils_1.canUserCreateInvite)(options, inviterUser, { email, role })) {
|
|
82
|
+
throw ctx.error("BAD_REQUEST", {
|
|
83
|
+
message: types_1.ERROR_CODES.INSUFFICIENT_PERMISSIONS,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
let generateToken = (0, utils_1.resolveTokenGenerator)(tokenType, options);
|
|
87
|
+
const invitedUser = email &&
|
|
88
|
+
(await ctx.context.internalAdapter.findUserByEmail(email, {
|
|
89
|
+
includeAccounts: true,
|
|
90
|
+
}));
|
|
91
|
+
// If the user already exists they should sign in, else they should sign up
|
|
92
|
+
const callbackURL = invitedUser ? redirectToSignIn : redirectToSignUp;
|
|
93
|
+
const token = generateToken();
|
|
94
|
+
const now = options.getDate();
|
|
95
|
+
const expiresAt = (0, utils_1.getDate)(expiresIn, "sec");
|
|
96
|
+
await ctx.context.adapter.create({
|
|
97
|
+
model: "invite",
|
|
98
|
+
data: {
|
|
99
|
+
token,
|
|
100
|
+
createdByUserId: inviterUser.id,
|
|
101
|
+
createdAt: now,
|
|
102
|
+
expiresAt,
|
|
103
|
+
maxUses,
|
|
104
|
+
redirectToAfterUpgrade,
|
|
105
|
+
shareInviterName,
|
|
106
|
+
email,
|
|
107
|
+
role,
|
|
108
|
+
},
|
|
109
|
+
});
|
|
110
|
+
const url = `${ctx.context.baseURL}/invite/${token}`;
|
|
111
|
+
const redirectURLEmail = `${url}?callbackURL=${callbackURL}?`;
|
|
112
|
+
// If an email is provided, send the invitation or role upgrade using the configured function
|
|
113
|
+
if (email) {
|
|
114
|
+
const sendFn = invitedUser
|
|
115
|
+
? options.sendUserRoleUpgrade ?? options.sendUserInvitation
|
|
116
|
+
: options.sendUserInvitation;
|
|
117
|
+
if (!sendFn) {
|
|
118
|
+
throw ctx.error("INTERNAL_SERVER_ERROR", {
|
|
119
|
+
message: "No invitation sending function configured",
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
await sendFn({
|
|
123
|
+
email,
|
|
124
|
+
role,
|
|
125
|
+
url: redirectURLEmail,
|
|
126
|
+
token,
|
|
127
|
+
}, ctx.request).catch((e) => {
|
|
128
|
+
ctx.context.logger.error("Error sending the invitation email", e);
|
|
129
|
+
});
|
|
130
|
+
return ctx.json({
|
|
131
|
+
status: true,
|
|
132
|
+
message: "The invitation was sent",
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
const redirectTo = senderResponseRedirect === "signUp"
|
|
136
|
+
? redirectToSignUp
|
|
137
|
+
: redirectToSignIn;
|
|
138
|
+
const redirectURL = `${url}?callbackURL=${redirectTo}`;
|
|
139
|
+
const returnToken = senderResponse === "token" ? token : redirectURL;
|
|
140
|
+
return ctx.json({
|
|
141
|
+
status: true,
|
|
142
|
+
message: "The invitation token was generated",
|
|
143
|
+
token: returnToken,
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
};
|
|
147
|
+
exports.createInvite = createInvite;
|
|
148
|
+
/**
|
|
149
|
+
* Can be used calling it form better auth
|
|
150
|
+
* Ex: auth.api.activateInvite()
|
|
151
|
+
*/
|
|
152
|
+
const activateInvite = (options) => {
|
|
153
|
+
return (0, api_1.createAuthEndpoint)("/invite/activate", {
|
|
154
|
+
method: "POST",
|
|
155
|
+
body: z.object({
|
|
156
|
+
/**
|
|
157
|
+
* Where to redirect the user after sing in/up
|
|
158
|
+
*/
|
|
159
|
+
callbackURL: z
|
|
160
|
+
.string()
|
|
161
|
+
.describe("Where to redirect the user after sing in/up"),
|
|
162
|
+
/**
|
|
163
|
+
* The invite token.
|
|
164
|
+
*/
|
|
165
|
+
token: z.string().describe("The invite token"),
|
|
166
|
+
}),
|
|
167
|
+
metadata: {
|
|
168
|
+
openapi: {
|
|
169
|
+
operationId: "activateInvite",
|
|
170
|
+
description: "Redirects the user to the callback URL with the token in a cookie",
|
|
171
|
+
parameters: [
|
|
172
|
+
{
|
|
173
|
+
name: "token",
|
|
174
|
+
in: "path",
|
|
175
|
+
required: true,
|
|
176
|
+
description: "The invitation token",
|
|
177
|
+
schema: {
|
|
178
|
+
type: "string",
|
|
179
|
+
},
|
|
180
|
+
},
|
|
181
|
+
],
|
|
182
|
+
responses: {
|
|
183
|
+
"200": {
|
|
184
|
+
description: "Success",
|
|
185
|
+
content: {
|
|
186
|
+
"application/json": {
|
|
187
|
+
schema: {
|
|
188
|
+
type: "object",
|
|
189
|
+
properties: {
|
|
190
|
+
token: {
|
|
191
|
+
type: "string",
|
|
192
|
+
},
|
|
193
|
+
},
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
},
|
|
197
|
+
},
|
|
198
|
+
},
|
|
199
|
+
},
|
|
200
|
+
},
|
|
201
|
+
}, async (ctx) => {
|
|
202
|
+
const { token, callbackURL } = ctx.body;
|
|
203
|
+
await activateInviteLogic({ ctx, options, token, callbackURL });
|
|
204
|
+
});
|
|
205
|
+
};
|
|
206
|
+
exports.activateInvite = activateInvite;
|
|
207
|
+
/**
|
|
208
|
+
* Only used for invite links
|
|
209
|
+
*/
|
|
210
|
+
const activateInviteCallback = (options) => {
|
|
211
|
+
return (0, api_1.createAuthEndpoint)("/invite/:token", {
|
|
212
|
+
method: "GET",
|
|
213
|
+
query: z.object({
|
|
214
|
+
/**
|
|
215
|
+
* Where to redirect the user after sing in/up
|
|
216
|
+
*/
|
|
217
|
+
callbackURL: z
|
|
218
|
+
.string()
|
|
219
|
+
.describe("Where to redirect the user after sing in/up"),
|
|
220
|
+
}),
|
|
221
|
+
metadata: {
|
|
222
|
+
openapi: {
|
|
223
|
+
operationId: "activateInviteCallback",
|
|
224
|
+
description: "Redirects the user to the callback URL with the token in a cookie",
|
|
225
|
+
parameters: [
|
|
226
|
+
{
|
|
227
|
+
name: "token",
|
|
228
|
+
in: "path",
|
|
229
|
+
required: true,
|
|
230
|
+
description: "The invitation token",
|
|
231
|
+
schema: {
|
|
232
|
+
type: "string",
|
|
233
|
+
},
|
|
234
|
+
},
|
|
235
|
+
],
|
|
236
|
+
responses: {
|
|
237
|
+
"200": {
|
|
238
|
+
description: "Success",
|
|
239
|
+
content: {
|
|
240
|
+
"application/json": {
|
|
241
|
+
schema: {
|
|
242
|
+
type: "object",
|
|
243
|
+
properties: {
|
|
244
|
+
token: {
|
|
245
|
+
type: "string",
|
|
246
|
+
},
|
|
247
|
+
},
|
|
248
|
+
},
|
|
249
|
+
},
|
|
250
|
+
},
|
|
251
|
+
},
|
|
252
|
+
},
|
|
253
|
+
},
|
|
254
|
+
},
|
|
255
|
+
}, async (ctx) => {
|
|
256
|
+
const { token } = ctx.params;
|
|
257
|
+
const { callbackURL } = ctx.query;
|
|
258
|
+
await activateInviteLogic({ ctx, options, token, callbackURL });
|
|
259
|
+
});
|
|
260
|
+
};
|
|
261
|
+
exports.activateInviteCallback = activateInviteCallback;
|
|
262
|
+
const activateInviteLogic = async ({ ctx, options, token, callbackURL, }) => {
|
|
263
|
+
if (!token) {
|
|
264
|
+
throw ctx.redirect((0, utils_1.redirectError)(ctx.context, callbackURL, { error: "INVALID_TOKEN" }));
|
|
265
|
+
}
|
|
266
|
+
const invite = (await ctx.context.adapter.findOne({
|
|
267
|
+
model: "invite",
|
|
268
|
+
where: [{ field: "token", value: token }],
|
|
269
|
+
}));
|
|
270
|
+
if (invite === null) {
|
|
271
|
+
throw ctx.redirect((0, utils_1.redirectError)(ctx.context, callbackURL, { error: "INVALID_TOKEN" }));
|
|
272
|
+
}
|
|
273
|
+
const timesUsed = await ctx.context.adapter.count({
|
|
274
|
+
model: "invite_use",
|
|
275
|
+
where: [{ field: "inviteId", value: invite.id }],
|
|
276
|
+
});
|
|
277
|
+
if (!(timesUsed < invite.maxUses)) {
|
|
278
|
+
throw ctx.redirect((0, utils_1.redirectError)(ctx.context, callbackURL, { error: "INVALID_TOKEN" }));
|
|
279
|
+
}
|
|
280
|
+
if (options.getDate() > invite.expiresAt) {
|
|
281
|
+
throw ctx.redirect((0, utils_1.redirectError)(ctx.context, callbackURL, { error: "INVALID_TOKEN" }));
|
|
282
|
+
}
|
|
283
|
+
const sessionObject = ctx.context.session;
|
|
284
|
+
const session = sessionObject?.session;
|
|
285
|
+
const userId = sessionObject?.user.id;
|
|
286
|
+
const user = userId
|
|
287
|
+
? (await ctx.context.internalAdapter.findUserById(userId))
|
|
288
|
+
: undefined;
|
|
289
|
+
if (user) {
|
|
290
|
+
// Upgrade existing user's role instead of redirecting if user already exists
|
|
291
|
+
if (!session || !userId) {
|
|
292
|
+
throw ctx.redirect((0, utils_1.redirectError)(ctx.context, callbackURL, {
|
|
293
|
+
error: "INTERNAL_SERVER_ERROR",
|
|
294
|
+
}));
|
|
295
|
+
}
|
|
296
|
+
await (0, utils_1.consumeInvite)({
|
|
297
|
+
ctx,
|
|
298
|
+
invite,
|
|
299
|
+
user,
|
|
300
|
+
options,
|
|
301
|
+
userId,
|
|
302
|
+
timesUsed,
|
|
303
|
+
token,
|
|
304
|
+
session,
|
|
305
|
+
newAccount: false,
|
|
306
|
+
});
|
|
307
|
+
await (0, utils_1.redirectToAfterUpgrade)({
|
|
308
|
+
shareInviterName: invite.shareInviterName,
|
|
309
|
+
ctx,
|
|
310
|
+
invite,
|
|
311
|
+
signUp: true,
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
// If user doesn't already exist, we set a cookie and redirect them to the sign in/up page
|
|
315
|
+
// Get cookie name (customizable)
|
|
316
|
+
const cookie = (0, utils_1.getCookieName)({ ctx, options });
|
|
317
|
+
ctx.setCookie(cookie, token, {
|
|
318
|
+
httpOnly: true,
|
|
319
|
+
path: "/",
|
|
320
|
+
expires: invite.expiresAt,
|
|
321
|
+
});
|
|
322
|
+
// Redirects the user to sign in/up
|
|
323
|
+
throw ctx.redirect((0, utils_1.redirectCallback)(ctx.context, callbackURL));
|
|
324
|
+
};
|
package/dist/schema.d.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
export declare const schema: {
|
|
2
|
+
invite: {
|
|
3
|
+
fields: {
|
|
4
|
+
token: {
|
|
5
|
+
type: "string";
|
|
6
|
+
unique: true;
|
|
7
|
+
};
|
|
8
|
+
createdAt: {
|
|
9
|
+
type: "date";
|
|
10
|
+
};
|
|
11
|
+
expiresAt: {
|
|
12
|
+
type: "date";
|
|
13
|
+
required: true;
|
|
14
|
+
};
|
|
15
|
+
maxUses: {
|
|
16
|
+
type: "number";
|
|
17
|
+
required: true;
|
|
18
|
+
};
|
|
19
|
+
createdByUserId: {
|
|
20
|
+
type: "string";
|
|
21
|
+
references: {
|
|
22
|
+
model: string;
|
|
23
|
+
field: string;
|
|
24
|
+
onDelete: "set null";
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
callbackURL: {
|
|
28
|
+
type: "string";
|
|
29
|
+
required: true;
|
|
30
|
+
};
|
|
31
|
+
redirectToAfterUpgrade: {
|
|
32
|
+
type: "string";
|
|
33
|
+
required: true;
|
|
34
|
+
};
|
|
35
|
+
shareInviterName: {
|
|
36
|
+
type: "boolean";
|
|
37
|
+
required: true;
|
|
38
|
+
};
|
|
39
|
+
email: {
|
|
40
|
+
type: "string";
|
|
41
|
+
required: false;
|
|
42
|
+
};
|
|
43
|
+
role: {
|
|
44
|
+
type: "string";
|
|
45
|
+
required: true;
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
invite_use: {
|
|
50
|
+
fields: {
|
|
51
|
+
inviteId: {
|
|
52
|
+
type: "string";
|
|
53
|
+
required: true;
|
|
54
|
+
references: {
|
|
55
|
+
model: string;
|
|
56
|
+
field: string;
|
|
57
|
+
onDelete: "set null";
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
usedAt: {
|
|
61
|
+
type: "date";
|
|
62
|
+
required: true;
|
|
63
|
+
};
|
|
64
|
+
usedByUserId: {
|
|
65
|
+
type: "string";
|
|
66
|
+
required: false;
|
|
67
|
+
references: {
|
|
68
|
+
model: string;
|
|
69
|
+
field: string;
|
|
70
|
+
onDelete: "set null";
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
export type InviteSchema = typeof schema;
|
package/dist/schema.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.schema = void 0;
|
|
4
|
+
exports.schema = {
|
|
5
|
+
invite: {
|
|
6
|
+
fields: {
|
|
7
|
+
token: { type: "string", unique: true },
|
|
8
|
+
createdAt: { type: "date" },
|
|
9
|
+
expiresAt: { type: "date", required: true },
|
|
10
|
+
maxUses: { type: "number", required: true },
|
|
11
|
+
createdByUserId: {
|
|
12
|
+
type: "string",
|
|
13
|
+
references: { model: "user", field: "id", onDelete: "set null" },
|
|
14
|
+
},
|
|
15
|
+
callbackURL: { type: "string", required: true },
|
|
16
|
+
redirectToAfterUpgrade: { type: "string", required: true },
|
|
17
|
+
shareInviterName: { type: "boolean", required: true },
|
|
18
|
+
email: { type: "string", required: false },
|
|
19
|
+
role: { type: "string", required: true },
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
invite_use: {
|
|
23
|
+
fields: {
|
|
24
|
+
inviteId: {
|
|
25
|
+
type: "string",
|
|
26
|
+
required: true,
|
|
27
|
+
references: { model: "invite", field: "id", onDelete: "set null" },
|
|
28
|
+
},
|
|
29
|
+
usedAt: { type: "date", required: true },
|
|
30
|
+
usedByUserId: {
|
|
31
|
+
type: "string",
|
|
32
|
+
required: false,
|
|
33
|
+
references: { model: "user", field: "id", onDelete: "set null" },
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
export declare const createInviteBodySchema: z.ZodObject<{
|
|
3
|
+
role: z.ZodString;
|
|
4
|
+
email: z.ZodOptional<z.ZodEmail>;
|
|
5
|
+
tokenType: z.ZodOptional<z.ZodEnum<{
|
|
6
|
+
token: "token";
|
|
7
|
+
code: "code";
|
|
8
|
+
custom: "custom";
|
|
9
|
+
}>>;
|
|
10
|
+
redirectToSignUp: z.ZodOptional<z.ZodString>;
|
|
11
|
+
redirectToSignIn: z.ZodOptional<z.ZodString>;
|
|
12
|
+
maxUses: z.ZodOptional<z.ZodNumber>;
|
|
13
|
+
expiresIn: z.ZodOptional<z.ZodNumber>;
|
|
14
|
+
redirectToAfterUpgrade: z.ZodOptional<z.ZodString>;
|
|
15
|
+
shareInviterName: z.ZodOptional<z.ZodBoolean>;
|
|
16
|
+
senderResponse: z.ZodOptional<z.ZodEnum<{
|
|
17
|
+
token: "token";
|
|
18
|
+
url: "url";
|
|
19
|
+
}>>;
|
|
20
|
+
senderResponseRedirect: z.ZodOptional<z.ZodEnum<{
|
|
21
|
+
signUp: "signUp";
|
|
22
|
+
signIn: "signIn";
|
|
23
|
+
}>>;
|
|
24
|
+
}, z.core.$strip>;
|