@softr/sdk
Version:
The SDK for building Softr apps on TanStack Start. It lets an app interact with Softr: auth, datasources, workflows and integrations.
74 lines (72 loc) • 3.16 kB
JavaScript
import { assert, panic } from "../../assertions.js";
import { sessionConfig } from "../config.js";
import { makePlatform } from "../platform.js";
import { assertCanChange, clearSessionData, readSessionToken } from "../session.js";
import { authError } from "./utils.js";
//#region src/auth/methods/profile.ts
/**
* Changes the signed-in visitor's own name, photo, or both. A field left out
* stays as it is. {@link removeAvatar} removes the photo.
*
* The photo can change only where `user.avatarEditable` is true. Elsewhere a
* photo answers `avatarNotSupported`, so hide the photo controls instead.
*
* Answers with the visitor as they are now, without `properties`. The page keeps showing what it
* loaded until its route context loads again, so call `router.invalidate()`
* after a success.
*
* A bad name answers `invalidName`. A photo the platform refuses, sent alone,
* answers `invalidPhoto`. When a name and a photo are sent together, a refusal
* answers `invalidName`.
*
* @example
* const result = await auth.updateProfile({ fullName: 'Ann Lee' })
* const result = await auth.updateProfile({ avatar: file })
*/
async function updateProfile(changes) {
assertCanChange("auth.updateProfile");
assert(changes.fullName !== void 0 || changes.avatar !== void 0, "updateProfile needs a name or a photo to change");
const token = readSessionToken();
if (token === null) return authError("notAuthenticated", "Nobody is signed in.");
const form = new FormData();
if (changes.fullName !== void 0) form.set("fullName", changes.fullName);
if (changes.avatar) form.set("avatar", changes.avatar);
const platform = makePlatform(sessionConfig().applicationId);
const badRequest = changes.fullName === void 0 ? "invalidPhoto" : "invalidName";
return answer(await platform.updateProfile(token, form), badRequest);
}
/**
* Removes the signed-in visitor's own photo. Like {@link updateProfile}, it
* works only where `user.avatarEditable` is true, and answers with the visitor
* as they are now, without `properties`.
*
* @example
* const result = await auth.removeAvatar()
*/
async function removeAvatar() {
assertCanChange("auth.removeAvatar");
const token = readSessionToken();
if (token === null) return authError("notAuthenticated", "Nobody is signed in.");
return answer(await makePlatform(sessionConfig().applicationId).removeAvatar(token), "invalidPhoto");
}
function answer(result, badRequest) {
if (!result.ok) {
switch (result.status) {
case 400: return authError(badRequest, result.message);
case 401: return authError("notAuthenticated", result.message);
case 409: return authError("avatarNotSupported", result.message);
case 413: return authError("fileTooLarge", result.message);
case 415: return authError("notAnImage", result.message);
case 429: return authError("tooManyAttempts", result.message);
}
if (result.status >= 500) panic(`Expected the profile to update, but got ${result.status}: ` + result.message);
throw new Error(`${result.message}. Check SOFTR_APPLICATION_ID.`);
}
clearSessionData();
return {
success: true,
data: result.data
};
}
//#endregion
export { removeAvatar, updateProfile };