Files
gerbeur/src/utils/auth/home.ts

43 lines
1.4 KiB
TypeScript

/**
* Points at the instance an account from another gerbeur comes from, its
* home, where its password, email, avatar and name are changed. Such a user's
* handle here is `name@host` (`splitHandle`); the profile, the settings and
* the `/authorize` page read it through these.
*
* @module
*/
import { API_URL } from "../../config/api.ts";
import { splitHandle } from "../../../shared/validation.ts";
/**
* Checks whether `host`, as a handle writes it, is this instance's: its API's,
* which handles carry, or its pages'. Another instance writes this one's users
* with it.
*/
export function isSelfHost(host: string): boolean {
const h = host.toLowerCase();
return h === new URL(API_URL || location.origin).host ||
h === location.host;
}
/**
* Returns the host of `handle`'s home instance, or undefined for a local
* user.
*/
export function homeHost(handle: string): string | undefined {
return splitHandle(handle).host;
}
/**
* Returns a link to `path` on `handle`'s home instance, their profile there
* by default, or null for a local user. The host in a handle is the home's
* API, not necessarily its pages, so the link goes through
* `GET /api/users/:key/home`, which redirects to them.
*/
export function homeUrl(handle: string, path?: string): string | null {
if (!homeHost(handle)) return null;
const query = path ? `?path=${encodeURIComponent(path)}` : "";
return `${API_URL}/api/users/@${encodeURIComponent(handle)}/home${query}`;
}