@softr/sdk 1.4.2 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # @softr/sdk
2
2
 
3
+ ## 1.5.0
4
+
5
+ ### Minor Changes
6
+
7
+ - `auth.sendSignInCode` fails as `noAccount` when no account has the email, and as `accountDeactivated` for a deactivated account, instead of succeeding without sending anything. Action: on the login page, send the code from the email step and say so there, and point the visitor to sign-up on `noAccount` where the app has it. Open the code inputs only once the code is sent.
8
+ - `auth.signInMethod({ email })` says whether an email has an account, and which ways in work for it. Action: on a login page that asks for the email first, go to the step for the first of `methods`, or say there is no account, instead of always asking for a password. On a `password` method with `passwordSet` false, the account has no password yet: say so and link to the password reset.
9
+ - Codes Softr emails, for sign-in, sign-up and a second factor, are six digits. Action: give code fields `inputMode="numeric"` and `autoComplete="one-time-code"`, and accept digits only.
10
+
11
+ ## 1.4.3
12
+
13
+ ### Patch Changes
14
+
15
+ - The MCP consent page shows where the client document lives and the host the browser goes to with the code, so a user can tell a real client from one with a look-alike name.
16
+
3
17
  ## 1.4.2
4
18
 
5
19
  ### Patch Changes
@@ -7,9 +7,8 @@ import { AuthError, Result } from "./utils.js";
7
7
  * Emails a one-time sign-in code, for an app that signs people in with a code
8
8
  * rather than a password.
9
9
  *
10
- * It succeeds whether or not the address has an account, so nobody can use the
11
- * form it backs to find out who is registered. Say "check your email" either
12
- * way.
10
+ * An address with no account fails as `noAccount`, and a deactivated one as
11
+ * `accountDeactivated`, so the visitor is not left waiting for an email.
13
12
  */
14
13
  declare function sendSignInCode(details: {
15
14
  email: string;
@@ -6,19 +6,19 @@ import { clearPendingAuth, makePendingAuthCookie, makeSessionDataCookie, makeSes
6
6
  import { authError } from "./utils.js";
7
7
 
8
8
  //#region src/auth/methods/code.ts
9
+ const SEND_CODE_ERRORS = new Map([["No account exists for this email.", "noAccount"], ["This account is deactivated. Please contact the application owner.", "accountDeactivated"]]);
9
10
  /**
10
11
  * Emails a one-time sign-in code, for an app that signs people in with a code
11
12
  * rather than a password.
12
13
  *
13
- * It succeeds whether or not the address has an account, so nobody can use the
14
- * form it backs to find out who is registered. Say "check your email" either
15
- * way.
14
+ * An address with no account fails as `noAccount`, and a deactivated one as
15
+ * `accountDeactivated`, so the visitor is not left waiting for an email.
16
16
  */
17
17
  async function sendSignInCode(details) {
18
18
  const result = await makePlatform(sessionConfig().applicationId).sendSignInCode(details.email);
19
19
  if (!result.ok) {
20
20
  if (result.status === 429) return authError("tooManyAttempts", result.message);
21
- if (result.status === 400) return authError("invalidDetails", result.message);
21
+ if (result.status === 400) return authError(SEND_CODE_ERRORS.get(result.message) ?? "invalidDetails", result.message);
22
22
  if (result.status >= 500) panic(`Expected the sign-in code to be emailed, but got ${result.status}: ` + result.message);
23
23
  throw new Error(`${result.message}. Check SOFTR_APPLICATION_ID and that the app signs people in with an emailed code.`);
24
24
  }
@@ -0,0 +1,27 @@
1
+ import { AuthError, Result } from "./utils.js";
2
+
3
+ //#region src/auth/methods/signInMethod.d.ts
4
+ type SignInMethod = "password" | "emailCode" | "google" | "sso";
5
+ /**
6
+ * How a visitor with this email signs in, for a login page that asks for the
7
+ * email first and routes from there.
8
+ *
9
+ * `account` is `none` when the email has no account, and `deactivated` for a
10
+ * deactivated one. For an `active` account, `methods` lists what the app
11
+ * offers and also works for that account, in the order to prefer them.
12
+ * `passwordSet` is false when the account has no password yet: `password` is
13
+ * then listed after the methods that work right away, and the visitor sets
14
+ * one through the password reset first. `methods` can be empty when nothing
15
+ * the app offers works for the account.
16
+ *
17
+ * It tells anyone who asks whether an email has an account.
18
+ */
19
+ declare function signInMethod(details: {
20
+ email: string;
21
+ }): Promise<Result<{
22
+ account: "none" | "deactivated" | "active";
23
+ methods: SignInMethod[];
24
+ passwordSet: boolean;
25
+ }, AuthError>>;
26
+ //#endregion
27
+ export { signInMethod };
@@ -0,0 +1,51 @@
1
+ import { panic } from "../../assertions.js";
2
+ import { sessionConfig } from "../config.js";
3
+ import { makePlatform } from "../platform.js";
4
+ import { authError } from "./utils.js";
5
+
6
+ //#region src/auth/methods/signInMethod.ts
7
+ const METHODS = {
8
+ PASSWORD: "password",
9
+ EMAIL_CODE: "emailCode",
10
+ GOOGLE: "google",
11
+ SSO: "sso"
12
+ };
13
+ const ACCOUNTS = {
14
+ NONE: "none",
15
+ DEACTIVATED: "deactivated",
16
+ ACTIVE: "active"
17
+ };
18
+ /**
19
+ * How a visitor with this email signs in, for a login page that asks for the
20
+ * email first and routes from there.
21
+ *
22
+ * `account` is `none` when the email has no account, and `deactivated` for a
23
+ * deactivated one. For an `active` account, `methods` lists what the app
24
+ * offers and also works for that account, in the order to prefer them.
25
+ * `passwordSet` is false when the account has no password yet: `password` is
26
+ * then listed after the methods that work right away, and the visitor sets
27
+ * one through the password reset first. `methods` can be empty when nothing
28
+ * the app offers works for the account.
29
+ *
30
+ * It tells anyone who asks whether an email has an account.
31
+ */
32
+ async function signInMethod(details) {
33
+ const result = await makePlatform(sessionConfig().applicationId).signInMethod(details);
34
+ if (!result.ok) {
35
+ if (result.status === 429) return authError("tooManyAttempts", result.message);
36
+ if (result.status === 400) return authError("invalidDetails", result.message);
37
+ if (result.status >= 500) panic(`Expected to learn how that email signs in, but got ${result.status}: ` + result.message);
38
+ throw new Error(`${result.message}. Check SOFTR_APPLICATION_ID.`);
39
+ }
40
+ return {
41
+ success: true,
42
+ data: {
43
+ account: ACCOUNTS[result.data.account],
44
+ methods: result.data.methods.flatMap((method) => Object.hasOwn(METHODS, method) ? [METHODS[method]] : []),
45
+ passwordSet: result.data.passwordSet
46
+ }
47
+ };
48
+ }
49
+
50
+ //#endregion
51
+ export { signInMethod };
@@ -10,6 +10,6 @@ type AuthError = {
10
10
  code: AuthErrorCode;
11
11
  message: string;
12
12
  };
13
- type AuthErrorCode = "invalidCredentials" | "tooManyAttempts" | "emailTaken" | "invalidDetails" | "invalidCode" | "notAuthenticated" | "emailDomainNotAllowed" | "linkExpired" | "invalidName" | "fileTooLarge" | "notAnImage" | "avatarNotSupported";
13
+ type AuthErrorCode = "invalidCredentials" | "tooManyAttempts" | "emailTaken" | "invalidDetails" | "invalidCode" | "noAccount" | "accountDeactivated" | "notAuthenticated" | "emailDomainNotAllowed" | "linkExpired" | "invalidName" | "fileTooLarge" | "notAnImage" | "avatarNotSupported";
14
14
  //#endregion
15
15
  export { AuthError, Result };
@@ -128,6 +128,18 @@ function makePlatform(applicationId) {
128
128
  method: "POST",
129
129
  json: { email }
130
130
  }, z.null()),
131
+ signInMethod: (body) => request(`/v1/vibe/applications/${applicationId}/users/sign-in-method`, {
132
+ method: "POST",
133
+ json: body
134
+ }, z.object({
135
+ account: z.enum([
136
+ "NONE",
137
+ "DEACTIVATED",
138
+ "ACTIVE"
139
+ ]),
140
+ methods: z.array(z.string()),
141
+ passwordSet: z.boolean()
142
+ })),
131
143
  signInWithCode: (credentials) => request(`/v1/vibe/applications/${applicationId}/users/tokens/with-code${withAttributes}`, {
132
144
  method: "POST",
133
145
  json: credentials
@@ -5,6 +5,7 @@ import { signInWithMagicLink } from "./methods/magicLink.js";
5
5
  import { signIn, signOut } from "./methods/password.js";
6
6
  import { checkPasswordLink, requestPasswordReset, setPasswordWithLink } from "./methods/passwordLink.js";
7
7
  import { settings } from "./methods/settings.js";
8
+ import { signInMethod } from "./methods/signInMethod.js";
8
9
  import { discoverSso } from "./methods/sso.js";
9
10
  import { confirmSignUp, signUp } from "./methods/signup.js";
10
11
  import { signInWithGoogle } from "./methods/social.js";
@@ -22,6 +23,7 @@ declare const auth: {
22
23
  signOut: typeof signOut;
23
24
  signUp: typeof signUp;
24
25
  confirmSignUp: typeof confirmSignUp;
26
+ signInMethod: typeof signInMethod;
25
27
  sendSignInCode: typeof sendSignInCode;
26
28
  signInWithCode: typeof signInWithCode;
27
29
  signInWithGoogle: typeof signInWithGoogle;
@@ -5,6 +5,7 @@ import { resendEmailCode, verifyEmailCode } from "./methods/emailCode2fa.js";
5
5
  import { signInWithMagicLink } from "./methods/magicLink.js";
6
6
  import { signIn, signOut } from "./methods/password.js";
7
7
  import { checkPasswordLink, requestPasswordReset, setPasswordWithLink } from "./methods/passwordLink.js";
8
+ import { signInMethod } from "./methods/signInMethod.js";
8
9
  import { discoverSso } from "./methods/sso.js";
9
10
  import { confirmSignUp, signUp } from "./methods/signup.js";
10
11
  import { signInWithGoogle } from "./methods/social.js";
@@ -21,6 +22,7 @@ const auth = {
21
22
  signOut,
22
23
  signUp,
23
24
  confirmSignUp,
25
+ signInMethod,
24
26
  sendSignInCode,
25
27
  signInWithCode,
26
28
  signInWithGoogle,
package/dist/mcp.js CHANGED
@@ -103,7 +103,7 @@ async function authorize(request, login) {
103
103
  if (user === null || sessionToken === null) return html(200, signInPage(url, login));
104
104
  const client = await platformConsent(query.data);
105
105
  if (!client.ok) return html(400, page("We can't connect this right now", escapeHtml(client.message)));
106
- if (request.method === "GET") return html(200, consentPage(user, url, client.name));
106
+ if (request.method === "GET") return html(200, consentPage(user, url, client.name, query.data));
107
107
  const form = await request.formData();
108
108
  const decision = await platformApprove(sessionToken, query.data, form.get("decision") === "allow");
109
109
  if (!decision.ok) return html(502, page("We could not finish this", decision.message));
@@ -534,8 +534,10 @@ function html(status, body) {
534
534
  }
535
535
  });
536
536
  }
537
- function consentPage(user, url, clientName) {
538
- return page("Connect to this app?", `<strong>${escapeHtml(clientName)}</strong> is asking to use this app as <strong>${escapeHtml(user.fullName || user.email)}</strong>. It will see only what you can see.`, `<form method="post" action="${escapeHtml(url.pathname + url.search)}">
537
+ function consentPage(user, url, clientName, query) {
538
+ const clientHost = query.client_id.startsWith("https://") ? `Client registered at ${escapeHtml(new URL(query.client_id).host)}. ` : "";
539
+ const redirectHost = `It will send you back to ${escapeHtml(new URL(query.redirect_uri).host)}.`;
540
+ return page("Connect to this app?", `<strong>${escapeHtml(clientName)}</strong> is asking to use this app as <strong>${escapeHtml(user.fullName || user.email)}</strong>. It will see only what you can see.<br><small>${clientHost}${redirectHost}</small>`, `<form method="post" action="${escapeHtml(url.pathname + url.search)}">
539
541
  <button type="submit" name="decision" value="allow" class="primary">Allow</button>
540
542
  <button type="submit" name="decision" value="deny">Cancel</button>
541
543
  </form>`);
package/dist/package.js CHANGED
@@ -1,5 +1,5 @@
1
1
  //#region package.json
2
- var version = "1.4.2";
2
+ var version = "1.5.0";
3
3
 
4
4
  //#endregion
5
5
  export { version };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@softr/sdk",
3
- "version": "1.4.2",
3
+ "version": "1.5.0",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": { "types": "./dist/index.d.ts", "import": "./dist/index.js" },
@@ -10,7 +10,7 @@ description: >-
10
10
  metadata:
11
11
  type: core
12
12
  library: softr-sdk
13
- library_version: 1.4.2
13
+ library_version: 1.5.0
14
14
  ---
15
15
 
16
16
  # Softr SDK core
@@ -13,7 +13,7 @@ description: >-
13
13
  metadata:
14
14
  type: sub-skill
15
15
  library: softr-sdk
16
- library_version: 1.4.2
16
+ library_version: 1.5.0
17
17
  ---
18
18
 
19
19
  **Boundary.** `ai` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler — importing it into a component fails the build. `defineAiAction` and every type come from `@softr/sdk` and can be imported anywhere.
@@ -10,7 +10,7 @@ description: >-
10
10
  metadata:
11
11
  type: sub-skill
12
12
  library: softr-sdk
13
- library_version: 1.4.2
13
+ library_version: 1.5.0
14
14
  ---
15
15
 
16
16
  **Boundary.** `auth` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler — importing it into a component fails the build.
@@ -30,6 +30,7 @@ Signing in:
30
30
  | Call | Does |
31
31
  | --- | --- |
32
32
  | `auth.signIn({ email, password })` | Signs a visitor in with an email and password |
33
+ | `auth.signInMethod({ email })` | Says whether an email has an account, and which ways in work for it |
33
34
  | `auth.sendSignInCode({ email })` | Emails a one-time sign-in code |
34
35
  | `auth.signInWithCode({ email, code })` | Signs a visitor in with that code |
35
36
  | `auth.signInWithGoogle({ email, token, fullName? })` | Signs a visitor in with a Google id token the browser obtained, creating the account the first time |
@@ -129,6 +130,7 @@ type AuthErrorCode =
129
130
  | 'emailTaken' // signUp, confirmSignUp: that email already has an account
130
131
  | 'invalidDetails' // signUp, sendSignInCode, setPasswordWithLink: details Softr rejected
131
132
  | 'invalidCode' // any call that takes a code: wrong or expired
133
+ | 'noAccount' | 'accountDeactivated' // sendSignInCode: no account has that email, or it is deactivated
132
134
  | 'notAuthenticated' // a call that needs a signed-in visitor, made by one who is not
133
135
  | 'emailDomainNotAllowed' // signUp, signInWithGoogle: the app lets only some email domains sign up, and this is not one
134
136
  | 'linkExpired' // checkPasswordLink, setPasswordWithLink: the link is used, expired or no longer valid
@@ -271,11 +273,9 @@ if (answer.outcome.status === 'emailCodeRequired') {
271
273
  await navigate({ href: target })
272
274
  ```
273
275
 
274
- Four exits: a message for the form to render, the emailed-code step, the authenticator step, the return target.
276
+ Four exits: a message for the form to render, the emailed-code step, the authenticator step, the return target. A server function that throws `redirect` needs the hook. Call the imported function directly and the throw never reaches the router, so a successful sign-in leaves the visitor on the form.
275
277
 
276
- A server function that throws `redirect` needs the hook. Call the imported function directly and the throw never reaches the router, so a successful sign-in leaves the visitor on the form.
277
-
278
- A login page is a `NON_LOGGED_IN_USERS` route, which is what keeps an already-signed-in visitor off it. See [routes-and-visibility](../routes-and-visibility/SKILL.md).
278
+ A login page is a `NON_LOGGED_IN_USERS` route, which is what keeps an already-signed-in visitor off it. See [routes-and-visibility](../routes-and-visibility/SKILL.md). A login page that asks for the email first passes it to `auth.signInMethod` and goes to the step for the first of `methods`. On an `account` of `none` or `deactivated`, or no `methods`, it says so instead of asking for a password. When it reaches `password` and `passwordSet` is false, it says the account has no password yet and links to the password reset, where the visitor sets one, instead of asking for a password.
279
279
 
280
280
  ### Sign in with an emailed code
281
281
 
@@ -284,12 +284,11 @@ For an app that signs people in with a code instead of a password.
284
284
  ```ts
285
285
  // Step 1: emails the code.
286
286
  const sent = await auth.sendSignInCode({ email })
287
-
288
287
  // Step 2: same two-armed result as signIn.
289
288
  const result = await auth.signInWithCode({ email, code })
290
289
  ```
291
290
 
292
- Step one succeeds whether or not that address has an account, so the form cannot be used to find out who is registered. Say "check your email" either way, and never branch the UI on it.
291
+ Every code Softr emails, for sign-in, sign-up or a second factor, is six digits. Give the code field `inputMode="numeric"` and `autoComplete="one-time-code"`, and accept digits only. Step one fails as `noAccount` when no account has that email, and as `accountDeactivated` for a deactivated one. Say so, so the visitor is not left waiting for an email that never comes. Send it from the email step and open the code inputs only once it succeeds, so these errors land under the email field, not under inputs for a code that was never sent. Where the app has sign-up, point them there on `noAccount`. When the builder asks not to reveal who has an account, the server function treats every failure except `tooManyAttempts` as success and returns exactly what a real send returns. That covers an SSO account too, which fails as `invalidDetails` while the app offers SSO. The page then says "if an account exists for this email, we sent it a code", and never calls `auth.signInMethod`. Changing only the wording is not enough: the server function's response is visible in the browser.
293
292
 
294
293
  ### Sign in with Google
295
294
 
@@ -10,7 +10,7 @@ description: >-
10
10
  metadata:
11
11
  type: sub-skill
12
12
  library: softr-sdk
13
- library_version: 1.4.2
13
+ library_version: 1.5.0
14
14
  ---
15
15
 
16
16
  **Boundary.** `ds` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler or a server-side route hook — importing it into a component fails the build. `defineQuery` and every type come from `@softr/sdk` and can be imported anywhere.
@@ -11,7 +11,7 @@ description: >-
11
11
  metadata:
12
12
  type: sub-skill
13
13
  library: softr-sdk
14
- library_version: 1.4.2
14
+ library_version: 1.5.0
15
15
  ---
16
16
 
17
17
  # The current user
@@ -7,7 +7,7 @@ description: >-
7
7
  metadata:
8
8
  type: sub-skill
9
9
  library: softr-sdk
10
- library_version: 1.4.2
10
+ library_version: 1.5.0
11
11
  ---
12
12
 
13
13
  **Boundary.** `upload` lives on `@softr/sdk/server` and runs inside server functions. `useFileUpload` and `FileItem` live on `@softr/sdk` and run in components. `FileAttachment` comes from `@softr/sdk` and can be imported anywhere.
@@ -8,7 +8,7 @@ description: >-
8
8
  metadata:
9
9
  type: sub-skill
10
10
  library: softr-sdk
11
- library_version: 1.4.2
11
+ library_version: 1.5.0
12
12
  ---
13
13
 
14
14
  # Dates, times and numbers
@@ -7,7 +7,7 @@ description: >-
7
7
  metadata:
8
8
  type: sub-skill
9
9
  library: softr-sdk
10
- library_version: 1.4.2
10
+ library_version: 1.5.0
11
11
  ---
12
12
 
13
13
  **Boundary.** `integration` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler — importing it into a component fails the build. `defineIntegration` and every type come from `@softr/sdk` and can be imported anywhere.
@@ -9,7 +9,7 @@ description: >-
9
9
  metadata:
10
10
  type: sub-skill
11
11
  library: softr-sdk
12
- library_version: 1.4.2
12
+ library_version: 1.5.0
13
13
  ---
14
14
 
15
15
  **Boundary.** `softrMcpMiddleware` lives on `@softr/sdk` and is registered once, in `src/start.ts`. The actions you hand it are the same `defineQuery` / `defineMutation` values the app's own pages use — declared wherever they already live, not duplicated for MCP.
@@ -9,7 +9,7 @@ description: >-
9
9
  metadata:
10
10
  type: sub-skill
11
11
  library: softr-sdk
12
- library_version: 1.4.2
12
+ library_version: 1.5.0
13
13
  ---
14
14
 
15
15
  # Push notifications
@@ -9,7 +9,7 @@ description: >-
9
9
  metadata:
10
10
  type: sub-skill
11
11
  library: softr-sdk
12
- library_version: 1.4.2
12
+ library_version: 1.5.0
13
13
  ---
14
14
 
15
15
  # Installing the app
@@ -10,7 +10,7 @@ description: >-
10
10
  metadata:
11
11
  type: sub-skill
12
12
  library: softr-sdk
13
- library_version: 1.4.2
13
+ library_version: 1.5.0
14
14
  ---
15
15
 
16
16
  **Boundary.** `ds` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler — importing it into a component fails the build. `defineMutation` and every type come from `@softr/sdk` and can be imported anywhere.
@@ -11,7 +11,7 @@ description: >-
11
11
  metadata:
12
12
  type: sub-skill
13
13
  library: softr-sdk
14
- library_version: 1.4.2
14
+ library_version: 1.5.0
15
15
  ---
16
16
 
17
17
  **Boundary.** `ds` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler or a server-side route hook — importing it into a component fails the build. The same holds for any helper that calls `ds`: declare it inside the handler. A function at module level that calls `ds` keeps the server-only import in the browser bundle, and every page stops loading until it moves. `defineQuery` and every type come from `@softr/sdk` and can be imported anywhere.
@@ -9,7 +9,7 @@ description: >-
9
9
  metadata:
10
10
  type: sub-skill
11
11
  library: softr-sdk
12
- library_version: 1.4.2
12
+ library_version: 1.5.0
13
13
  ---
14
14
 
15
15
  **Boundary.** `Audience` and `defineVisibility` come from `@softr/sdk` and work anywhere.
@@ -9,7 +9,7 @@ description: >-
9
9
  metadata:
10
10
  type: sub-skill
11
11
  library: softr-sdk
12
- library_version: 1.4.2
12
+ library_version: 1.5.0
13
13
  ---
14
14
 
15
15
  **Boundary.** `definePage`, `pageHead`, the access functions and the error predicates come from `@softr/sdk` and work anywhere.
@@ -9,7 +9,7 @@ description: >-
9
9
  metadata:
10
10
  type: sub-skill
11
11
  library: softr-sdk
12
- library_version: 1.4.2
12
+ library_version: 1.5.0
13
13
  ---
14
14
 
15
15
  **Boundary.** `workflow` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler — importing it into a component fails the build. `defineWorkflow` and every type come from `@softr/sdk` and can be imported anywhere.