@softr/sdk
Version:
The SDK for building Softr apps on TanStack Start. It lets an app interact with Softr: auth, datasources, workflows and integrations.
77 lines (75 loc) • 3.63 kB
JavaScript
import { assert, panic } from "../../assertions.js";
import { writeCookies } from "../../cookies.js";
import { sessionConfig } from "../config.js";
import { makePlatform } from "../platform.js";
import { assertCanChange, clearPendingAuth, makePendingAuthCookie, makeSessionDataCookie, makeSessionTokenCookie } from "../session.js";
import { authError, isAuthenticatorNotInPlan, isNotInPlan, isUserLimitReached } from "./utils.js";
//#region src/auth/methods/code.ts
const SEND_CODE_ERRORS = new Map([["No account exists for this email.", "noAccount"], ["This account is deactivated. Please contact the application owner.", "accountDeactivated"]]);
/**
* Emails a one-time sign-in code, for an app that signs people in with a code
* rather than a password.
*
* An address with no account fails as `noAccount`, and a deactivated one as
* `accountDeactivated`, so the visitor is not left waiting for an email.
*/
async function sendSignInCode(details) {
assertCanChange("auth.sendSignInCode");
const result = await makePlatform(sessionConfig().applicationId).sendSignInCode(details.email);
if (!result.ok) {
if (isNotInPlan(result)) return authError("notEnabled", result.message);
if (result.status === 429) return authError("tooManyAttempts", result.message);
if (result.status === 400) return authError(SEND_CODE_ERRORS.get(result.message) ?? "invalidDetails", result.message);
if (result.status >= 500) panic(`Expected the sign-in code to be emailed, but got ${result.status}: ` + result.message);
throw new Error(`${result.message}. Check SOFTR_APPLICATION_ID and that the app signs people in with an emailed code.`);
}
return {
success: true,
data: null
};
}
/**
* Signs a visitor in with the code {@link sendSignInCode} emailed them.
*
* A `signedIn` result means the visitor is signed in. An app that also asks for
* an authenticator app answers `authenticatorRequired` instead, naming the step
* to send them to next.
*/
async function signInWithCode(credentials) {
assertCanChange("auth.signInWithCode");
const result = await makePlatform(sessionConfig().applicationId).signInWithCode(credentials);
if (!result.ok) {
if (isUserLimitReached(result)) return authError("userLimitReached", result.message);
if (isAuthenticatorNotInPlan(result)) return authError("secondFactorNotEnabled", result.message);
if (isNotInPlan(result)) return authError("notEnabled", result.message);
if (result.status === 429) return authError("tooManyAttempts", result.message);
if (result.status === 400 || result.status === 401) return authError("invalidCode", result.message);
if (result.status >= 500) panic(`Expected sign-in to succeed, but got ${result.status}: ` + result.message);
throw new Error(`${result.message}. Check SOFTR_APPLICATION_ID and that the app signs people in with an emailed code.`);
}
if (result.data.status === "MFA_REQUIRED") {
const { mfa } = result.data;
assert(mfa, "Expected a challenge from sign-in, but got none");
writeCookies([makePendingAuthCookie(mfa.challengeToken)]);
return {
success: true,
data: {
status: "authenticatorRequired",
step: mfa.action === "VERIFICATION_REQUIRED" ? "verify" : mfa.action === "SETUP_REQUIRED" ? "setup" : "setupOptional"
}
};
}
const { session } = result.data;
assert(session, "Expected a session from sign-in, but got none");
writeCookies([...makeSessionTokenCookie(session.jwt), makeSessionDataCookie({ user: session.user }, session.jwt)]);
clearPendingAuth();
return {
success: true,
data: {
status: "signedIn",
user: session.user
}
};
}
//#endregion
export { sendSignInCode, signInWithCode };