@softr/sdk
Version:
The SDK for building Softr apps on TanStack Start. It lets an app interact with Softr: auth, datasources, workflows and integrations.
51 lines (49 loc) • 1.87 kB
JavaScript
import { panic } from "../../assertions.js";
import { sessionConfig } from "../config.js";
import { makePlatform } from "../platform.js";
import { authError } from "./utils.js";
//#region src/auth/methods/signInMethod.ts
const METHODS = {
PASSWORD: "password",
EMAIL_CODE: "emailCode",
GOOGLE: "google",
SSO: "sso"
};
const ACCOUNTS = {
NONE: "none",
DEACTIVATED: "deactivated",
ACTIVE: "active"
};
/**
* How a visitor with this email signs in, for a login page that asks for the
* email first and routes from there.
*
* `account` is `none` when the email has no account, and `deactivated` for a
* deactivated one. For an `active` account, `methods` lists what the app
* offers and also works for that account, in the order to prefer them.
* `passwordSet` is false when the account has no password yet: `password` is
* then listed after the methods that work right away, and the visitor sets
* one through the password reset first. `methods` can be empty when nothing
* the app offers works for the account.
*
* It tells anyone who asks whether an email has an account.
*/
async function signInMethod(details) {
const result = await makePlatform(sessionConfig().applicationId).signInMethod(details);
if (!result.ok) {
if (result.status === 429) return authError("tooManyAttempts", result.message);
if (result.status === 400) return authError("invalidDetails", result.message);
if (result.status >= 500) panic(`Expected to learn how that email signs in, but got ${result.status}: ` + result.message);
throw new Error(`${result.message}. Check SOFTR_APPLICATION_ID.`);
}
return {
success: true,
data: {
account: ACCOUNTS[result.data.account],
methods: result.data.methods.flatMap((method) => Object.hasOwn(METHODS, method) ? [METHODS[method]] : []),
passwordSet: result.data.passwordSet
}
};
}
//#endregion
export { signInMethod };