@softr/sdk
Version:
The SDK for building Softr apps on TanStack Start. It lets an app interact with Softr: auth, datasources, workflows and integrations.
72 lines (70 loc) • 3.06 kB
JavaScript
import { assert, panic, softAssert } from "../../assertions.js";
import { writeCookies } from "../../cookies.js";
import { sessionConfig } from "../config.js";
import { makePlatform } from "../platform.js";
import { assertCanChange, clearPendingAuth, clearSession, makePendingAuthCookie, makeSessionDataCookie, makeSessionTokenCookie, readSessionToken } from "../session.js";
import { authError, isAuthenticatorNotInPlan, isNotInPlan, isUserLimitReached } from "./utils.js";
//#region src/auth/methods/password.ts
/**
* Signs a visitor in with an email and password.
*
* 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. An app that also asks for an emailed code answers
* `emailCodeRequired`; `verifyEmailCode` finishes it.
*/
async function signIn(credentials) {
assertCanChange("auth.signIn");
const result = await makePlatform(sessionConfig().applicationId).tokens(credentials);
if (!result.ok) {
if (isUserLimitReached(result)) return authError("userLimitReached", result.message);
if (isAuthenticatorNotInPlan(result) || isNotInPlan(result)) return authError("secondFactorNotEnabled", result.message);
if (result.status === 401) return authError("invalidCredentials", result.message);
if (result.status === 429) return authError("tooManyAttempts", 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 allows sign-in.`);
}
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)]);
if (mfa.action === "EMAIL_CODE_REQUIRED") return {
success: true,
data: { status: "emailCodeRequired" }
};
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
}
};
}
/**
* Signs the visitor out.
*
* This ends **every** session that user has in this app, on every device. There
* is no per-device sign-out.
*/
async function signOut() {
assertCanChange("auth.signOut");
const token = readSessionToken();
clearSession();
if (!token) return;
const result = await makePlatform(sessionConfig().applicationId).signOut(token);
softAssert(result.ok || result.status < 500, () => `Expected sign-out to succeed, but got ${result.status}. This browser is signed out, other devices may not be.`);
}
//#endregion
export { signIn, signOut };