@robelest/convex-auth 0.0.4-preview.21 → 0.0.4-preview.23
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/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/convex.config.d.ts +2 -2
- package/dist/component/convex.config.d.ts.map +1 -1
- package/dist/component/model.d.ts +5 -5
- 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 +39 -39
- package/dist/component/server/auth.d.ts +95 -52
- package/dist/component/server/auth.d.ts.map +1 -1
- package/dist/component/server/auth.js +63 -43
- package/dist/component/server/auth.js.map +1 -1
- package/dist/component/server/core.js +116 -235
- 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 +58 -15
- 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.js +26 -21
- 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 +3 -3
- package/dist/component/server/runtime.d.ts.map +1 -1
- package/dist/component/server/runtime.js +62 -20
- 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 +95 -52
- package/dist/server/auth.d.ts.map +1 -1
- package/dist/server/auth.js +63 -43
- package/dist/server/auth.js.map +1 -1
- package/dist/server/core.d.ts +71 -159
- package/dist/server/core.d.ts.map +1 -1
- package/dist/server/core.js +116 -235
- 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 +58 -15
- 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 +2 -2
- package/dist/server/http.d.ts.map +1 -1
- package/dist/server/http.js +25 -20
- 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 +2 -2
- package/dist/server/limits.js +21 -30
- package/dist/server/limits.js.map +1 -1
- package/dist/server/mounts.d.ts +26 -64
- package/dist/server/mounts.d.ts.map +1 -1
- package/dist/server/mounts.js +45 -106
- 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 +13 -13
- 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 +12 -10
- 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/signin.d.ts.map +1 -1
- package/dist/server/mutations/signout.js.map +1 -1
- package/dist/server/mutations/store.d.ts +97 -97
- package/dist/server/mutations/store.d.ts.map +1 -1
- 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 +10 -10
- 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 +14 -14
- package/dist/server/runtime.d.ts.map +1 -1
- package/dist/server/runtime.js +61 -19
- 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 +2 -6
- package/src/authorization/index.ts +1 -1
- package/src/cli/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 +1 -0
- 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 +177 -111
- package/src/server/core.ts +197 -233
- 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 +36 -30
- package/src/server/identity.ts +5 -5
- package/src/server/index.ts +2 -0
- package/src/server/limits.ts +53 -80
- package/src/server/mounts.ts +47 -74
- 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 +70 -55
- 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
|
@@ -10,7 +10,9 @@ import { GenericId } from "convex/values";
|
|
|
10
10
|
* minus `component` (which is passed as the first constructor argument).
|
|
11
11
|
*/
|
|
12
12
|
type AuthConfig = Omit<ConvexAuthConfig, "component">;
|
|
13
|
-
|
|
13
|
+
/** Canonical user document type exposed by Convex Auth. */
|
|
14
|
+
type UserDoc = Doc<"User">;
|
|
15
|
+
type MemberApiWithAuthorization<TAuthorization extends AuthAuthorizationConfig | undefined> = Omit<ReturnType<typeof Auth>["auth"]["member"], "create" | "list" | "update" | "inspect" | "require"> & {
|
|
14
16
|
create: (ctx: Parameters<ReturnType<typeof Auth>["auth"]["member"]["create"]>[0], data: {
|
|
15
17
|
groupId: string;
|
|
16
18
|
userId: string;
|
|
@@ -18,7 +20,6 @@ type MemberApiWithAuthorization<TAuthorization extends AuthAuthorizationConfig |
|
|
|
18
20
|
status?: string;
|
|
19
21
|
extend?: Record<string, unknown>;
|
|
20
22
|
}) => Promise<{
|
|
21
|
-
ok: true;
|
|
22
23
|
memberId: string;
|
|
23
24
|
}>;
|
|
24
25
|
list: (ctx: Parameters<ReturnType<typeof Auth>["auth"]["member"]["list"]>[0], opts?: {
|
|
@@ -36,17 +37,22 @@ type MemberApiWithAuthorization<TAuthorization extends AuthAuthorizationConfig |
|
|
|
36
37
|
update: (ctx: Parameters<ReturnType<typeof Auth>["auth"]["member"]["update"]>[0], memberId: string, data: Record<string, unknown> & {
|
|
37
38
|
roleIds?: AuthRoleId<TAuthorization>[];
|
|
38
39
|
}) => Promise<{
|
|
39
|
-
ok: true;
|
|
40
40
|
memberId: string;
|
|
41
41
|
}>;
|
|
42
|
-
|
|
42
|
+
inspect: (ctx: Parameters<ReturnType<typeof Auth>["auth"]["member"]["inspect"]>[0], opts: {
|
|
43
|
+
userId: string;
|
|
44
|
+
groupId: string;
|
|
45
|
+
ancestry?: boolean;
|
|
46
|
+
maxDepth?: number;
|
|
47
|
+
}) => ReturnType<ReturnType<typeof Auth>["auth"]["member"]["inspect"]>;
|
|
48
|
+
require: (ctx: Parameters<ReturnType<typeof Auth>["auth"]["member"]["require"]>[0], opts: {
|
|
43
49
|
userId: string;
|
|
44
50
|
groupId: string;
|
|
45
51
|
ancestry?: boolean;
|
|
46
52
|
roleIds?: AuthRoleId<TAuthorization>[];
|
|
47
53
|
grants?: AuthGrant<TAuthorization>[];
|
|
48
54
|
maxDepth?: number;
|
|
49
|
-
}) => ReturnType<ReturnType<typeof Auth>["auth"]["member"]["
|
|
55
|
+
}) => ReturnType<ReturnType<typeof Auth>["auth"]["member"]["require"]>;
|
|
50
56
|
};
|
|
51
57
|
/**
|
|
52
58
|
* The base auth API surface returned by {@link createAuth}.
|
|
@@ -77,30 +83,29 @@ type AuthApiBase<TAuthorization extends AuthAuthorizationConfig | undefined = un
|
|
|
77
83
|
key: ReturnType<typeof Auth>["auth"]["key"];
|
|
78
84
|
http: ReturnType<typeof Auth>["auth"]["http"];
|
|
79
85
|
/**
|
|
80
|
-
* Resolve the current
|
|
86
|
+
* Resolve the current request's auth context. Framework-agnostic — use
|
|
81
87
|
* this in fluent-convex middleware, custom wrappers, or anywhere you
|
|
82
|
-
* need the
|
|
88
|
+
* need the current `{ userId, user, groupId, role, grants }` object.
|
|
83
89
|
*
|
|
84
|
-
*
|
|
90
|
+
* Throws a structured `ConvexError` when unauthenticated.
|
|
85
91
|
*
|
|
86
92
|
* @param ctx - Convex query, mutation, or action context.
|
|
87
|
-
* @returns The
|
|
93
|
+
* @returns The current auth context.
|
|
88
94
|
*
|
|
89
95
|
* @example fluent-convex middleware
|
|
90
96
|
* ```ts
|
|
91
97
|
* const withAuth = convex.createMiddleware(async (ctx, next) => {
|
|
92
|
-
* return next({ ...ctx, auth: await auth.
|
|
98
|
+
* return next({ ...ctx, auth: await auth.context(ctx) });
|
|
93
99
|
* });
|
|
94
100
|
* ```
|
|
95
101
|
*
|
|
96
102
|
* @example Direct usage in a handler
|
|
97
103
|
* ```ts
|
|
98
|
-
* const
|
|
99
|
-
*
|
|
100
|
-
* const { userId, grants } = resolved;
|
|
104
|
+
* const authContext = await auth.context(ctx);
|
|
105
|
+
* const { userId, grants } = authContext;
|
|
101
106
|
* ```
|
|
102
107
|
*/
|
|
103
|
-
|
|
108
|
+
context: (ctx: any) => Promise<AuthContext>;
|
|
104
109
|
/**
|
|
105
110
|
* Context enrichment for convex-helpers `customQuery` / `customMutation` /
|
|
106
111
|
* `customAction`.
|
|
@@ -109,9 +114,9 @@ type AuthApiBase<TAuthorization extends AuthAuthorizationConfig | undefined = un
|
|
|
109
114
|
* and grants, then attaches them to `ctx.auth`. Returns a `Customization`
|
|
110
115
|
* object compatible with convex-helpers' custom function builders.
|
|
111
116
|
*
|
|
112
|
-
* `ctx.auth` is
|
|
113
|
-
*
|
|
114
|
-
*
|
|
117
|
+
* `ctx.auth` is the current request auth context.
|
|
118
|
+
* By default this throws when unauthenticated so handlers can assume
|
|
119
|
+
* `ctx.auth.userId` and `ctx.auth.user` exist.
|
|
115
120
|
*
|
|
116
121
|
* @returns A convex-helpers `Customization` object.
|
|
117
122
|
*
|
|
@@ -133,7 +138,6 @@ type AuthApiBase<TAuthorization extends AuthAuthorizationConfig | undefined = un
|
|
|
133
138
|
* export const list = authQuery({
|
|
134
139
|
* args: { workspaceId: v.string() },
|
|
135
140
|
* handler: async (ctx, args) => {
|
|
136
|
-
* if (!ctx.auth) return [];
|
|
137
141
|
* const { userId, groupId, grants } = ctx.auth;
|
|
138
142
|
* // business logic
|
|
139
143
|
* },
|
|
@@ -144,26 +148,53 @@ type AuthApiBase<TAuthorization extends AuthAuthorizationConfig | undefined = un
|
|
|
144
148
|
args: Record<string, never>;
|
|
145
149
|
input: (ctx: any) => Promise<{
|
|
146
150
|
ctx: {
|
|
147
|
-
auth:
|
|
151
|
+
auth: AuthContext;
|
|
148
152
|
};
|
|
149
153
|
args: Record<string, never>;
|
|
150
154
|
}>;
|
|
151
155
|
};
|
|
152
156
|
};
|
|
153
157
|
/**
|
|
154
|
-
*
|
|
158
|
+
* Current request auth context injected into `ctx.auth` by `auth.ctx()` and
|
|
159
|
+
* {@link AuthCtx}. This is the authenticated auth shape returned by
|
|
160
|
+
* {@link createAuth().context}. Optional context builders may still surface
|
|
161
|
+
* nullable fields when `optional: true` is used.
|
|
155
162
|
*
|
|
156
|
-
* - `null` when unauthenticated.
|
|
157
163
|
* - `groupId` is `null` when the user has no active group set.
|
|
158
|
-
* - `role`
|
|
164
|
+
* - `role` is `null` when no active group or no membership is resolved.
|
|
165
|
+
* - `grants` is `[]` when no active group or no membership is resolved.
|
|
166
|
+
*
|
|
167
|
+
* @example
|
|
168
|
+
* ```ts
|
|
169
|
+
* import type { AuthContext } from "@robelest/convex-auth/server";
|
|
170
|
+
*
|
|
171
|
+
* const mockAuth: AuthContext = {
|
|
172
|
+
* userId: "user123" as Id<"User">,
|
|
173
|
+
* user: { _id: "user123", email: "test@example.com" },
|
|
174
|
+
* groupId: "group456",
|
|
175
|
+
* role: "admin",
|
|
176
|
+
* grants: ["read", "write"],
|
|
177
|
+
* };
|
|
178
|
+
* ```
|
|
159
179
|
*/
|
|
160
|
-
type
|
|
161
|
-
/** The authenticated user's document ID. */userId:
|
|
162
|
-
user:
|
|
180
|
+
type AuthContext = {
|
|
181
|
+
/** The authenticated user's document ID. */userId: GenericId<"User">; /** The authenticated user's full document. */
|
|
182
|
+
user: UserDoc; /** The user's active group ID, or `null` if none set. */
|
|
163
183
|
groupId: string | null; /** The user's primary role in the active group, or `null`. */
|
|
164
184
|
role: string | null; /** Resolved grant strings from the user's role definitions. */
|
|
165
185
|
grants: string[];
|
|
166
186
|
};
|
|
187
|
+
type AuthCtxBase = {
|
|
188
|
+
getUserIdentity: () => Promise<UserIdentity | null>;
|
|
189
|
+
};
|
|
190
|
+
type RequiredAuthCtxState = AuthCtxBase & AuthContext;
|
|
191
|
+
type OptionalAuthCtxState = AuthCtxBase & {
|
|
192
|
+
userId: GenericId<"User"> | null;
|
|
193
|
+
user: UserDoc | null;
|
|
194
|
+
groupId: string | null;
|
|
195
|
+
role: string | null;
|
|
196
|
+
grants: string[];
|
|
197
|
+
};
|
|
167
198
|
type InternalSsoApi = ReturnType<typeof Auth>["auth"]["sso"];
|
|
168
199
|
type PublicSsoAdminApi = {
|
|
169
200
|
connection: InternalSsoApi["connection"] & {
|
|
@@ -174,7 +205,6 @@ type PublicSsoAdminApi = {
|
|
|
174
205
|
domain: string;
|
|
175
206
|
isPrimary?: boolean;
|
|
176
207
|
}>) => Promise<{
|
|
177
|
-
ok: true;
|
|
178
208
|
enterpriseId: string;
|
|
179
209
|
domains: Array<{
|
|
180
210
|
domainId: string;
|
|
@@ -189,7 +219,6 @@ type PublicSsoAdminApi = {
|
|
|
189
219
|
enterpriseId: string;
|
|
190
220
|
domain: string;
|
|
191
221
|
}) => Promise<{
|
|
192
|
-
ok: true;
|
|
193
222
|
enterpriseId: string;
|
|
194
223
|
domain: string;
|
|
195
224
|
requestedAt: number;
|
|
@@ -204,7 +233,6 @@ type PublicSsoAdminApi = {
|
|
|
204
233
|
enterpriseId: string;
|
|
205
234
|
domain: string;
|
|
206
235
|
}) => Promise<{
|
|
207
|
-
ok: boolean;
|
|
208
236
|
enterpriseId: string;
|
|
209
237
|
domain: string;
|
|
210
238
|
verifiedAt?: number;
|
|
@@ -280,8 +308,6 @@ declare function createAuth<P extends AuthProviderConfig[], TAuthorization exten
|
|
|
280
308
|
providers: P;
|
|
281
309
|
authorization?: TAuthorization;
|
|
282
310
|
}): ConvexAuthResult<P, TAuthorization>;
|
|
283
|
-
/** Canonical user document type exposed by Convex Auth. */
|
|
284
|
-
type UserDoc = Doc<"User">;
|
|
285
311
|
/**
|
|
286
312
|
* Configuration for {@link AuthCtx} context enrichment.
|
|
287
313
|
*
|
|
@@ -291,16 +317,42 @@ type UserDoc = Doc<"User">;
|
|
|
291
317
|
type AuthCtxConfig<TResolve extends Record<string, unknown> = Record<string, never>> = {
|
|
292
318
|
/** Allow unauthenticated callers and return `userId: null` / `user: null`. */optional?: boolean;
|
|
293
319
|
/**
|
|
294
|
-
* Attach additional derived fields to the auth context after the
|
|
320
|
+
* Attach additional derived fields to the auth context after the base auth
|
|
321
|
+
* context is resolved.
|
|
322
|
+
*/
|
|
323
|
+
resolve?: (ctx: any, user: UserDoc, auth: AuthContext) => Promise<TResolve> | TResolve;
|
|
324
|
+
/**
|
|
325
|
+
* Override or wrap the base auth resolution used by {@link AuthCtx}.
|
|
326
|
+
*
|
|
327
|
+
* Return `undefined` to fall back to the built-in resolver,
|
|
328
|
+
* `null` for an explicit unauthenticated state, or an
|
|
329
|
+
* {@link AuthContext} object to provide a pre-resolved auth state.
|
|
330
|
+
* This is useful for tests, proxy auth, impersonation flows, or any
|
|
331
|
+
* environment that needs to inject auth without depending on the standard
|
|
332
|
+
* Convex auth tables.
|
|
333
|
+
*
|
|
334
|
+
* @param ctx - The Convex function context.
|
|
335
|
+
* @param fallback - The built-in auth resolver used by {@link AuthCtx}.
|
|
336
|
+
* @returns Resolved auth state, `null`, or `undefined` to use the fallback.
|
|
337
|
+
*
|
|
338
|
+
* @example
|
|
339
|
+
* ```ts
|
|
340
|
+
* const authCtx = AuthCtx(auth, {
|
|
341
|
+
* authResolve: async (ctx, fallback) => {
|
|
342
|
+
* const injected = getInjectedAuth(ctx);
|
|
343
|
+
* return injected ?? (await fallback());
|
|
344
|
+
* },
|
|
345
|
+
* });
|
|
346
|
+
* ```
|
|
295
347
|
*/
|
|
296
|
-
|
|
348
|
+
authResolve?: (ctx: any, fallback: () => Promise<AuthContext | null>) => Promise<AuthContext | null | undefined> | AuthContext | null | undefined;
|
|
297
349
|
};
|
|
298
350
|
/**
|
|
299
351
|
* Create a context enrichment for `customQuery` / `customMutation` — optional auth.
|
|
300
352
|
*
|
|
301
353
|
* When `optional: true` is set, unauthenticated requests are allowed.
|
|
302
|
-
* The enriched `ctx.auth` will have `userId: null
|
|
303
|
-
* for unauthenticated callers.
|
|
354
|
+
* The enriched `ctx.auth` will have `userId: null`, `user: null`,
|
|
355
|
+
* `groupId: null`, `role: null`, and `grants: []` for unauthenticated callers.
|
|
304
356
|
*
|
|
305
357
|
* @param auth - The auth API object returned by {@link createAuth}.
|
|
306
358
|
* @param config - Configuration with `optional: true` and an optional
|
|
@@ -324,11 +376,7 @@ declare function AuthCtx<TResolve extends Record<string, unknown> = Record<strin
|
|
|
324
376
|
args: {};
|
|
325
377
|
input: (ctx: any, _args: any, _extra?: any) => Promise<{
|
|
326
378
|
ctx: {
|
|
327
|
-
auth:
|
|
328
|
-
getUserIdentity: () => Promise<UserIdentity | null>;
|
|
329
|
-
userId: GenericId<"User"> | null;
|
|
330
|
-
user: UserDoc | null;
|
|
331
|
-
} & TResolve;
|
|
379
|
+
auth: OptionalAuthCtxState & TResolve;
|
|
332
380
|
};
|
|
333
381
|
args: {};
|
|
334
382
|
}>;
|
|
@@ -336,10 +384,8 @@ declare function AuthCtx<TResolve extends Record<string, unknown> = Record<strin
|
|
|
336
384
|
/**
|
|
337
385
|
* Create a context enrichment for `customQuery` / `customMutation` — required auth (default).
|
|
338
386
|
*
|
|
339
|
-
* When `optional` is omitted or `false`,
|
|
340
|
-
*
|
|
341
|
-
* no user is signed in the returned `ctx.auth.userId` and `ctx.auth.user` are
|
|
342
|
-
* `null`.
|
|
387
|
+
* When `optional` is omitted or `false`, unauthenticated requests throw a
|
|
388
|
+
* structured `ConvexError` before your handler runs.
|
|
343
389
|
*
|
|
344
390
|
* @param auth - The auth API object returned by {@link createAuth}.
|
|
345
391
|
* @param config - Optional configuration with a `resolve` callback
|
|
@@ -360,11 +406,7 @@ declare function AuthCtx<TResolve extends Record<string, unknown> = Record<strin
|
|
|
360
406
|
args: {};
|
|
361
407
|
input: (ctx: any, _args: any, _extra?: any) => Promise<{
|
|
362
408
|
ctx: {
|
|
363
|
-
auth:
|
|
364
|
-
getUserIdentity: () => Promise<UserIdentity | null>;
|
|
365
|
-
userId: GenericId<"User">;
|
|
366
|
-
user: UserDoc;
|
|
367
|
-
} & TResolve;
|
|
409
|
+
auth: RequiredAuthCtxState & TResolve;
|
|
368
410
|
};
|
|
369
411
|
args: {};
|
|
370
412
|
}>;
|
|
@@ -374,9 +416,10 @@ declare function AuthCtx<TResolve extends Record<string, unknown> = Record<strin
|
|
|
374
416
|
*
|
|
375
417
|
* Use this to type function parameters or variables that receive the
|
|
376
418
|
* enriched auth context produced by `AuthCtx`. The inferred type includes
|
|
377
|
-
* `userId`, `user`, `getUserIdentity`, and any
|
|
378
|
-
* by the `resolve` callback. This is the generic
|
|
379
|
-
* enriched auth shape without manually duplicating
|
|
419
|
+
* `userId`, `user`, `groupId`, `role`, `grants`, `getUserIdentity`, and any
|
|
420
|
+
* additional fields added by the `resolve` callback. This is the generic
|
|
421
|
+
* utility for reusing the enriched auth shape without manually duplicating
|
|
422
|
+
* conditional auth types.
|
|
380
423
|
*
|
|
381
424
|
* @typeParam T - An `AuthCtx` return value (must have an `input` method
|
|
382
425
|
* that returns `{ ctx: { auth: ... } }`).
|
|
@@ -400,5 +443,5 @@ type InferAuth<T extends {
|
|
|
400
443
|
}>;
|
|
401
444
|
}> = Awaited<ReturnType<T["input"]>>["ctx"]["auth"];
|
|
402
445
|
//#endregion
|
|
403
|
-
export { AuthApi, AuthConfig, AuthCtx, AuthCtxConfig, InferAuth, UserDoc, createAuth };
|
|
446
|
+
export { AuthApi, AuthConfig, AuthContext, AuthCtx, AuthCtxConfig, InferAuth, UserDoc, createAuth };
|
|
404
447
|
//# 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":";;;;;;;;AAqCA;;;KAHY,UAAA,GAAa,IAAA,CAAK,gBAAA;;KAGlB,OAAA,GAAU,GAAA;AAAA,KAEjB,0BAAA,wBACoB,uBAAA,gBACrB,IAAA,CACF,UAAA,QAAkB,IAAA;EAGlB,MAAA,GACE,GAAA,EAAK,UAAA,CACH,UAAA,QAAkB,IAAA,mCAEpB,IAAA;IACE,O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A,CAAQ,UAAA,CAAW,CAAA"}
|
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
import { AuthError } from "./authError.js";
|
|
2
1
|
import { Auth } from "./runtime.js";
|
|
3
|
-
import {
|
|
2
|
+
import { Cv } from "@robelest/fx/convex";
|
|
4
3
|
|
|
5
4
|
//#region src/server/auth.ts
|
|
6
5
|
/**
|
|
6
|
+
* Auth configuration helpers for Convex Auth.
|
|
7
|
+
*
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
7
11
|
* Create an auth API object.
|
|
8
12
|
*
|
|
9
13
|
* When `new SSO()` is included in providers, `auth.sso` and `auth.scim`
|
|
@@ -37,9 +41,9 @@ import { Fx } from "@robelest/fx";
|
|
|
37
41
|
* 1. `user.id(ctx)` → userId or null (exit early)
|
|
38
42
|
* 2. `user.get(ctx, userId)` → user doc (cached per-execution)
|
|
39
43
|
* 3. `user.getActiveGroup(ctx, { userId })` → groupId or null
|
|
40
|
-
* 4. If groupId → `member.
|
|
44
|
+
* 4. If groupId → `member.inspect(ctx, { userId, groupId })` → role + grants
|
|
41
45
|
*/
|
|
42
|
-
async function
|
|
46
|
+
async function getAuthContext(auth, ctx) {
|
|
43
47
|
const userId = await auth.user.id(ctx);
|
|
44
48
|
if (!userId) return null;
|
|
45
49
|
const user = await auth.user.get(ctx, userId);
|
|
@@ -47,7 +51,7 @@ async function resolveAuthContext(auth, ctx) {
|
|
|
47
51
|
let role = null;
|
|
48
52
|
let grants = [];
|
|
49
53
|
if (groupId) {
|
|
50
|
-
const resolved = await auth.member.
|
|
54
|
+
const resolved = await auth.member.inspect(ctx, {
|
|
51
55
|
userId,
|
|
52
56
|
groupId
|
|
53
57
|
});
|
|
@@ -73,20 +77,32 @@ function createAuth(component, config) {
|
|
|
73
77
|
const { domain: domainApi, scim: scimApi, connection: connectionApi, audit: auditApi, webhook: webhookApi, oidc: oidcApi, saml: samlApi, ...restSso } = authResult.auth.sso;
|
|
74
78
|
const setEnterpriseDomains = async (ctx, enterpriseId, domains) => {
|
|
75
79
|
const enterprise = await connectionApi.get(ctx, enterpriseId);
|
|
76
|
-
if (enterprise === null) throw
|
|
80
|
+
if (enterprise === null) throw Cv.error({
|
|
81
|
+
code: "INVALID_PARAMETERS",
|
|
82
|
+
message: "Enterprise not found."
|
|
83
|
+
});
|
|
77
84
|
const normalized = domains.map((entry) => ({
|
|
78
85
|
...entry,
|
|
79
86
|
domain: entry.domain.trim().toLowerCase()
|
|
80
87
|
}));
|
|
81
88
|
const deduped = /* @__PURE__ */ new Map();
|
|
82
89
|
for (const entry of normalized) {
|
|
83
|
-
if (entry.domain.length === 0) throw
|
|
84
|
-
|
|
90
|
+
if (entry.domain.length === 0) throw Cv.error({
|
|
91
|
+
code: "INVALID_PARAMETERS",
|
|
92
|
+
message: "Domain must not be empty."
|
|
93
|
+
});
|
|
94
|
+
if (deduped.has(entry.domain)) throw Cv.error({
|
|
95
|
+
code: "INVALID_PARAMETERS",
|
|
96
|
+
message: `Duplicate domain: ${entry.domain}`
|
|
97
|
+
});
|
|
85
98
|
deduped.set(entry.domain, entry);
|
|
86
99
|
}
|
|
87
100
|
const nextDomains = [...deduped.values()];
|
|
88
101
|
const primaryCount = nextDomains.filter((entry) => entry.isPrimary).length;
|
|
89
|
-
if (primaryCount > 1) throw
|
|
102
|
+
if (primaryCount > 1) throw Cv.error({
|
|
103
|
+
code: "INVALID_PARAMETERS",
|
|
104
|
+
message: "Only one primary domain may be set."
|
|
105
|
+
});
|
|
90
106
|
if (nextDomains.length > 0 && primaryCount === 0) nextDomains[0] = {
|
|
91
107
|
...nextDomains[0],
|
|
92
108
|
isPrimary: true
|
|
@@ -110,7 +126,6 @@ function createAuth(component, config) {
|
|
|
110
126
|
});
|
|
111
127
|
}
|
|
112
128
|
return {
|
|
113
|
-
ok: true,
|
|
114
129
|
enterpriseId,
|
|
115
130
|
domains: (await domainApi.list(ctx, enterpriseId)).map((domain) => ({
|
|
116
131
|
domainId: domain._id,
|
|
@@ -169,12 +184,24 @@ function createAuth(component, config) {
|
|
|
169
184
|
validate: scimApi.validate
|
|
170
185
|
} },
|
|
171
186
|
http: authResult.auth.http,
|
|
172
|
-
|
|
187
|
+
context: async (ctx) => {
|
|
188
|
+
const authContext = await getAuthContext(authResult.auth, ctx);
|
|
189
|
+
if (authContext === null) throw Cv.error({
|
|
190
|
+
code: "NOT_SIGNED_IN",
|
|
191
|
+
message: "Authentication required."
|
|
192
|
+
});
|
|
193
|
+
return authContext;
|
|
194
|
+
},
|
|
173
195
|
ctx: () => ({
|
|
174
196
|
args: {},
|
|
175
197
|
input: async (ctx) => {
|
|
198
|
+
const authCtx = await getAuthContext(authResult.auth, ctx);
|
|
199
|
+
if (authCtx === null) throw Cv.error({
|
|
200
|
+
code: "NOT_SIGNED_IN",
|
|
201
|
+
message: "Authentication required."
|
|
202
|
+
});
|
|
176
203
|
return {
|
|
177
|
-
ctx: { auth:
|
|
204
|
+
ctx: { auth: authCtx },
|
|
178
205
|
args: {}
|
|
179
206
|
};
|
|
180
207
|
}
|
|
@@ -186,39 +213,32 @@ function AuthCtx(auth, config) {
|
|
|
186
213
|
args: {},
|
|
187
214
|
input: async (ctx, _args, _extra) => {
|
|
188
215
|
const nativeAuth = ctx.auth;
|
|
189
|
-
const
|
|
190
|
-
const
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
userId: null,
|
|
212
|
-
user: null
|
|
213
|
-
} },
|
|
214
|
-
args: {}
|
|
215
|
-
};
|
|
216
|
-
const extra = config?.resolve ? await config.resolve(ctx, userContext.user) : {};
|
|
216
|
+
const getUserIdentity = nativeAuth.getUserIdentity.bind(nativeAuth);
|
|
217
|
+
const fallback = () => getAuthContext(auth, ctx);
|
|
218
|
+
const authOverride = config?.authResolve ? await config.authResolve(ctx, fallback) : void 0;
|
|
219
|
+
const resolved = authOverride === void 0 ? await fallback() : authOverride;
|
|
220
|
+
if (resolved === null) {
|
|
221
|
+
if (config?.optional !== true) throw Cv.error({
|
|
222
|
+
code: "NOT_SIGNED_IN",
|
|
223
|
+
message: "Authentication required."
|
|
224
|
+
});
|
|
225
|
+
return {
|
|
226
|
+
ctx: { auth: {
|
|
227
|
+
getUserIdentity,
|
|
228
|
+
userId: null,
|
|
229
|
+
user: null,
|
|
230
|
+
groupId: null,
|
|
231
|
+
role: null,
|
|
232
|
+
grants: []
|
|
233
|
+
} },
|
|
234
|
+
args: {}
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
const extra = config?.resolve ? await config.resolve(ctx, resolved.user, resolved) : {};
|
|
217
238
|
return {
|
|
218
239
|
ctx: { auth: {
|
|
219
|
-
getUserIdentity
|
|
220
|
-
|
|
221
|
-
user: userContext.user,
|
|
240
|
+
getUserIdentity,
|
|
241
|
+
...resolved,
|
|
222
242
|
...extra
|
|
223
243
|
} },
|
|
224
244
|
args: {}
|