@robelest/convex-auth 0.0.4-preview.22 → 0.0.4-preview.24
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/README.md +10 -11
- package/dist/authorization/index.d.ts +1 -1
- package/dist/authorization/index.js +1 -1
- package/dist/authorization/index.js.map +1 -1
- package/dist/client/index.d.ts +1 -2
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +36 -39
- package/dist/client/index.js.map +1 -1
- package/dist/component/client/index.d.ts +1 -2
- package/dist/component/index.js +2 -2
- package/dist/component/model.d.ts +9 -9
- package/dist/component/model.d.ts.map +1 -1
- package/dist/component/public/enterprise/audit.d.ts.map +1 -1
- package/dist/component/public/enterprise/audit.js.map +1 -1
- package/dist/component/public/enterprise/core.d.ts.map +1 -1
- package/dist/component/public/enterprise/core.js.map +1 -1
- package/dist/component/public/enterprise/domains.d.ts.map +1 -1
- package/dist/component/public/enterprise/domains.js.map +1 -1
- package/dist/component/public/enterprise/scim.d.ts.map +1 -1
- package/dist/component/public/enterprise/scim.js.map +1 -1
- package/dist/component/public/enterprise/secrets.d.ts.map +1 -1
- package/dist/component/public/enterprise/secrets.js.map +1 -1
- package/dist/component/public/enterprise/webhooks.d.ts.map +1 -1
- package/dist/component/public/enterprise/webhooks.js.map +1 -1
- package/dist/component/public/factors/devices.d.ts.map +1 -1
- package/dist/component/public/factors/devices.js.map +1 -1
- package/dist/component/public/factors/passkeys.d.ts.map +1 -1
- package/dist/component/public/factors/passkeys.js.map +1 -1
- package/dist/component/public/factors/totp.d.ts.map +1 -1
- package/dist/component/public/factors/totp.js.map +1 -1
- package/dist/component/public/groups/core.js.map +1 -1
- package/dist/component/public/groups/invites.d.ts.map +1 -1
- package/dist/component/public/groups/invites.js.map +1 -1
- package/dist/component/public/groups/members.d.ts.map +1 -1
- package/dist/component/public/groups/members.js.map +1 -1
- package/dist/component/public/identity/accounts.d.ts.map +1 -1
- package/dist/component/public/identity/accounts.js.map +1 -1
- package/dist/component/public/identity/codes.d.ts.map +1 -1
- package/dist/component/public/identity/codes.js.map +1 -1
- package/dist/component/public/identity/sessions.d.ts.map +1 -1
- package/dist/component/public/identity/sessions.js.map +1 -1
- package/dist/component/public/identity/tokens.d.ts.map +1 -1
- package/dist/component/public/identity/tokens.js.map +1 -1
- package/dist/component/public/identity/users.d.ts.map +1 -1
- package/dist/component/public/identity/users.js.map +1 -1
- package/dist/component/public/identity/verifiers.d.ts.map +1 -1
- package/dist/component/public/identity/verifiers.js.map +1 -1
- package/dist/component/public/security/keys.d.ts.map +1 -1
- package/dist/component/public/security/keys.js.map +1 -1
- package/dist/component/public/security/limits.d.ts.map +1 -1
- package/dist/component/public/security/limits.js.map +1 -1
- package/dist/component/schema.d.ts +41 -41
- package/dist/component/server/auth.d.ts +127 -130
- package/dist/component/server/auth.d.ts.map +1 -1
- package/dist/component/server/auth.js +100 -64
- package/dist/component/server/auth.js.map +1 -1
- package/dist/component/server/context.js +53 -0
- package/dist/component/server/context.js.map +1 -0
- package/dist/component/server/core.js +113 -250
- package/dist/component/server/core.js.map +1 -1
- package/dist/component/server/crypto.js +25 -7
- package/dist/component/server/crypto.js.map +1 -1
- package/dist/component/server/device.js +59 -16
- package/dist/component/server/device.js.map +1 -1
- package/dist/component/server/enterprise/domain.js +148 -59
- package/dist/component/server/enterprise/domain.js.map +1 -1
- package/dist/component/server/enterprise/http.js +36 -15
- package/dist/component/server/enterprise/http.js.map +1 -1
- package/dist/component/server/enterprise/oidc.js +1 -1
- package/dist/component/server/http.d.ts +85 -0
- package/dist/component/server/http.d.ts.map +1 -0
- package/dist/component/server/http.js +85 -22
- package/dist/component/server/http.js.map +1 -1
- package/dist/component/server/identity.js +5 -2
- package/dist/component/server/identity.js.map +1 -1
- package/dist/component/server/limits.js +21 -30
- package/dist/component/server/limits.js.map +1 -1
- package/dist/component/server/mutations/account.js +12 -10
- package/dist/component/server/mutations/account.js.map +1 -1
- package/dist/component/server/mutations/code.js +5 -2
- package/dist/component/server/mutations/code.js.map +1 -1
- package/dist/component/server/mutations/invalidate.js +1 -1
- package/dist/component/server/mutations/invalidate.js.map +1 -1
- package/dist/component/server/mutations/oauth.js +10 -4
- package/dist/component/server/mutations/oauth.js.map +1 -1
- package/dist/component/server/mutations/refresh.js +2 -2
- package/dist/component/server/mutations/refresh.js.map +1 -1
- package/dist/component/server/mutations/register.js +46 -42
- package/dist/component/server/mutations/register.js.map +1 -1
- package/dist/component/server/mutations/retrieve.js +21 -25
- package/dist/component/server/mutations/retrieve.js.map +1 -1
- package/dist/component/server/mutations/signature.js +10 -4
- package/dist/component/server/mutations/signature.js.map +1 -1
- package/dist/component/server/mutations/signout.js.map +1 -1
- package/dist/component/server/mutations/store.js +9 -24
- package/dist/component/server/mutations/store.js.map +1 -1
- package/dist/component/server/mutations/verifier.js.map +1 -1
- package/dist/component/server/mutations/verify.js +1 -1
- package/dist/component/server/mutations/verify.js.map +1 -1
- package/dist/component/server/oauth.js +53 -16
- package/dist/component/server/oauth.js.map +1 -1
- package/dist/component/server/passkey.js +115 -31
- package/dist/component/server/passkey.js.map +1 -1
- package/dist/component/server/redirects.js +9 -3
- package/dist/component/server/redirects.js.map +1 -1
- package/dist/component/server/refresh.js +10 -7
- package/dist/component/server/refresh.js.map +1 -1
- package/dist/component/server/runtime.d.ts +5 -5
- package/dist/component/server/runtime.js +156 -113
- package/dist/component/server/runtime.js.map +1 -1
- package/dist/component/server/signin.js +34 -10
- package/dist/component/server/signin.js.map +1 -1
- package/dist/component/server/totp.js +79 -19
- package/dist/component/server/totp.js.map +1 -1
- package/dist/component/server/types.d.ts +12 -20
- package/dist/component/server/types.d.ts.map +1 -1
- package/dist/component/server/types.js.map +1 -1
- package/dist/component/server/users.js +6 -3
- package/dist/component/server/users.js.map +1 -1
- package/dist/component/server/utils.js +10 -4
- package/dist/component/server/utils.js.map +1 -1
- package/dist/core/types.d.ts +14 -22
- package/dist/core/types.d.ts.map +1 -1
- package/dist/factors/device.js +8 -9
- package/dist/factors/device.js.map +1 -1
- package/dist/factors/passkey.js +18 -21
- package/dist/factors/passkey.js.map +1 -1
- package/dist/providers/password.js +66 -81
- package/dist/providers/password.js.map +1 -1
- package/dist/runtime/invite.js +2 -8
- package/dist/runtime/invite.js.map +1 -1
- package/dist/server/auth.d.ts +127 -130
- package/dist/server/auth.d.ts.map +1 -1
- package/dist/server/auth.js +100 -64
- package/dist/server/auth.js.map +1 -1
- package/dist/server/context.d.ts +1 -0
- package/dist/server/context.js +53 -0
- package/dist/server/context.js.map +1 -0
- package/dist/server/core.d.ts +74 -195
- package/dist/server/core.d.ts.map +1 -1
- package/dist/server/core.js +113 -250
- package/dist/server/core.js.map +1 -1
- package/dist/server/crypto.d.ts.map +1 -1
- package/dist/server/crypto.js +25 -7
- package/dist/server/crypto.js.map +1 -1
- package/dist/server/device.js +59 -16
- package/dist/server/device.js.map +1 -1
- package/dist/server/enterprise/domain.d.ts +0 -8
- package/dist/server/enterprise/domain.d.ts.map +1 -1
- package/dist/server/enterprise/domain.js +148 -59
- package/dist/server/enterprise/domain.js.map +1 -1
- package/dist/server/enterprise/http.d.ts.map +1 -1
- package/dist/server/enterprise/http.js +35 -14
- package/dist/server/enterprise/http.js.map +1 -1
- package/dist/server/http.d.ts +81 -3
- package/dist/server/http.d.ts.map +1 -1
- package/dist/server/http.js +84 -21
- package/dist/server/http.js.map +1 -1
- package/dist/server/identity.js +5 -2
- package/dist/server/identity.js.map +1 -1
- package/dist/server/index.d.ts +3 -2
- package/dist/server/index.js +2 -2
- package/dist/server/limits.js +21 -30
- package/dist/server/limits.js.map +1 -1
- package/dist/server/mounts.d.ts +25 -63
- package/dist/server/mounts.d.ts.map +1 -1
- package/dist/server/mounts.js +46 -107
- package/dist/server/mounts.js.map +1 -1
- package/dist/server/mutations/account.d.ts +8 -9
- package/dist/server/mutations/account.d.ts.map +1 -1
- package/dist/server/mutations/account.js +11 -9
- package/dist/server/mutations/account.js.map +1 -1
- package/dist/server/mutations/code.d.ts +12 -12
- package/dist/server/mutations/code.d.ts.map +1 -1
- package/dist/server/mutations/code.js +5 -2
- package/dist/server/mutations/code.js.map +1 -1
- package/dist/server/mutations/invalidate.d.ts +4 -4
- package/dist/server/mutations/invalidate.d.ts.map +1 -1
- package/dist/server/mutations/invalidate.js.map +1 -1
- package/dist/server/mutations/oauth.d.ts +14 -12
- package/dist/server/mutations/oauth.d.ts.map +1 -1
- package/dist/server/mutations/oauth.js +9 -3
- package/dist/server/mutations/oauth.js.map +1 -1
- package/dist/server/mutations/refresh.d.ts +3 -3
- package/dist/server/mutations/refresh.d.ts.map +1 -1
- package/dist/server/mutations/refresh.js +1 -1
- package/dist/server/mutations/refresh.js.map +1 -1
- package/dist/server/mutations/register.d.ts +11 -11
- package/dist/server/mutations/register.d.ts.map +1 -1
- package/dist/server/mutations/register.js +45 -41
- package/dist/server/mutations/register.js.map +1 -1
- package/dist/server/mutations/retrieve.d.ts +6 -6
- package/dist/server/mutations/retrieve.d.ts.map +1 -1
- package/dist/server/mutations/retrieve.js +20 -24
- package/dist/server/mutations/retrieve.js.map +1 -1
- package/dist/server/mutations/signature.d.ts +6 -7
- package/dist/server/mutations/signature.d.ts.map +1 -1
- package/dist/server/mutations/signature.js +9 -3
- package/dist/server/mutations/signature.js.map +1 -1
- package/dist/server/mutations/signin.d.ts +5 -5
- package/dist/server/mutations/signout.js.map +1 -1
- package/dist/server/mutations/store.d.ts +83 -83
- package/dist/server/mutations/store.js +8 -23
- package/dist/server/mutations/store.js.map +1 -1
- package/dist/server/mutations/verifier.js.map +1 -1
- package/dist/server/mutations/verify.d.ts +7 -7
- package/dist/server/mutations/verify.d.ts.map +1 -1
- package/dist/server/mutations/verify.js.map +1 -1
- package/dist/server/oauth.js +53 -16
- package/dist/server/oauth.js.map +1 -1
- package/dist/server/passkey.d.ts +2 -2
- package/dist/server/passkey.d.ts.map +1 -1
- package/dist/server/passkey.js +114 -30
- package/dist/server/passkey.js.map +1 -1
- package/dist/server/redirects.js +9 -3
- package/dist/server/redirects.js.map +1 -1
- package/dist/server/refresh.js +10 -7
- package/dist/server/refresh.js.map +1 -1
- package/dist/server/runtime.d.ts +11 -11
- package/dist/server/runtime.js +155 -112
- package/dist/server/runtime.js.map +1 -1
- package/dist/server/signin.js +34 -10
- package/dist/server/signin.js.map +1 -1
- package/dist/server/ssr.d.ts.map +1 -1
- package/dist/server/ssr.js +175 -184
- package/dist/server/ssr.js.map +1 -1
- package/dist/server/totp.js +78 -18
- package/dist/server/totp.js.map +1 -1
- package/dist/server/types.d.ts +13 -21
- package/dist/server/types.d.ts.map +1 -1
- package/dist/server/types.js.map +1 -1
- package/dist/server/users.js +6 -3
- package/dist/server/users.js.map +1 -1
- package/dist/server/utils.js +10 -4
- package/dist/server/utils.js.map +1 -1
- package/package.json +1 -5
- package/src/authorization/index.ts +1 -1
- package/src/client/core/types.ts +14 -14
- package/src/client/factors/device.ts +10 -12
- package/src/client/factors/passkey.ts +23 -26
- package/src/client/index.ts +54 -64
- package/src/client/runtime/invite.ts +5 -7
- package/src/component/index.ts +9 -3
- package/src/component/public/enterprise/audit.ts +6 -1
- package/src/component/public/enterprise/core.ts +1 -0
- package/src/component/public/enterprise/domains.ts +5 -1
- package/src/component/public/enterprise/scim.ts +1 -0
- package/src/component/public/enterprise/secrets.ts +1 -0
- package/src/component/public/enterprise/webhooks.ts +1 -0
- package/src/component/public/factors/devices.ts +1 -0
- package/src/component/public/factors/passkeys.ts +1 -0
- package/src/component/public/factors/totp.ts +1 -0
- package/src/component/public/groups/core.ts +1 -1
- package/src/component/public/groups/invites.ts +7 -1
- package/src/component/public/groups/members.ts +1 -0
- package/src/component/public/identity/accounts.ts +1 -0
- package/src/component/public/identity/codes.ts +1 -0
- package/src/component/public/identity/sessions.ts +1 -0
- package/src/component/public/identity/tokens.ts +1 -0
- package/src/component/public/identity/users.ts +1 -0
- package/src/component/public/identity/verifiers.ts +1 -0
- package/src/component/public/security/keys.ts +1 -0
- package/src/component/public/security/limits.ts +1 -0
- package/src/providers/password.ts +89 -110
- package/src/server/auth.ts +240 -182
- package/src/server/context.ts +90 -0
- package/src/server/core.ts +195 -286
- package/src/server/crypto.ts +31 -29
- package/src/server/device.ts +65 -32
- package/src/server/enterprise/domain.ts +158 -170
- package/src/server/enterprise/http.ts +46 -39
- package/src/server/http.ts +289 -30
- package/src/server/identity.ts +5 -5
- package/src/server/index.ts +9 -3
- package/src/server/limits.ts +53 -80
- package/src/server/mounts.ts +56 -80
- package/src/server/mutations/account.ts +22 -36
- package/src/server/mutations/code.ts +6 -6
- package/src/server/mutations/invalidate.ts +1 -1
- package/src/server/mutations/oauth.ts +14 -8
- package/src/server/mutations/refresh.ts +5 -4
- package/src/server/mutations/register.ts +87 -132
- package/src/server/mutations/retrieve.ts +44 -44
- package/src/server/mutations/signature.ts +13 -6
- package/src/server/mutations/signout.ts +1 -1
- package/src/server/mutations/store.ts +16 -31
- package/src/server/mutations/verifier.ts +1 -1
- package/src/server/mutations/verify.ts +3 -5
- package/src/server/oauth.ts +60 -69
- package/src/server/passkey.ts +567 -517
- package/src/server/redirects.ts +10 -6
- package/src/server/refresh.ts +14 -18
- package/src/server/runtime.ts +340 -302
- package/src/server/signin.ts +44 -37
- package/src/server/ssr.ts +390 -407
- package/src/server/totp.ts +85 -35
- package/src/server/types.ts +19 -22
- package/src/server/users.ts +7 -6
- package/src/server/utils.ts +10 -12
- package/dist/component/server/authError.js +0 -34
- package/dist/component/server/authError.js.map +0 -1
- package/dist/component/server/errors.d.ts +0 -1
- package/dist/component/server/errors.js +0 -137
- package/dist/component/server/errors.js.map +0 -1
- package/dist/server/authError.d.ts +0 -46
- package/dist/server/authError.d.ts.map +0 -1
- package/dist/server/authError.js +0 -34
- package/dist/server/authError.js.map +0 -1
- package/dist/server/errors.d.ts +0 -177
- package/dist/server/errors.d.ts.map +0 -1
- package/dist/server/errors.js +0 -212
- package/dist/server/errors.js.map +0 -1
- package/src/server/authError.ts +0 -44
- package/src/server/errors.ts +0 -290
package/dist/server/auth.d.ts
CHANGED
|
@@ -13,7 +13,7 @@ import { GenericId } from "convex/values";
|
|
|
13
13
|
type AuthConfig = Omit<ConvexAuthConfig, "component">;
|
|
14
14
|
/** Canonical user document type exposed by Convex Auth. */
|
|
15
15
|
type UserDoc = Doc<"User">;
|
|
16
|
-
type MemberApiWithAuthorization<TAuthorization extends AuthAuthorizationConfig | undefined> = Omit<ReturnType<typeof Auth$1>["auth"]["member"], "create" | "list" | "update" | "
|
|
16
|
+
type MemberApiWithAuthorization<TAuthorization extends AuthAuthorizationConfig | undefined> = Omit<ReturnType<typeof Auth$1>["auth"]["member"], "create" | "list" | "update" | "inspect" | "require"> & {
|
|
17
17
|
create: (ctx: Parameters<ReturnType<typeof Auth$1>["auth"]["member"]["create"]>[0], data: {
|
|
18
18
|
groupId: string;
|
|
19
19
|
userId: string;
|
|
@@ -21,7 +21,6 @@ type MemberApiWithAuthorization<TAuthorization extends AuthAuthorizationConfig |
|
|
|
21
21
|
status?: string;
|
|
22
22
|
extend?: Record<string, unknown>;
|
|
23
23
|
}) => Promise<{
|
|
24
|
-
ok: true;
|
|
25
24
|
memberId: string;
|
|
26
25
|
}>;
|
|
27
26
|
list: (ctx: Parameters<ReturnType<typeof Auth$1>["auth"]["member"]["list"]>[0], opts?: {
|
|
@@ -39,17 +38,22 @@ type MemberApiWithAuthorization<TAuthorization extends AuthAuthorizationConfig |
|
|
|
39
38
|
update: (ctx: Parameters<ReturnType<typeof Auth$1>["auth"]["member"]["update"]>[0], memberId: string, data: Record<string, unknown> & {
|
|
40
39
|
roleIds?: AuthRoleId<TAuthorization>[];
|
|
41
40
|
}) => Promise<{
|
|
42
|
-
ok: true;
|
|
43
41
|
memberId: string;
|
|
44
42
|
}>;
|
|
45
|
-
|
|
43
|
+
inspect: (ctx: Parameters<ReturnType<typeof Auth$1>["auth"]["member"]["inspect"]>[0], opts: {
|
|
44
|
+
userId: string;
|
|
45
|
+
groupId: string;
|
|
46
|
+
ancestry?: boolean;
|
|
47
|
+
maxDepth?: number;
|
|
48
|
+
}) => ReturnType<ReturnType<typeof Auth$1>["auth"]["member"]["inspect"]>;
|
|
49
|
+
require: (ctx: Parameters<ReturnType<typeof Auth$1>["auth"]["member"]["require"]>[0], opts: {
|
|
46
50
|
userId: string;
|
|
47
51
|
groupId: string;
|
|
48
52
|
ancestry?: boolean;
|
|
49
53
|
roleIds?: AuthRoleId<TAuthorization>[];
|
|
50
54
|
grants?: AuthGrant<TAuthorization>[];
|
|
51
55
|
maxDepth?: number;
|
|
52
|
-
}) => ReturnType<ReturnType<typeof Auth$1>["auth"]["member"]["
|
|
56
|
+
}) => ReturnType<ReturnType<typeof Auth$1>["auth"]["member"]["require"]>;
|
|
53
57
|
};
|
|
54
58
|
/**
|
|
55
59
|
* The base auth API surface returned by {@link createAuth}.
|
|
@@ -80,30 +84,40 @@ type AuthApiBase<TAuthorization extends AuthAuthorizationConfig | undefined = un
|
|
|
80
84
|
key: ReturnType<typeof Auth$1>["auth"]["key"];
|
|
81
85
|
http: ReturnType<typeof Auth$1>["auth"]["http"];
|
|
82
86
|
/**
|
|
83
|
-
* Resolve the current
|
|
87
|
+
* Resolve the current request's auth context. Framework-agnostic — use
|
|
84
88
|
* this in fluent-convex middleware, custom wrappers, or anywhere you
|
|
85
|
-
* need the
|
|
89
|
+
* need the current `{ userId, user, groupId, role, grants }` object.
|
|
86
90
|
*
|
|
87
|
-
*
|
|
91
|
+
* Throws a structured `ConvexError` when unauthenticated by default.
|
|
92
|
+
* Pass `{ optional: true }` to get a null-shaped auth object instead.
|
|
88
93
|
*
|
|
89
94
|
* @param ctx - Convex query, mutation, or action context.
|
|
90
|
-
* @
|
|
95
|
+
* @param config - Optional auth resolution config. Supports `optional`,
|
|
96
|
+
* `resolve`, and `authResolve`.
|
|
97
|
+
* @returns The current auth context.
|
|
91
98
|
*
|
|
92
99
|
* @example fluent-convex middleware
|
|
93
100
|
* ```ts
|
|
94
101
|
* const withAuth = convex.createMiddleware(async (ctx, next) => {
|
|
95
|
-
* return next({ ...ctx, auth: await auth.
|
|
102
|
+
* return next({ ...ctx, auth: await auth.context(ctx) });
|
|
96
103
|
* });
|
|
97
104
|
* ```
|
|
98
105
|
*
|
|
99
106
|
* @example Direct usage in a handler
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
+
* ```ts
|
|
108
|
+
* const authContext = await auth.context(ctx);
|
|
109
|
+
* const { userId, grants } = authContext;
|
|
110
|
+
* ```
|
|
111
|
+
*
|
|
112
|
+
* @example Optional usage
|
|
113
|
+
* ```ts
|
|
114
|
+
* const authContext = await auth.context(ctx, { optional: true });
|
|
115
|
+
* if (authContext.userId === null) {
|
|
116
|
+
* return null;
|
|
117
|
+
* }
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
context: AuthContextResolver;
|
|
107
121
|
/**
|
|
108
122
|
* Context enrichment for convex-helpers `customQuery` / `customMutation` /
|
|
109
123
|
* `customAction`.
|
|
@@ -112,9 +126,9 @@ type AuthApiBase<TAuthorization extends AuthAuthorizationConfig | undefined = un
|
|
|
112
126
|
* and grants, then attaches them to `ctx.auth`. Returns a `Customization`
|
|
113
127
|
* object compatible with convex-helpers' custom function builders.
|
|
114
128
|
*
|
|
115
|
-
* `ctx.auth` is
|
|
116
|
-
*
|
|
117
|
-
*
|
|
129
|
+
* `ctx.auth` is the current request auth context.
|
|
130
|
+
* By default this throws when unauthenticated so handlers can assume
|
|
131
|
+
* `ctx.auth.userId` and `ctx.auth.user` exist.
|
|
118
132
|
*
|
|
119
133
|
* @returns A convex-helpers `Customization` object.
|
|
120
134
|
*
|
|
@@ -136,37 +150,29 @@ type AuthApiBase<TAuthorization extends AuthAuthorizationConfig | undefined = un
|
|
|
136
150
|
* export const list = authQuery({
|
|
137
151
|
* args: { workspaceId: v.string() },
|
|
138
152
|
* handler: async (ctx, args) => {
|
|
139
|
-
* if (!ctx.auth) return [];
|
|
140
153
|
* const { userId, groupId, grants } = ctx.auth;
|
|
141
154
|
* // business logic
|
|
142
155
|
* },
|
|
143
156
|
* });
|
|
144
157
|
* ```
|
|
145
158
|
*/
|
|
146
|
-
ctx:
|
|
147
|
-
args: Record<string, never>;
|
|
148
|
-
input: (ctx: any) => Promise<{
|
|
149
|
-
ctx: {
|
|
150
|
-
auth: AuthResolvedContext | null;
|
|
151
|
-
};
|
|
152
|
-
args: Record<string, never>;
|
|
153
|
-
}>;
|
|
154
|
-
};
|
|
159
|
+
ctx: AuthContextFactory;
|
|
155
160
|
};
|
|
156
161
|
/**
|
|
157
|
-
*
|
|
158
|
-
*
|
|
159
|
-
*
|
|
162
|
+
* Current request auth context injected into `ctx.auth` by `auth.ctx()`. This
|
|
163
|
+
* is the authenticated auth shape returned by {@link createAuth().context}.
|
|
164
|
+
* Optional context builders may still surface nullable fields when
|
|
165
|
+
* `optional: true` is used.
|
|
160
166
|
*
|
|
161
|
-
* - `null` when unauthenticated.
|
|
162
167
|
* - `groupId` is `null` when the user has no active group set.
|
|
163
|
-
* - `role`
|
|
168
|
+
* - `role` is `null` when no active group or no membership is resolved.
|
|
169
|
+
* - `grants` is `[]` when no active group or no membership is resolved.
|
|
164
170
|
*
|
|
165
171
|
* @example
|
|
166
172
|
* ```ts
|
|
167
|
-
* import type {
|
|
173
|
+
* import type { AuthContext } from "@robelest/convex-auth/server";
|
|
168
174
|
*
|
|
169
|
-
* const mockAuth:
|
|
175
|
+
* const mockAuth: AuthContext = {
|
|
170
176
|
* userId: "user123" as Id<"User">,
|
|
171
177
|
* user: { _id: "user123", email: "test@example.com" },
|
|
172
178
|
* groupId: "group456",
|
|
@@ -175,23 +181,66 @@ type AuthApiBase<TAuthorization extends AuthAuthorizationConfig | undefined = un
|
|
|
175
181
|
* };
|
|
176
182
|
* ```
|
|
177
183
|
*/
|
|
178
|
-
type
|
|
184
|
+
type AuthContext = {
|
|
179
185
|
/** The authenticated user's document ID. */userId: GenericId<"User">; /** The authenticated user's full document. */
|
|
180
186
|
user: UserDoc; /** The user's active group ID, or `null` if none set. */
|
|
181
187
|
groupId: string | null; /** The user's primary role in the active group, or `null`. */
|
|
182
188
|
role: string | null; /** Resolved grant strings from the user's role definitions. */
|
|
183
189
|
grants: string[];
|
|
184
190
|
};
|
|
185
|
-
|
|
191
|
+
/**
|
|
192
|
+
* Nullable auth context returned by `auth.context(ctx, { optional: true })`
|
|
193
|
+
* and injected by `auth.ctx({ optional: true })`.
|
|
194
|
+
*
|
|
195
|
+
* Use this when callers may be unauthenticated but you still want a stable
|
|
196
|
+
* auth-shaped object.
|
|
197
|
+
*
|
|
198
|
+
* - `userId` and `user` are `null` when unauthenticated.
|
|
199
|
+
* - `groupId` and `role` are `null` when no active group is resolved.
|
|
200
|
+
* - `grants` is `[]` when no membership is resolved.
|
|
201
|
+
*
|
|
202
|
+
* @example
|
|
203
|
+
* ```ts
|
|
204
|
+
* const authContext = await auth.context(ctx, { optional: true });
|
|
205
|
+
* if (authContext.userId === null) {
|
|
206
|
+
* return null;
|
|
207
|
+
* }
|
|
208
|
+
* ```
|
|
209
|
+
*/
|
|
210
|
+
type OptionalAuthContext = {
|
|
211
|
+
/** The authenticated user's document ID, or `null` when unauthenticated. */userId: GenericId<"User"> | null; /** The authenticated user's full document, or `null` when unauthenticated. */
|
|
212
|
+
user: UserDoc | null; /** The user's active group ID, or `null` if none is set. */
|
|
213
|
+
groupId: string | null; /** The user's primary role in the active group, or `null`. */
|
|
214
|
+
role: string | null; /** Resolved grant strings for the active membership, or `[]`. */
|
|
215
|
+
grants: string[];
|
|
216
|
+
};
|
|
217
|
+
type AuthContextBase = {
|
|
186
218
|
getUserIdentity: () => Promise<UserIdentity | null>;
|
|
187
219
|
};
|
|
188
|
-
type
|
|
189
|
-
type
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
220
|
+
type RequiredAuthContextState = AuthContextBase & AuthContext;
|
|
221
|
+
type OptionalAuthContextState = AuthContextBase & OptionalAuthContext;
|
|
222
|
+
type ResolvedAuthContext<TResolve> = AuthContext & TResolve;
|
|
223
|
+
type ResolvedOptionalAuthContext<TResolve> = OptionalAuthContext & TResolve;
|
|
224
|
+
type AuthContextResolver = {
|
|
225
|
+
<TResolve extends Record<string, unknown> = Record<string, never>>(ctx: any, config: AuthContextConfig<TResolve> & {
|
|
226
|
+
optional: true;
|
|
227
|
+
}): Promise<ResolvedOptionalAuthContext<TResolve>>;
|
|
228
|
+
<TResolve extends Record<string, unknown> = Record<string, never>>(ctx: any, config?: AuthContextConfig<TResolve>): Promise<ResolvedAuthContext<TResolve>>;
|
|
229
|
+
};
|
|
230
|
+
type AuthContextCustomization<TAuth> = {
|
|
231
|
+
args: {};
|
|
232
|
+
input: (ctx: any, _args: any, _extra?: any) => Promise<{
|
|
233
|
+
ctx: {
|
|
234
|
+
auth: TAuth;
|
|
235
|
+
};
|
|
236
|
+
args: {};
|
|
237
|
+
}>;
|
|
238
|
+
};
|
|
239
|
+
type AuthContextFactory = {
|
|
240
|
+
<TResolve extends Record<string, unknown> = Record<string, never>>(config: AuthContextConfig<TResolve> & {
|
|
241
|
+
optional: true;
|
|
242
|
+
}): AuthContextCustomization<OptionalAuthContextState & TResolve>;
|
|
243
|
+
<TResolve extends Record<string, unknown> = Record<string, never>>(config?: AuthContextConfig<TResolve>): AuthContextCustomization<RequiredAuthContextState & TResolve>;
|
|
195
244
|
};
|
|
196
245
|
type InternalSsoApi = ReturnType<typeof Auth$1>["auth"]["sso"];
|
|
197
246
|
type PublicSsoAdminApi = {
|
|
@@ -203,7 +252,6 @@ type PublicSsoAdminApi = {
|
|
|
203
252
|
domain: string;
|
|
204
253
|
isPrimary?: boolean;
|
|
205
254
|
}>) => Promise<{
|
|
206
|
-
ok: true;
|
|
207
255
|
enterpriseId: string;
|
|
208
256
|
domains: Array<{
|
|
209
257
|
domainId: string;
|
|
@@ -218,7 +266,6 @@ type PublicSsoAdminApi = {
|
|
|
218
266
|
enterpriseId: string;
|
|
219
267
|
domain: string;
|
|
220
268
|
}) => Promise<{
|
|
221
|
-
ok: true;
|
|
222
269
|
enterpriseId: string;
|
|
223
270
|
domain: string;
|
|
224
271
|
requestedAt: number;
|
|
@@ -233,7 +280,6 @@ type PublicSsoAdminApi = {
|
|
|
233
280
|
enterpriseId: string;
|
|
234
281
|
domain: string;
|
|
235
282
|
}) => Promise<{
|
|
236
|
-
ok: boolean;
|
|
237
283
|
enterpriseId: string;
|
|
238
284
|
domain: string;
|
|
239
285
|
verifiedAt?: number;
|
|
@@ -328,35 +374,53 @@ declare function createAuth<P extends AuthProviderConfig[], TAuthorization exten
|
|
|
328
374
|
authorization?: TAuthorization;
|
|
329
375
|
}): ConvexAuthResult<P, TAuthorization>;
|
|
330
376
|
/**
|
|
331
|
-
* Configuration for {@link
|
|
377
|
+
* Configuration for {@link createAuth().ctx} context enrichment.
|
|
378
|
+
*
|
|
379
|
+
* The same config shape is also used by {@link createAuth().context}.
|
|
332
380
|
*
|
|
333
381
|
* @typeParam TResolve - Extra fields returned from `resolve()` and merged into
|
|
334
382
|
* the resulting `ctx.auth` object.
|
|
383
|
+
*
|
|
384
|
+
* @example
|
|
385
|
+
* ```ts
|
|
386
|
+
* const authContext = await auth.context(ctx, {
|
|
387
|
+
* resolve: async (_ctx, user, authState) => ({
|
|
388
|
+
* email: user.email,
|
|
389
|
+
* canWrite: authState.grants.includes("posts.write"),
|
|
390
|
+
* }),
|
|
391
|
+
* });
|
|
392
|
+
* ```
|
|
335
393
|
*/
|
|
336
|
-
type
|
|
337
|
-
/**
|
|
394
|
+
type AuthContextConfig<TResolve extends Record<string, unknown> = Record<string, never>> = {
|
|
395
|
+
/**
|
|
396
|
+
* Allow unauthenticated callers and return a null-shaped auth object instead
|
|
397
|
+
* of throwing `NOT_SIGNED_IN`.
|
|
398
|
+
*/
|
|
399
|
+
optional?: boolean;
|
|
338
400
|
/**
|
|
339
401
|
* Attach additional derived fields to the auth context after the base auth
|
|
340
402
|
* context is resolved.
|
|
403
|
+
*
|
|
404
|
+
* This callback runs only when a user is authenticated.
|
|
341
405
|
*/
|
|
342
|
-
resolve?: (ctx: any, user: UserDoc, auth:
|
|
406
|
+
resolve?: (ctx: any, user: UserDoc, auth: AuthContext) => Promise<TResolve> | TResolve;
|
|
343
407
|
/**
|
|
344
|
-
* Override or wrap the base auth resolution used by {@link
|
|
408
|
+
* Override or wrap the base auth resolution used by {@link createAuth().ctx}.
|
|
345
409
|
*
|
|
346
410
|
* Return `undefined` to fall back to the built-in resolver,
|
|
347
411
|
* `null` for an explicit unauthenticated state, or an
|
|
348
|
-
* {@link
|
|
412
|
+
* {@link AuthContext} object to provide a pre-resolved auth state.
|
|
349
413
|
* This is useful for tests, proxy auth, impersonation flows, or any
|
|
350
414
|
* environment that needs to inject auth without depending on the standard
|
|
351
415
|
* Convex auth tables.
|
|
352
416
|
*
|
|
353
417
|
* @param ctx - The Convex function context.
|
|
354
|
-
* @param fallback - The built-in auth resolver used by {@link
|
|
418
|
+
* @param fallback - The built-in auth resolver used by {@link createAuth().ctx}.
|
|
355
419
|
* @returns Resolved auth state, `null`, or `undefined` to use the fallback.
|
|
356
420
|
*
|
|
357
421
|
* @example
|
|
358
422
|
* ```ts
|
|
359
|
-
* const authCtx =
|
|
423
|
+
* const authCtx = auth.ctx({
|
|
360
424
|
* authResolve: async (ctx, fallback) => {
|
|
361
425
|
* const injected = getInjectedAuth(ctx);
|
|
362
426
|
* return injected ?? (await fallback());
|
|
@@ -364,91 +428,24 @@ type AuthCtxConfig<TResolve extends Record<string, unknown> = Record<string, nev
|
|
|
364
428
|
* });
|
|
365
429
|
* ```
|
|
366
430
|
*/
|
|
367
|
-
authResolve?: (ctx: any, fallback: () => Promise<
|
|
368
|
-
};
|
|
369
|
-
/**
|
|
370
|
-
* Create a context enrichment for `customQuery` / `customMutation` — optional auth.
|
|
371
|
-
*
|
|
372
|
-
* When `optional: true` is set, unauthenticated requests are allowed.
|
|
373
|
-
* The enriched `ctx.auth` will have `userId: null`, `user: null`,
|
|
374
|
-
* `groupId: null`, `role: null`, and `grants: []` for unauthenticated callers.
|
|
375
|
-
*
|
|
376
|
-
* @param auth - The auth API object returned by {@link createAuth}.
|
|
377
|
-
* @param config - Configuration with `optional: true` and an optional
|
|
378
|
-
* `resolve` callback for attaching extra fields to the auth context.
|
|
379
|
-
* @returns An object with `args` and `input` compatible with Convex
|
|
380
|
-
* custom function builders.
|
|
381
|
-
*
|
|
382
|
-
* @example
|
|
383
|
-
* ```ts
|
|
384
|
-
* const authCtx = AuthCtx(auth, {
|
|
385
|
-
* optional: true,
|
|
386
|
-
* resolve: async (_ctx, user) => ({ plan: user?.extend?.plan ?? null }),
|
|
387
|
-
* });
|
|
388
|
-
* ```
|
|
389
|
-
*
|
|
390
|
-
* @see {@link createAuth}
|
|
391
|
-
*/
|
|
392
|
-
declare function AuthCtx<TResolve extends Record<string, unknown> = Record<string, never>>(auth: AuthLike, config: AuthCtxConfig<TResolve> & {
|
|
393
|
-
optional: true;
|
|
394
|
-
}): {
|
|
395
|
-
args: {};
|
|
396
|
-
input: (ctx: any, _args: any, _extra?: any) => Promise<{
|
|
397
|
-
ctx: {
|
|
398
|
-
auth: OptionalAuthCtxState & TResolve;
|
|
399
|
-
};
|
|
400
|
-
args: {};
|
|
401
|
-
}>;
|
|
402
|
-
};
|
|
403
|
-
/**
|
|
404
|
-
* Create a context enrichment for `customQuery` / `customMutation` — required auth (default).
|
|
405
|
-
*
|
|
406
|
-
* When `optional` is omitted or `false`, the inferred type is the authenticated
|
|
407
|
-
* auth shape. At runtime this helper still resolves instead of throwing, so if
|
|
408
|
-
* no user is signed in the returned `ctx.auth.userId` / `ctx.auth.user` are
|
|
409
|
-
* `null`, `ctx.auth.groupId` / `ctx.auth.role` are `null`, and
|
|
410
|
-
* `ctx.auth.grants` is `[]`.
|
|
411
|
-
*
|
|
412
|
-
* @param auth - The auth API object returned by {@link createAuth}.
|
|
413
|
-
* @param config - Optional configuration with a `resolve` callback
|
|
414
|
-
* for attaching extra fields to the auth context.
|
|
415
|
-
* @returns An object with `args` and `input` compatible with Convex
|
|
416
|
-
* custom function builders.
|
|
417
|
-
*
|
|
418
|
-
* @example
|
|
419
|
-
* ```ts
|
|
420
|
-
* const authCtx = AuthCtx(auth, {
|
|
421
|
-
* resolve: async (_ctx, user) => ({ email: user.email }),
|
|
422
|
-
* });
|
|
423
|
-
* ```
|
|
424
|
-
*
|
|
425
|
-
* @see {@link createAuth}
|
|
426
|
-
*/
|
|
427
|
-
declare function AuthCtx<TResolve extends Record<string, unknown> = Record<string, never>>(auth: AuthLike, config?: AuthCtxConfig<TResolve>): {
|
|
428
|
-
args: {};
|
|
429
|
-
input: (ctx: any, _args: any, _extra?: any) => Promise<{
|
|
430
|
-
ctx: {
|
|
431
|
-
auth: RequiredAuthCtxState & TResolve;
|
|
432
|
-
};
|
|
433
|
-
args: {};
|
|
434
|
-
}>;
|
|
431
|
+
authResolve?: (ctx: any, fallback: () => Promise<AuthContext | null>) => Promise<AuthContext | null | undefined> | AuthContext | null | undefined;
|
|
435
432
|
};
|
|
436
433
|
/**
|
|
437
|
-
* Extract the resolved `auth` context type from an
|
|
434
|
+
* Extract the resolved `auth` context type from an `auth.ctx()` customization.
|
|
438
435
|
*
|
|
439
436
|
* Use this to type function parameters or variables that receive the
|
|
440
|
-
* enriched auth context produced by `
|
|
437
|
+
* enriched auth context produced by `auth.ctx()`. The inferred type includes
|
|
441
438
|
* `userId`, `user`, `groupId`, `role`, `grants`, `getUserIdentity`, and any
|
|
442
439
|
* additional fields added by the `resolve` callback. This is the generic
|
|
443
440
|
* utility for reusing the enriched auth shape without manually duplicating
|
|
444
441
|
* conditional auth types.
|
|
445
442
|
*
|
|
446
|
-
* @typeParam T - An `
|
|
443
|
+
* @typeParam T - An `auth.ctx()` return value (must have an `input` method
|
|
447
444
|
* that returns `{ ctx: { auth: ... } }`).
|
|
448
445
|
*
|
|
449
446
|
* @example
|
|
450
447
|
* ```ts
|
|
451
|
-
* const authCtx =
|
|
448
|
+
* const authCtx = auth.ctx({
|
|
452
449
|
* resolve: async (ctx, user) => ({ orgId: user.orgId }),
|
|
453
450
|
* });
|
|
454
451
|
* type Auth = InferAuth<typeof authCtx>;
|
|
@@ -465,5 +462,5 @@ type InferAuth<T extends {
|
|
|
465
462
|
}>;
|
|
466
463
|
}> = Awaited<ReturnType<T["input"]>>["ctx"]["auth"];
|
|
467
464
|
//#endregion
|
|
468
|
-
export { AuthApi, AuthApiBase, AuthConfig,
|
|
465
|
+
export { AuthApi, AuthApiBase, AuthConfig, AuthContext, AuthContextConfig, ConvexAuthResult, InferAuth, InferClientApi, OptionalAuthContext, UserDoc, createAuth };
|
|
469
466
|
//# sourceMappingURL=auth.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth.d.ts","names":[],"sources":["../../src/server/auth.ts"],"mappings":";;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"auth.d.ts","names":[],"sources":["../../src/server/auth.ts"],"mappings":";;;;;;;;;;AAyCA;;KAHY,UAAA,GAAa,IAAA,CAAK,gBAAA;;KAGlB,OAAA,GAAU,GAAA;AAAA,KAEjB,0BAAA,wBACoB,uBAAA,gBACrB,IAAA,CACF,UAAA,QAAkB,MAAA;EAGlB,MAAA,GACE,GAAA,EAAK,UAAA,CACH,UAAA,QAAkB,MAAA,mCAEpB,IAAA;IACE,OAAA;IACA,MAAA;IACA,OAAA,GAAU,UAAA,CAAW,cAAA;IACrB,MAAA;IACA,MAAA,GAAS,MAAA;EAAA,MAER,OAAA;IAAU,QAAA;EAAA;EACf,IAAA,GACE,GAAA,EAAK,UAAA,CACH,UAAA,QAAkB,MAAA,iCAEpB,IAAA;IACE,KAAA;MACE,OAAA;MACA,MAAA;MACA,MAAA,GAAS,UAAA,CAAW,cAAA;MACpB,MAAA;IAAA;IAEF,KAAA;IACA,MAAA;IACA,OAAA;IACA,KAAA;EAAA,MAEC,UAAA,CAAW,UAAA,QAAkB,MAAA;EAClC,MAAA,GACE,GAAA,EAAK,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CAAA"}
|
package/dist/server/auth.js
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createUnauthenticatedAuthContext, getAuthContext } from "./context.js";
|
|
2
2
|
import { Auth } from "./runtime.js";
|
|
3
|
+
import { Cv } from "@robelest/fx/convex";
|
|
3
4
|
|
|
4
5
|
//#region src/server/auth.ts
|
|
5
6
|
/**
|
|
7
|
+
* Auth configuration helpers for Convex Auth.
|
|
8
|
+
*
|
|
9
|
+
* @module
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
6
12
|
* Create an auth API object.
|
|
7
13
|
*
|
|
8
14
|
* When `new SSO()` is included in providers, `auth.sso` and `auth.scim`
|
|
@@ -26,41 +32,29 @@ import { Auth } from "./runtime.js";
|
|
|
26
32
|
* });
|
|
27
33
|
* ```
|
|
28
34
|
*
|
|
29
|
-
* @see {@link
|
|
30
|
-
*/
|
|
31
|
-
/**
|
|
32
|
-
* Resolve auth context for the current user. Returns the enriched
|
|
33
|
-
* `ctx.auth` object or `null` when unauthenticated.
|
|
34
|
-
*
|
|
35
|
-
* Resolution flow:
|
|
36
|
-
* 1. `user.id(ctx)` → userId or null (exit early)
|
|
37
|
-
* 2. `user.get(ctx, userId)` → user doc (cached per-execution)
|
|
38
|
-
* 3. `user.getActiveGroup(ctx, { userId })` → groupId or null
|
|
39
|
-
* 4. If groupId → `member.resolve(ctx, { userId, groupId })` → role + grants
|
|
35
|
+
* @see {@link AuthContextConfig}
|
|
40
36
|
*/
|
|
41
|
-
async function
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
37
|
+
async function resolveConfiguredAuthContext(auth, ctx, config) {
|
|
38
|
+
const fallback = () => getAuthContext(auth, ctx);
|
|
39
|
+
const authOverride = config?.authResolve ? await config.authResolve(ctx, fallback) : void 0;
|
|
40
|
+
return authOverride === void 0 ? await fallback() : authOverride;
|
|
41
|
+
}
|
|
42
|
+
function createNotSignedInError() {
|
|
43
|
+
return Cv.error({
|
|
44
|
+
code: "NOT_SIGNED_IN",
|
|
45
|
+
message: "Authentication required."
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
async function createPublicAuthContext(auth, ctx, config) {
|
|
49
|
+
const resolved = await resolveConfiguredAuthContext(auth, ctx, config);
|
|
50
|
+
if (resolved === null) {
|
|
51
|
+
if (config?.optional !== true) throw createNotSignedInError();
|
|
52
|
+
return createUnauthenticatedAuthContext();
|
|
57
53
|
}
|
|
54
|
+
const extra = config?.resolve ? await config.resolve(ctx, resolved.user, resolved) : {};
|
|
58
55
|
return {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
groupId,
|
|
62
|
-
role,
|
|
63
|
-
grants
|
|
56
|
+
...resolved,
|
|
57
|
+
...extra
|
|
64
58
|
};
|
|
65
59
|
}
|
|
66
60
|
function createAuth(component, config) {
|
|
@@ -72,20 +66,32 @@ function createAuth(component, config) {
|
|
|
72
66
|
const { domain: domainApi, scim: scimApi, connection: connectionApi, audit: auditApi, webhook: webhookApi, oidc: oidcApi, saml: samlApi, ...restSso } = authResult.auth.sso;
|
|
73
67
|
const setEnterpriseDomains = async (ctx, enterpriseId, domains) => {
|
|
74
68
|
const enterprise = await connectionApi.get(ctx, enterpriseId);
|
|
75
|
-
if (enterprise === null) throw
|
|
69
|
+
if (enterprise === null) throw Cv.error({
|
|
70
|
+
code: "INVALID_PARAMETERS",
|
|
71
|
+
message: "Enterprise not found."
|
|
72
|
+
});
|
|
76
73
|
const normalized = domains.map((entry) => ({
|
|
77
74
|
...entry,
|
|
78
75
|
domain: entry.domain.trim().toLowerCase()
|
|
79
76
|
}));
|
|
80
77
|
const deduped = /* @__PURE__ */ new Map();
|
|
81
78
|
for (const entry of normalized) {
|
|
82
|
-
if (entry.domain.length === 0) throw
|
|
83
|
-
|
|
79
|
+
if (entry.domain.length === 0) throw Cv.error({
|
|
80
|
+
code: "INVALID_PARAMETERS",
|
|
81
|
+
message: "Domain must not be empty."
|
|
82
|
+
});
|
|
83
|
+
if (deduped.has(entry.domain)) throw Cv.error({
|
|
84
|
+
code: "INVALID_PARAMETERS",
|
|
85
|
+
message: `Duplicate domain: ${entry.domain}`
|
|
86
|
+
});
|
|
84
87
|
deduped.set(entry.domain, entry);
|
|
85
88
|
}
|
|
86
89
|
const nextDomains = [...deduped.values()];
|
|
87
90
|
const primaryCount = nextDomains.filter((entry) => entry.isPrimary).length;
|
|
88
|
-
if (primaryCount > 1) throw
|
|
91
|
+
if (primaryCount > 1) throw Cv.error({
|
|
92
|
+
code: "INVALID_PARAMETERS",
|
|
93
|
+
message: "Only one primary domain may be set."
|
|
94
|
+
});
|
|
89
95
|
if (nextDomains.length > 0 && primaryCount === 0) nextDomains[0] = {
|
|
90
96
|
...nextDomains[0],
|
|
91
97
|
isPrimary: true
|
|
@@ -109,7 +115,6 @@ function createAuth(component, config) {
|
|
|
109
115
|
});
|
|
110
116
|
}
|
|
111
117
|
return {
|
|
112
|
-
ok: true,
|
|
113
118
|
enterpriseId,
|
|
114
119
|
domains: (await domainApi.list(ctx, enterpriseId)).map((domain) => ({
|
|
115
120
|
domainId: domain._id,
|
|
@@ -168,38 +173,69 @@ function createAuth(component, config) {
|
|
|
168
173
|
validate: scimApi.validate
|
|
169
174
|
} },
|
|
170
175
|
http: authResult.auth.http,
|
|
171
|
-
|
|
172
|
-
ctx: () => (
|
|
173
|
-
args: {},
|
|
174
|
-
input: async (ctx) => {
|
|
175
|
-
return {
|
|
176
|
-
ctx: { auth: await resolveAuthContext(authResult.auth, ctx) },
|
|
177
|
-
args: {}
|
|
178
|
-
};
|
|
179
|
-
}
|
|
180
|
-
})
|
|
176
|
+
context: ((ctx, config$1) => createPublicAuthContext(authResult.auth, ctx, config$1)),
|
|
177
|
+
ctx: ((config$1) => createAuthContextCustomization(authResult.auth, config$1))
|
|
181
178
|
};
|
|
182
179
|
}
|
|
183
|
-
|
|
180
|
+
/**
|
|
181
|
+
* Create a context enrichment for `customQuery` / `customMutation` — optional auth.
|
|
182
|
+
*
|
|
183
|
+
* When `optional: true` is set, unauthenticated requests are allowed.
|
|
184
|
+
* The enriched `ctx.auth` will have `userId: null`, `user: null`,
|
|
185
|
+
* `groupId: null`, `role: null`, and `grants: []` for unauthenticated callers.
|
|
186
|
+
*
|
|
187
|
+
* @param config - Configuration with `optional: true` and an optional
|
|
188
|
+
* `resolve` callback for attaching extra fields to the auth context.
|
|
189
|
+
* @returns An object with `args` and `input` compatible with Convex
|
|
190
|
+
* custom function builders.
|
|
191
|
+
*
|
|
192
|
+
* @example
|
|
193
|
+
* ```ts
|
|
194
|
+
* const authCtx = auth.ctx({
|
|
195
|
+
* optional: true,
|
|
196
|
+
* resolve: async (_ctx, user) => ({ plan: user.extend?.plan ?? null }),
|
|
197
|
+
* });
|
|
198
|
+
* ```
|
|
199
|
+
*
|
|
200
|
+
* @see {@link createAuth}
|
|
201
|
+
*/
|
|
202
|
+
/**
|
|
203
|
+
* Create a context enrichment for `customQuery` / `customMutation` — required auth (default).
|
|
204
|
+
*
|
|
205
|
+
* When `optional` is omitted or `false`, unauthenticated requests throw a
|
|
206
|
+
* structured `ConvexError` before your handler runs.
|
|
207
|
+
*
|
|
208
|
+
* @param config - Optional configuration with a `resolve` callback
|
|
209
|
+
* for attaching extra fields to the auth context.
|
|
210
|
+
* @returns An object with `args` and `input` compatible with Convex
|
|
211
|
+
* custom function builders.
|
|
212
|
+
*
|
|
213
|
+
* @example
|
|
214
|
+
* ```ts
|
|
215
|
+
* const authCtx = auth.ctx({
|
|
216
|
+
* resolve: async (_ctx, user) => ({ email: user.email }),
|
|
217
|
+
* });
|
|
218
|
+
* ```
|
|
219
|
+
*
|
|
220
|
+
* @see {@link createAuth}
|
|
221
|
+
*/
|
|
222
|
+
function createAuthContextCustomization(auth, config) {
|
|
184
223
|
return {
|
|
185
224
|
args: {},
|
|
186
225
|
input: async (ctx, _args, _extra) => {
|
|
187
226
|
const nativeAuth = ctx.auth;
|
|
188
227
|
const getUserIdentity = nativeAuth.getUserIdentity.bind(nativeAuth);
|
|
189
|
-
const
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
} },
|
|
201
|
-
args: {}
|
|
202
|
-
};
|
|
228
|
+
const resolved = await resolveConfiguredAuthContext(auth, ctx, config);
|
|
229
|
+
if (resolved === null) {
|
|
230
|
+
if (config?.optional !== true) throw createNotSignedInError();
|
|
231
|
+
return {
|
|
232
|
+
ctx: { auth: {
|
|
233
|
+
getUserIdentity,
|
|
234
|
+
...createUnauthenticatedAuthContext()
|
|
235
|
+
} },
|
|
236
|
+
args: {}
|
|
237
|
+
};
|
|
238
|
+
}
|
|
203
239
|
const extra = config?.resolve ? await config.resolve(ctx, resolved.user, resolved) : {};
|
|
204
240
|
return {
|
|
205
241
|
ctx: { auth: {
|
|
@@ -214,5 +250,5 @@ function AuthCtx(auth, config) {
|
|
|
214
250
|
}
|
|
215
251
|
|
|
216
252
|
//#endregion
|
|
217
|
-
export {
|
|
253
|
+
export { createAuth };
|
|
218
254
|
//# sourceMappingURL=auth.js.map
|