Sessions and credentials
Ask the client who is signed in, and keep the access token apart from code that only needs the user.
A session is who is signed in, { sessionId, user }, and never holds
tokens. getSession() is how code outside React asks for it. It waits for a
restore or a refresh that is under way, so a loader never acts on a stale
answer:
import { } from "./api.js";
import { } from "./auth.js";
/** A loader: it runs outside React, so it asks the client. */
export async function (): <unknown> {
const = await .();
if (!) return null;
const = await (`auth/carts/user/${..}`);
if (!.) throw new (`Cart failed (${.})`);
return .();
}Credentials are the access token, { sessionId, accessToken, expiresAt },
kept apart so that code which only needs the user never holds one.
createAuthFetch reads them for you. Code that
sends requests another way uses auth.credentials: get before a request,
renew after a 401, and reject after a second one.
Other HTTP clients explains the three,
with axios and ky.
auth.isCurrent({ sessionId }) tells whether a session you're holding is
still the signed-in one, for work that might finish after an account switch.