@revojs/auth 0.2.7 → 0.2.9

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.
Files changed (42) hide show
  1. package/dist/index.d.ts +167 -16
  2. package/dist/index.js +232 -33
  3. package/dist/module/index.d.ts +1 -0
  4. package/dist/module/index.js +55 -52
  5. package/dist/organization/routes/auth/invitations/accept/post.d.ts +4 -0
  6. package/dist/organization/routes/auth/invitations/accept/post.js +46 -0
  7. package/dist/organization/routes/auth/organizations/[organizationId]/get.d.ts +4 -0
  8. package/dist/organization/routes/auth/organizations/[organizationId]/get.js +14 -0
  9. package/dist/organization/routes/auth/organizations/[organizationId]/invitations/[invitationId]/delete.d.ts +4 -0
  10. package/dist/organization/routes/auth/organizations/[organizationId]/invitations/[invitationId]/delete.js +34 -0
  11. package/dist/organization/routes/auth/organizations/[organizationId]/invitations/get.d.ts +4 -0
  12. package/dist/organization/routes/auth/organizations/[organizationId]/invitations/get.js +22 -0
  13. package/dist/organization/routes/auth/organizations/[organizationId]/invitations/post.d.ts +4 -0
  14. package/dist/organization/routes/auth/organizations/[organizationId]/invitations/post.js +56 -0
  15. package/dist/organization/routes/auth/organizations/[organizationId]/memberships/[membershipId]/delete.d.ts +4 -0
  16. package/dist/organization/routes/auth/organizations/[organizationId]/memberships/[membershipId]/delete.js +30 -0
  17. package/dist/organization/routes/auth/organizations/[organizationId]/memberships/[membershipId]/patch.d.ts +4 -0
  18. package/dist/organization/routes/auth/organizations/[organizationId]/memberships/[membershipId]/patch.js +35 -0
  19. package/dist/organization/routes/auth/organizations/[organizationId]/memberships/get.d.ts +4 -0
  20. package/dist/organization/routes/auth/organizations/[organizationId]/memberships/get.js +14 -0
  21. package/dist/organization/routes/auth/organizations/[organizationId]/ownership/post.d.ts +4 -0
  22. package/dist/organization/routes/auth/organizations/[organizationId]/ownership/post.js +38 -0
  23. package/dist/organization/routes/auth/organizations/[organizationId]/patch.d.ts +4 -0
  24. package/dist/organization/routes/auth/organizations/[organizationId]/patch.js +36 -0
  25. package/dist/organization/routes/auth/organizations/[organizationId]/traces/get.d.ts +4 -0
  26. package/dist/organization/routes/auth/organizations/[organizationId]/traces/get.js +26 -0
  27. package/dist/organization/routes/auth/organizations/get.d.ts +4 -0
  28. package/dist/organization/routes/auth/organizations/get.js +11 -0
  29. package/dist/organization/routes/auth/organizations/post.d.ts +4 -0
  30. package/dist/organization/routes/auth/organizations/post.js +40 -0
  31. package/dist/routes/auth/+middleware.d.ts +4 -0
  32. package/dist/routes/auth/+middleware.js +9 -0
  33. package/dist/routes/auth/sessions/delete.js +2 -3
  34. package/dist/routes/auth/sessions/get.js +3 -5
  35. package/dist/routes/auth/sessions/post.js +4 -6
  36. package/dist/storage/mysql.d.ts +272 -0
  37. package/dist/storage/mysql.js +184 -0
  38. package/dist/storage/postgresql.d.ts +272 -0
  39. package/dist/storage/postgresql.js +184 -0
  40. package/dist/storage/sqlite.d.ts +272 -0
  41. package/dist/storage/sqlite.js +184 -0
  42. package/package.json +15 -1
@@ -1,61 +1,64 @@
1
+ import { addRoutes } from "@revojs/server/module";
1
2
  import { defineModule, useModule } from "revojs/module";
2
3
  //#region src/module/index.ts
3
4
  var module_default = defineModule({
4
5
  name: "auth",
5
- config: () => ({ session: {
6
- name: "session",
7
- cookie: {
8
- path: "/",
9
- secure: true,
10
- httpOnly: true,
11
- sameSite: "Lax",
12
- maxAge: 604800
6
+ config: () => ({
7
+ organizations: false,
8
+ session: {
9
+ name: "session",
10
+ cookie: {
11
+ path: "/",
12
+ secure: true,
13
+ httpOnly: true,
14
+ sameSite: "Lax",
15
+ maxAge: 604800
16
+ }
13
17
  }
14
- } }),
15
- setup() {
18
+ }),
19
+ setup(config) {
16
20
  const { fromModule } = useModule(import.meta.url);
17
- return {
18
- variables: { server: {
19
- sessionSecret: { type: "string" },
20
- appleClientKey: {
21
- type: "string",
22
- optional: true
23
- },
24
- appleClientSecret: {
25
- type: "string",
26
- optional: true
27
- },
28
- githubClientKey: {
29
- type: "string",
30
- optional: true
31
- },
32
- githubClientSecret: {
33
- type: "string",
34
- optional: true
35
- },
36
- googleClientKey: {
37
- type: "string",
38
- optional: true
39
- },
40
- googleClientSecret: {
41
- type: "string",
42
- optional: true
43
- },
44
- microsoftClientKey: {
45
- type: "string",
46
- optional: true
47
- },
48
- microsoftClientSecret: {
49
- type: "string",
50
- optional: true
51
- },
52
- microsoftTenant: {
53
- type: "string",
54
- optional: true
55
- }
56
- } },
57
- sources: { routes: { from: [fromModule("../routes")] } }
58
- };
21
+ addRoutes(config, fromModule("../routes"));
22
+ if (config.auth.organizations) addRoutes(config, fromModule("../organization/routes"));
23
+ return { variables: { server: {
24
+ sessionSecret: { type: "string" },
25
+ appleClientKey: {
26
+ type: "string",
27
+ optional: true
28
+ },
29
+ appleClientSecret: {
30
+ type: "string",
31
+ optional: true
32
+ },
33
+ githubClientKey: {
34
+ type: "string",
35
+ optional: true
36
+ },
37
+ githubClientSecret: {
38
+ type: "string",
39
+ optional: true
40
+ },
41
+ googleClientKey: {
42
+ type: "string",
43
+ optional: true
44
+ },
45
+ googleClientSecret: {
46
+ type: "string",
47
+ optional: true
48
+ },
49
+ microsoftClientKey: {
50
+ type: "string",
51
+ optional: true
52
+ },
53
+ microsoftClientSecret: {
54
+ type: "string",
55
+ optional: true
56
+ },
57
+ microsoftTenant: {
58
+ type: "string",
59
+ optional: true
60
+ }
61
+ } } };
59
62
  }
60
63
  });
61
64
  //#endregion
@@ -0,0 +1,4 @@
1
+ declare const _default: {
2
+ fetch(scope: import("revojs").Scope): Promise<Response>;
3
+ };
4
+ export default _default;
@@ -0,0 +1,46 @@
1
+ import { requireSession, useServerAuth } from "../../../../../index.js";
2
+ import { defineRoute, requireSameOrigin, sendForbidden, sendOk, useBody } from "@revojs/server";
3
+ import { object, pipe, regex, string } from "valibot";
4
+ //#region src/organization/routes/auth/invitations/accept/post.ts
5
+ var tokenSchema = pipe(string(), regex(/^[\w-]{43}$/));
6
+ var post_default = defineRoute({ async fetch(scope) {
7
+ await requireSession(scope);
8
+ requireSameOrigin(scope);
9
+ const { token } = await useBody(scope, object({ token: tokenSchema }));
10
+ const tokenHash = new Uint8Array(await crypto.subtle.digest("SHA-256", new TextEncoder().encode(token))).toHex();
11
+ const membership = await (await useServerAuth(scope)).transaction(async (database) => {
12
+ const session = await requireSession(scope);
13
+ const account = await database.getAccountById(session.accountId);
14
+ const invitation = await database.getInvitationByHash(tokenHash);
15
+ if (!account || !invitation || invitation.status !== "pending" || !Number.isFinite(invitation.expiresAt.getTime()) || invitation.expiresAt.getTime() <= Date.now() || invitation.email !== account.email.trim().toLowerCase()) throw sendForbidden(scope);
16
+ const organization = await database.getOrganization(invitation.organizationId);
17
+ const inviter = await database.getMembership(invitation.organizationId, invitation.invitedBy);
18
+ if (!organization || inviter?.status !== "active" || organization.accountId !== invitation.invitedBy) throw sendForbidden(scope);
19
+ let membership = await database.getMembership(invitation.organizationId, session.accountId);
20
+ if (membership && membership.status !== "active") throw sendForbidden(scope);
21
+ if (!membership) {
22
+ membership = {
23
+ id: crypto.randomUUID(),
24
+ organizationId: invitation.organizationId,
25
+ accountId: session.accountId,
26
+ status: "active",
27
+ createdAt: /* @__PURE__ */ new Date()
28
+ };
29
+ await database.createMembership(membership);
30
+ }
31
+ await database.updateInvitation(invitation.organizationId, invitation.id, "accepted");
32
+ await database.createTrace({
33
+ id: crypto.randomUUID(),
34
+ organizationId: invitation.organizationId,
35
+ accountId: session.accountId,
36
+ action: "invitation.accept",
37
+ subjectId: invitation.id,
38
+ content: JSON.stringify({ membershipId: membership.id }),
39
+ createdAt: /* @__PURE__ */ new Date()
40
+ });
41
+ return membership;
42
+ });
43
+ return sendOk(scope, membership);
44
+ } });
45
+ //#endregion
46
+ export { post_default as default };
@@ -0,0 +1,4 @@
1
+ declare const _default: {
2
+ fetch(scope: import("revojs").Scope): Promise<Response>;
3
+ };
4
+ export default _default;
@@ -0,0 +1,14 @@
1
+ import { organizationParametersSchema, requireSession, useServerAuth } from "../../../../../index.js";
2
+ import { defineRoute, sendForbidden, sendOk, useParameters } from "@revojs/server";
3
+ //#region src/organization/routes/auth/organizations/[organizationId]/get.ts
4
+ var get_default = defineRoute({ async fetch(scope) {
5
+ const session = await requireSession(scope);
6
+ const { organizationId } = useParameters(scope, organizationParametersSchema);
7
+ const storage = await useServerAuth(scope);
8
+ const organization = await storage.getOrganization(organizationId);
9
+ const membership = await storage.getMembership(organizationId, session.accountId);
10
+ if (!organization || membership?.status !== "active" || !await storage.getAccountById(session.accountId)) return sendForbidden(scope);
11
+ return sendOk(scope, organization);
12
+ } });
13
+ //#endregion
14
+ export { get_default as default };
@@ -0,0 +1,4 @@
1
+ declare const _default: {
2
+ fetch(scope: import("revojs").Scope): Promise<Response>;
3
+ };
4
+ export default _default;
@@ -0,0 +1,34 @@
1
+ import { authIdSchema, requireSession, useServerAuth } from "../../../../../../../index.js";
2
+ import { defineRoute, requireSameOrigin, sendForbidden, sendOk, useParameters } from "@revojs/server";
3
+ import { object } from "valibot";
4
+ //#region src/organization/routes/auth/organizations/[organizationId]/invitations/[invitationId]/delete.ts
5
+ var delete_default = defineRoute({ async fetch(scope) {
6
+ await requireSession(scope);
7
+ requireSameOrigin(scope);
8
+ const { organizationId, invitationId } = useParameters(scope, object({
9
+ organizationId: authIdSchema,
10
+ invitationId: authIdSchema
11
+ }));
12
+ await (await useServerAuth(scope)).transaction(async (database) => {
13
+ const session = await requireSession(scope);
14
+ if (!await database.getAccountById(session.accountId)) throw sendForbidden(scope);
15
+ const organization = await database.getOrganization(organizationId);
16
+ const membership = await database.getMembership(organizationId, session.accountId);
17
+ if (!organization || membership?.status !== "active" || organization.accountId !== session.accountId) throw sendForbidden(scope);
18
+ const invitation = await database.getInvitation(organizationId, invitationId);
19
+ if (invitation?.status !== "pending") throw sendForbidden(scope);
20
+ await database.updateInvitation(organizationId, invitation.id, "revoked");
21
+ await database.createTrace({
22
+ id: crypto.randomUUID(),
23
+ organizationId,
24
+ accountId: session.accountId,
25
+ action: "invitation.revoke",
26
+ subjectId: invitation.id,
27
+ content: "{}",
28
+ createdAt: /* @__PURE__ */ new Date()
29
+ });
30
+ });
31
+ return sendOk(scope);
32
+ } });
33
+ //#endregion
34
+ export { delete_default as default };
@@ -0,0 +1,4 @@
1
+ declare const _default: {
2
+ fetch(scope: import("revojs").Scope): Promise<Response>;
3
+ };
4
+ export default _default;
@@ -0,0 +1,22 @@
1
+ import { organizationParametersSchema, requireSession, useServerAuth } from "../../../../../../index.js";
2
+ import { defineRoute, sendForbidden, sendOk, useParameters } from "@revojs/server";
3
+ //#region src/organization/routes/auth/organizations/[organizationId]/invitations/get.ts
4
+ var get_default = defineRoute({ async fetch(scope) {
5
+ const session = await requireSession(scope);
6
+ const { organizationId } = useParameters(scope, organizationParametersSchema);
7
+ const storage = await useServerAuth(scope);
8
+ const organization = await storage.getOrganization(organizationId);
9
+ const membership = await storage.getMembership(organizationId, session.accountId);
10
+ if (!organization || membership?.status !== "active" || organization.accountId !== session.accountId || !await storage.getAccountById(session.accountId)) return sendForbidden(scope);
11
+ return sendOk(scope, (await storage.listInvitations(organizationId)).map((invitation) => ({
12
+ id: invitation.id,
13
+ organizationId: invitation.organizationId,
14
+ email: invitation.email,
15
+ invitedBy: invitation.invitedBy,
16
+ status: invitation.status,
17
+ expiresAt: invitation.expiresAt,
18
+ createdAt: invitation.createdAt
19
+ })));
20
+ } });
21
+ //#endregion
22
+ export { get_default as default };
@@ -0,0 +1,4 @@
1
+ declare const _default: {
2
+ fetch(scope: import("revojs").Scope): Promise<Response>;
3
+ };
4
+ export default _default;
@@ -0,0 +1,56 @@
1
+ import { organizationParametersSchema, requireSession, useServerAuth } from "../../../../../../index.js";
2
+ import { defineRoute, requireSameOrigin, sendForbidden, sendOk, useBody, useParameters } from "@revojs/server";
3
+ import { email, maxLength, maxValue, minValue, number, object, optional, pipe, string, trim } from "valibot";
4
+ //#region src/organization/routes/auth/organizations/[organizationId]/invitations/post.ts
5
+ var expiresInSchema = pipe(number(), minValue(1), maxValue(2592e3));
6
+ var bodySchema = object({
7
+ email: pipe(string(), trim(), maxLength(254), email()),
8
+ expiresIn: optional(expiresInSchema, 604800)
9
+ });
10
+ var post_default = defineRoute({ async fetch(scope) {
11
+ await requireSession(scope);
12
+ requireSameOrigin(scope);
13
+ const { organizationId } = useParameters(scope, organizationParametersSchema);
14
+ const body = await useBody(scope, bodySchema);
15
+ const token = crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(32)).toBase64({
16
+ alphabet: "base64url",
17
+ omitPadding: true
18
+ });
19
+ const tokenHash = new Uint8Array(await crypto.subtle.digest("SHA-256", new TextEncoder().encode(token))).toHex();
20
+ const invitation = await (await useServerAuth(scope)).transaction(async (database) => {
21
+ const session = await requireSession(scope);
22
+ if (!await database.getAccountById(session.accountId)) throw sendForbidden(scope);
23
+ const organization = await database.getOrganization(organizationId);
24
+ const membership = await database.getMembership(organizationId, session.accountId);
25
+ if (!organization || membership?.status !== "active" || organization.accountId !== session.accountId) throw sendForbidden(scope);
26
+ const invitation = {
27
+ id: crypto.randomUUID(),
28
+ organizationId,
29
+ email: body.email.toLowerCase(),
30
+ invitedBy: session.accountId,
31
+ status: "pending",
32
+ expiresAt: new Date(Date.now() + body.expiresIn * 1e3),
33
+ createdAt: /* @__PURE__ */ new Date()
34
+ };
35
+ await database.createInvitation({
36
+ ...invitation,
37
+ tokenHash
38
+ });
39
+ await database.createTrace({
40
+ id: crypto.randomUUID(),
41
+ organizationId,
42
+ accountId: session.accountId,
43
+ action: "invitation.create",
44
+ subjectId: invitation.id,
45
+ content: "{}",
46
+ createdAt: /* @__PURE__ */ new Date()
47
+ });
48
+ return invitation;
49
+ });
50
+ return sendOk(scope, {
51
+ invitation,
52
+ token
53
+ });
54
+ } });
55
+ //#endregion
56
+ export { post_default as default };
@@ -0,0 +1,4 @@
1
+ declare const _default: {
2
+ fetch(scope: import("revojs").Scope): Promise<Response>;
3
+ };
4
+ export default _default;
@@ -0,0 +1,30 @@
1
+ import { membershipParametersSchema, requireSession, useServerAuth } from "../../../../../../../index.js";
2
+ import { defineRoute, requireSameOrigin, sendForbidden, sendOk, useParameters } from "@revojs/server";
3
+ //#region src/organization/routes/auth/organizations/[organizationId]/memberships/[membershipId]/delete.ts
4
+ var delete_default = defineRoute({ async fetch(scope) {
5
+ await requireSession(scope);
6
+ requireSameOrigin(scope);
7
+ const { organizationId, membershipId } = useParameters(scope, membershipParametersSchema);
8
+ await (await useServerAuth(scope)).transaction(async (database) => {
9
+ const session = await requireSession(scope);
10
+ if (!await database.getAccountById(session.accountId)) throw sendForbidden(scope);
11
+ const organization = await database.getOrganization(organizationId);
12
+ const membership = await database.getMembership(organizationId, session.accountId);
13
+ if (!organization || membership?.status !== "active" || organization.accountId !== session.accountId) throw sendForbidden(scope);
14
+ const member = await database.getMembershipById(organizationId, membershipId);
15
+ if (!member || member.accountId === organization.accountId) throw sendForbidden(scope);
16
+ await database.deleteMembership(organizationId, member.id);
17
+ await database.createTrace({
18
+ id: crypto.randomUUID(),
19
+ organizationId,
20
+ accountId: session.accountId,
21
+ action: "membership.remove",
22
+ subjectId: member.id,
23
+ content: "{}",
24
+ createdAt: /* @__PURE__ */ new Date()
25
+ });
26
+ });
27
+ return sendOk(scope);
28
+ } });
29
+ //#endregion
30
+ export { delete_default as default };
@@ -0,0 +1,4 @@
1
+ declare const _default: {
2
+ fetch(scope: import("revojs").Scope): Promise<Response>;
3
+ };
4
+ export default _default;
@@ -0,0 +1,35 @@
1
+ import { membershipParametersSchema, requireSession, useServerAuth } from "../../../../../../../index.js";
2
+ import { defineRoute, requireSameOrigin, sendForbidden, sendOk, useBody, useParameters } from "@revojs/server";
3
+ import { object, picklist } from "valibot";
4
+ //#region src/organization/routes/auth/organizations/[organizationId]/memberships/[membershipId]/patch.ts
5
+ var patch_default = defineRoute({ async fetch(scope) {
6
+ await requireSession(scope);
7
+ requireSameOrigin(scope);
8
+ const { organizationId, membershipId } = useParameters(scope, membershipParametersSchema);
9
+ const { status } = await useBody(scope, object({ status: picklist(["active", "suspended"]) }));
10
+ await (await useServerAuth(scope)).transaction(async (database) => {
11
+ const session = await requireSession(scope);
12
+ if (!await database.getAccountById(session.accountId)) throw sendForbidden(scope);
13
+ const organization = await database.getOrganization(organizationId);
14
+ const membership = await database.getMembership(organizationId, session.accountId);
15
+ if (!organization || membership?.status !== "active" || organization.accountId !== session.accountId) throw sendForbidden(scope);
16
+ const member = await database.getMembershipById(organizationId, membershipId);
17
+ if (!member || member.accountId === organization.accountId) throw sendForbidden(scope);
18
+ await database.updateMembership(organizationId, member.id, status);
19
+ await database.createTrace({
20
+ id: crypto.randomUUID(),
21
+ organizationId,
22
+ accountId: session.accountId,
23
+ action: `membership.${status}`,
24
+ subjectId: member.id,
25
+ content: JSON.stringify({
26
+ beforeStatus: member.status,
27
+ status
28
+ }),
29
+ createdAt: /* @__PURE__ */ new Date()
30
+ });
31
+ });
32
+ return sendOk(scope);
33
+ } });
34
+ //#endregion
35
+ export { patch_default as default };
@@ -0,0 +1,4 @@
1
+ declare const _default: {
2
+ fetch(scope: import("revojs").Scope): Promise<Response>;
3
+ };
4
+ export default _default;
@@ -0,0 +1,14 @@
1
+ import { organizationParametersSchema, requireSession, useServerAuth } from "../../../../../../index.js";
2
+ import { defineRoute, sendForbidden, sendOk, useParameters } from "@revojs/server";
3
+ //#region src/organization/routes/auth/organizations/[organizationId]/memberships/get.ts
4
+ var get_default = defineRoute({ async fetch(scope) {
5
+ const session = await requireSession(scope);
6
+ const { organizationId } = useParameters(scope, organizationParametersSchema);
7
+ const storage = await useServerAuth(scope);
8
+ const organization = await storage.getOrganization(organizationId);
9
+ const membership = await storage.getMembership(organizationId, session.accountId);
10
+ if (!organization || membership?.status !== "active" || !await storage.getAccountById(session.accountId)) return sendForbidden(scope);
11
+ return sendOk(scope, await storage.listMemberships(organizationId));
12
+ } });
13
+ //#endregion
14
+ export { get_default as default };
@@ -0,0 +1,4 @@
1
+ declare const _default: {
2
+ fetch(scope: import("revojs").Scope): Promise<Response>;
3
+ };
4
+ export default _default;
@@ -0,0 +1,38 @@
1
+ import { authIdSchema, organizationParametersSchema, requireSession, useServerAuth } from "../../../../../../index.js";
2
+ import { defineRoute, requireSameOrigin, sendForbidden, sendOk, useBody, useParameters } from "@revojs/server";
3
+ import { object } from "valibot";
4
+ //#region src/organization/routes/auth/organizations/[organizationId]/ownership/post.ts
5
+ var post_default = defineRoute({ async fetch(scope) {
6
+ await requireSession(scope);
7
+ requireSameOrigin(scope);
8
+ const { organizationId } = useParameters(scope, organizationParametersSchema);
9
+ const { membershipId } = await useBody(scope, object({ membershipId: authIdSchema }));
10
+ await (await useServerAuth(scope)).transaction(async (database) => {
11
+ const session = await requireSession(scope);
12
+ if (!await database.getAccountById(session.accountId)) throw sendForbidden(scope);
13
+ const organization = await database.getOrganization(organizationId);
14
+ const membership = await database.getMembership(organizationId, session.accountId);
15
+ if (!organization || membership?.status !== "active" || organization.accountId !== session.accountId) throw sendForbidden(scope);
16
+ const member = await database.getMembershipById(organizationId, membershipId);
17
+ if (member?.status !== "active") throw sendForbidden(scope);
18
+ await database.updateOrganization(organizationId, {
19
+ name: organization.name,
20
+ accountId: member.accountId
21
+ });
22
+ await database.createTrace({
23
+ id: crypto.randomUUID(),
24
+ organizationId,
25
+ accountId: session.accountId,
26
+ action: "organization.transferOwnership",
27
+ subjectId: member.id,
28
+ content: JSON.stringify({
29
+ beforeAccountId: organization.accountId,
30
+ accountId: member.accountId
31
+ }),
32
+ createdAt: /* @__PURE__ */ new Date()
33
+ });
34
+ });
35
+ return sendOk(scope);
36
+ } });
37
+ //#endregion
38
+ export { post_default as default };
@@ -0,0 +1,4 @@
1
+ declare const _default: {
2
+ fetch(scope: import("revojs").Scope): Promise<Response>;
3
+ };
4
+ export default _default;
@@ -0,0 +1,36 @@
1
+ import { organizationNameSchema, organizationParametersSchema, requireSession, useServerAuth } from "../../../../../index.js";
2
+ import { defineRoute, requireSameOrigin, sendForbidden, sendOk, useBody, useParameters } from "@revojs/server";
3
+ import { object } from "valibot";
4
+ //#region src/organization/routes/auth/organizations/[organizationId]/patch.ts
5
+ var patch_default = defineRoute({ async fetch(scope) {
6
+ await requireSession(scope);
7
+ requireSameOrigin(scope);
8
+ const { organizationId } = useParameters(scope, organizationParametersSchema);
9
+ const { name } = await useBody(scope, object({ name: organizationNameSchema }));
10
+ await (await useServerAuth(scope)).transaction(async (database) => {
11
+ const session = await requireSession(scope);
12
+ if (!await database.getAccountById(session.accountId)) throw sendForbidden(scope);
13
+ const organization = await database.getOrganization(organizationId);
14
+ const membership = await database.getMembership(organizationId, session.accountId);
15
+ if (!organization || membership?.status !== "active" || organization.accountId !== session.accountId) throw sendForbidden(scope);
16
+ await database.updateOrganization(organizationId, {
17
+ name,
18
+ accountId: organization.accountId
19
+ });
20
+ await database.createTrace({
21
+ id: crypto.randomUUID(),
22
+ organizationId,
23
+ accountId: session.accountId,
24
+ action: "organization.update",
25
+ subjectId: organizationId,
26
+ content: JSON.stringify({
27
+ beforeName: organization.name,
28
+ name
29
+ }),
30
+ createdAt: /* @__PURE__ */ new Date()
31
+ });
32
+ });
33
+ return sendOk(scope);
34
+ } });
35
+ //#endregion
36
+ export { patch_default as default };
@@ -0,0 +1,4 @@
1
+ declare const _default: {
2
+ fetch(scope: import("revojs").Scope): Promise<Response>;
3
+ };
4
+ export default _default;
@@ -0,0 +1,26 @@
1
+ import { authIdSchema, organizationParametersSchema, requireSession, useServerAuth } from "../../../../../../index.js";
2
+ import { defineRoute, sendForbidden, sendOk, useParameters, useQuery } from "@revojs/server";
3
+ import { check, digits, maxValue, minValue, object, optional, pipe, string, toDate, toNumber } from "valibot";
4
+ //#region src/organization/routes/auth/organizations/[organizationId]/traces/get.ts
5
+ var limitSchema = pipe(string(), digits(), toNumber(), minValue(1), maxValue(500));
6
+ var timestampSchema = pipe(string(), toDate());
7
+ var querySchema = pipe(object({
8
+ limit: optional(limitSchema, "100"),
9
+ beforeCreatedAt: optional(timestampSchema),
10
+ beforeId: optional(authIdSchema)
11
+ }), check((value) => Boolean(value.beforeCreatedAt) === Boolean(value.beforeId)));
12
+ var get_default = defineRoute({ async fetch(scope) {
13
+ const session = await requireSession(scope);
14
+ const { organizationId } = useParameters(scope, organizationParametersSchema);
15
+ const query = useQuery(scope, querySchema);
16
+ const storage = await useServerAuth(scope);
17
+ const organization = await storage.getOrganization(organizationId);
18
+ const membership = await storage.getMembership(organizationId, session.accountId);
19
+ if (!organization || membership?.status !== "active" || organization.accountId !== session.accountId || !await storage.getAccountById(session.accountId)) return sendForbidden(scope);
20
+ return sendOk(scope, await storage.listTraces(organizationId, query.limit, query.beforeCreatedAt && query.beforeId ? {
21
+ createdAt: query.beforeCreatedAt,
22
+ id: query.beforeId
23
+ } : void 0));
24
+ } });
25
+ //#endregion
26
+ export { get_default as default };
@@ -0,0 +1,4 @@
1
+ declare const _default: {
2
+ fetch(scope: import("revojs").Scope): Promise<Response>;
3
+ };
4
+ export default _default;
@@ -0,0 +1,11 @@
1
+ import { requireSession, useServerAuth } from "../../../../index.js";
2
+ import { defineRoute, sendForbidden, sendOk } from "@revojs/server";
3
+ //#region src/organization/routes/auth/organizations/get.ts
4
+ var get_default = defineRoute({ async fetch(scope) {
5
+ const session = await requireSession(scope);
6
+ const storage = await useServerAuth(scope);
7
+ if (!await storage.getAccountById(session.accountId)) return sendForbidden(scope);
8
+ return sendOk(scope, await storage.listOrganizations(session.accountId));
9
+ } });
10
+ //#endregion
11
+ export { get_default as default };
@@ -0,0 +1,4 @@
1
+ declare const _default: {
2
+ fetch(scope: import("revojs").Scope): Promise<Response>;
3
+ };
4
+ export default _default;
@@ -0,0 +1,40 @@
1
+ import { organizationNameSchema, requireSession, useServerAuth } from "../../../../index.js";
2
+ import { defineRoute, requireSameOrigin, sendForbidden, sendOk, useBody } from "@revojs/server";
3
+ import { object } from "valibot";
4
+ //#region src/organization/routes/auth/organizations/post.ts
5
+ var post_default = defineRoute({ async fetch(scope) {
6
+ await requireSession(scope);
7
+ requireSameOrigin(scope);
8
+ const { name } = await useBody(scope, object({ name: organizationNameSchema }));
9
+ const organization = await (await useServerAuth(scope)).transaction(async (database) => {
10
+ const session = await requireSession(scope);
11
+ if (!await database.getAccountById(session.accountId)) throw sendForbidden(scope);
12
+ const organization = {
13
+ id: crypto.randomUUID(),
14
+ name,
15
+ accountId: session.accountId,
16
+ createdAt: /* @__PURE__ */ new Date()
17
+ };
18
+ await database.createOrganization(organization);
19
+ await database.createMembership({
20
+ id: crypto.randomUUID(),
21
+ organizationId: organization.id,
22
+ accountId: session.accountId,
23
+ status: "active",
24
+ createdAt: /* @__PURE__ */ new Date()
25
+ });
26
+ await database.createTrace({
27
+ id: crypto.randomUUID(),
28
+ organizationId: organization.id,
29
+ accountId: session.accountId,
30
+ action: "organization.create",
31
+ subjectId: organization.id,
32
+ content: "{}",
33
+ createdAt: /* @__PURE__ */ new Date()
34
+ });
35
+ return organization;
36
+ });
37
+ return sendOk(scope, organization);
38
+ } });
39
+ //#endregion
40
+ export { post_default as default };
@@ -0,0 +1,4 @@
1
+ declare const _default: {
2
+ fetch(scope: import("revojs").Scope, next: (() => import("@revojs/server").Result) | undefined): import("@revojs/server").Result | undefined;
3
+ };
4
+ export default _default;
@@ -0,0 +1,9 @@
1
+ import { defineMiddleware, useServer } from "@revojs/server";
2
+ //#region src/routes/auth/+middleware.ts
3
+ var _middleware_default = defineMiddleware({ fetch(scope, next) {
4
+ const { response } = useServer(scope);
5
+ response.headers.set("Cache-Control", "no-store");
6
+ return next?.();
7
+ } });
8
+ //#endregion
9
+ export { _middleware_default as default };
@@ -1,10 +1,9 @@
1
- import { DELETE_SESSION_HOOK, clearSession, getSession } from "../../../index.js";
1
+ import { clearSession, getSession, useAccountStorage } from "../../../index.js";
2
2
  import { defineRoute, sendOk } from "@revojs/server";
3
- import { dispatchHook } from "revojs";
4
3
  //#region src/routes/auth/sessions/delete.ts
5
4
  var delete_default = defineRoute({ async fetch(scope) {
6
5
  const session = await getSession(scope);
7
- if (session) await dispatchHook(scope, DELETE_SESSION_HOOK, session);
6
+ if (session) await (await useAccountStorage(scope)).deleteSession(session.id, session.accountId);
8
7
  clearSession(scope);
9
8
  return sendOk(scope);
10
9
  } });