@softr/sdk
Version:
The SDK for building Softr apps on TanStack Start. It lets an app interact with Softr: auth, datasources, workflows and integrations.
83 lines (81 loc) • 3.19 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/passwordLink.ts
/**
* Emails a link to set a new password.
*
* Succeeds whether or not the address has an account, and sends nothing when
* it has none.
*/
async function requestPasswordReset(details) {
const result = await makePlatform(sessionConfig().applicationId).forgotPassword(details);
if (!result.ok) {
if (result.status === 429) return authError("tooManyAttempts", result.message);
panic(`Expected the password reset link to be emailed, but got ${result.status}: ${result.message}.`);
}
return {
success: true,
data: null
};
}
/**
* Checks a password reset or invitation link, and returns the address it was
* mailed to. The link stays usable.
*/
async function checkPasswordLink(details) {
const result = await makePlatform(sessionConfig().applicationId).checkPasswordToken(details);
if (!result.ok) {
if (result.status === 429) return authError("tooManyAttempts", result.message);
if (result.status === 400 || result.status === 401) return authError("linkExpired", result.message);
panic(`Expected the link to be checked, but got ${result.status}: ${result.message}.`);
}
return {
success: true,
data: result.data
};
}
/**
* Sets a new password with a password reset or invitation link, and signs the
* visitor in.
*
* Any success uses up the link, `authenticatorRequired` included, and ends
* the visitor's other sessions.
*/
async function setPasswordWithLink(details) {
const result = await makePlatform(sessionConfig().applicationId).setPassword(details);
if (!result.ok) {
if (result.status === 429) return authError("tooManyAttempts", result.message);
if (result.status === 400 && result.message === "Password must be at least 8 characters long.") return authError("invalidDetails", result.message);
if (result.status === 400 || result.status === 401) return authError("linkExpired", result.message);
panic(`Expected the password to be set, but got ${result.status}: ${result.message}.`);
}
if (result.data.status === "MFA_REQUIRED") {
const { mfa } = result.data;
assert(mfa, "Expected a challenge from setting a password, 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 setting a password, 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 { checkPasswordLink, requestPasswordReset, setPasswordWithLink };