@softr/sdk
Version:
The SDK for building Softr apps on TanStack Start. It lets an app interact with Softr: auth, datasources, workflows and integrations.
81 lines (79 loc) • 3.38 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/signup.ts
/**
* Starts a sign-up flow by emailing a verification code.
*
* User only exists once `confirmSignUp` returns the code.
*/
async function signUp(details) {
const result = await makePlatform(sessionConfig().applicationId).sendSignUpCode(details);
if (!result.ok) {
if (result.status === 409) return authError("emailTaken", result.message);
if (result.status === 429) return authError("tooManyAttempts", result.message);
if (result.status === 400) return authError("invalidDetails", result.message);
if (result.status === 401 && result.message === "Email domain not allowed") return authError("emailDomainNotAllowed", result.message);
if (result.status >= 500) panic(`Expected the sign-up code to be emailed, but got ${result.status}: ` + result.message);
throw new Error(`${result.message}. Check SOFTR_APPLICATION_ID and that the app allows sign-up.`);
}
return {
success: true,
data: null
};
}
/**
* Finishes a sign-up with the emailed code, creating the account and signing
* the visitor in.
*
* `password` is what the account is created with. Leave it out only for an app
* that signs people in with an emailed code. An app expecting a password
* rejects a sign-up without one, and the other way round.
*
* 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 confirmSignUp(details) {
const platform = makePlatform(sessionConfig().applicationId);
const { password,...rest } = details;
const result = password === void 0 ? await platform.signUpWithCode(rest) : await platform.signUpWithPassword({
...rest,
password
});
if (!result.ok) {
if (result.status === 409) return authError("emailTaken", 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-up to succeed, but got ${result.status}: ` + result.message);
throw new Error(`${result.message}. Check SOFTR_APPLICATION_ID and that the app allows sign-up.`);
}
if (result.data.status === "MFA_REQUIRED") {
const { mfa } = result.data;
assert(mfa, "Expected a challenge from sign-up, 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-up, 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 { confirmSignUp, signUp };