@softr/sdk
Version:
The SDK for building Softr apps on TanStack Start. It lets an app interact with Softr: auth, datasources, workflows and integrations.
71 lines (69 loc) • 2.93 kB
JavaScript
import { assert, panic } from "../../assertions.js";
import { writeCookies } from "../../cookies.js";
import { sessionConfig } from "../config.js";
import { makePlatform } from "../platform.js";
import { clearPendingAuth, makePendingAuthCookie, makeSessionDataCookie, makeSessionTokenCookie } from "../session.js";
import { authError } from "./utils.js";
//#region src/auth/methods/code.ts
/**
* Emails a one-time sign-in code, for an app that signs people in with a code
* rather than a password.
*
* It succeeds whether or not the address has an account, so nobody can use the
* form it backs to find out who is registered. Say "check your email" either
* way.
*/
async function sendSignInCode(details) {
const result = await makePlatform(sessionConfig().applicationId).sendSignInCode(details.email);
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 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) {
const result = await makePlatform(sessionConfig().applicationId).signInWithCode(credentials);
if (!result.ok) {
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 };