Add end-user sign-in
Your app's users sign in to MultiPaaS, and your app gets an access token for the generated API. Run the commands in your app's directory, after multipaas init, which Build on the generated API covers.
Register your app
multipaas oauth-client create --name NAME --redirect-uri https://app.example.com/callback
- A redirect URI must be
https, orhttponlocalhostor127.0.0.1. - A client takes 1 to 10 redirect URIs. Repeat
--redirect-urito add more. - Updating the redirect URIs replaces the whole list.
- A test environment can also accept callback URLs of its own, such as a pull request's preview. They work only when the sign-in names that environment. Test each pull request in its own environment shows how.
- The client is public. It has no secret.
- For your app's own sites, tick Always allow changes on the client's page in the console. Sign a user in says what it does.
- You need permission to manage the application's sign-in (
application.manage_oauth) from a role granted in every environment, since the sign-in client is shared by all of them.
The client's id is saved in multipaas.json as oauthClientId.
Sign a user in
Use the endpoints that multipaas init --json printed: endpoints.authorize, endpoints.token, endpoints.revoke and endpoints.userinfo. They are also listed in the server's metadata, at /.well-known/oauth-authorization-server on the API host. If your framework has an OAuth library with PKCE, use it.
- Authorize. Send the browser to the authorize endpoint with
client_id,redirect_uri(one of the client's registered ones, or one registered on the environment you sign in to: the one named inenvironment, else the default environment),response_type=code,code_challenge,code_challenge_method=S256andstate. Addenvironment, theenvironmentIdinmultipaas.json(written bymultipaas initfrom CLI 1.2.0), to sign in to one environment. It is optional, and leaving it out means the default environment. To let the user pick an account, addprompt=select_account, which always shows the account chooser.prompt=loginmakes the user sign in again, and wins when both are given.prompttakes space-separated values, andconsentchanges nothing.prompt=noneon its own is answered with aninteraction_requiredredirect to yourredirect_uriwith yourstate, because the user is always asked, so don't send it.nonewith another value, or any value not named here, is refused withinvalid_request. To ask for one person, addlogin_hintwith their email address: with noprompt, if they are already signed in on the browser, the chooser is skipped, and otherwise sign-in opens with the address filled in.promptoutrankslogin_hint, soselect_accountstill shows the chooser andloginstill signs in again. Every account signed in on the browser is one click on the chooser, and choosing one never changes the account the user's other tabs or apps use. Amultipaas.jsonwritten by a CLI older than 1.2.0 has noenvironmentId, even after you upgrade; to sign in to another environment, runinitagain as Environments describes, adding--api-version API_VERSIONwith the file'sapiVersionto keep the pin. Don't sendscope. For an environment other than the default, the consent page names it. The user signs in to one of that environment's app-tenants only, choosing one on the page when they can reach several. The page also has an Allow changes checkbox, on by default. If the user turns it off, the tokens can only read: every mutation is refused withFORBIDDEN("This connection can only read"), and that holds after a refresh. If the sign-in client is set to Always allow changes (in the console, oralwaysAllowChangesthrough the builder tools), the page has no checkbox, and tokens from a sign-in made while it is on are never limited to reading (users still change only what their roles allow). Read-only sign-ins from before stay read-only until the user signs in again. Set it for your app's own sites; leave it off for a client another app uses. The tokens are for that environment. If that environment has been deleted, the authorize request is refused; see Environments for repointingmultipaas.json. - Exchange. Your callback receives
codeandstate. Checkstate. Within 60 seconds,POSTthe code, form-encoded (application/x-www-form-urlencoded), to the token endpoint withgrant_type=authorization_code,client_id,code,redirect_uriandcode_verifier. The answer holds an access token and a refresh token. - Call the API with
Authorization: Bearer <access_token>andX-MultiPaaS-App-Api-Version: <the apiVersion in multipaas.json>. - See who signed in.
GETthe userinfo endpoint withAuthorization: Bearer <access_token>. The answer is{"sub", "email", "app_tenant_id", "app_tenant_name"}, wheresubis the user's id andapp_tenant_idandapp_tenant_namename the app-tenant the user signed in to. An expired or revoked token answers 401 withinvalid_token: refresh it, and if the refresh is refused, send the user through the authorize step again. A token fromclient_credentials(an app identity) or one for an MCP server answers 403 withinsufficient_scope, because neither signs a person in: call userinfo only with the token this flow returned.login_hintis only a hint, so read the signed-in user from here rather than assuming it. - Refresh before the access token expires.
POST, form-encoded, to the token endpoint withgrant_type=refresh_token,client_idandrefresh_token. If the sign-in's environment has been deleted, the refresh is refused withinvalid_grant: send the user through the authorize step again, for a live environment. - Sign out.
POST, form-encoded, to the revoke endpoint withclient_idandtoken, the refresh token.
Access tokens last 3600 seconds. Each refresh returns a new pair, so store both. If you use a refresh token twice, the whole sign-in is revoked. The one exception is a refresh whose answer never arrived: retry it with the same refresh token within 60 seconds, and the retry gets a new pair. The pair in the lost answer stops working. A sign-in lasts at most 7 days, and then the user signs in again. An access token issued just before then still lasts its full hour.
Several accounts
Your app can let one browser hold several of its users and switch between them:
- Store each account by who signed in, and to which app-tenant. After the exchange, call the userinfo endpoint and store the tokens under its
subandapp_tenant_idtogether, with theemailandapp_tenant_nameto show. That pair is the account: one user signed in to one app-tenant. Keep each account under its ownlocalStoragekey, so every tab shares it. Tokens there are readable by your app's own scripts, as tokens in any browser storage are. Key byapp_tenant_id, not the name, which is only for showing. A browser keeps one sign-in per user per app-tenant, so signing in again as a stored user to the same app-tenant replaces it in every tab, and signing in to another app-tenant adds a second account. - Keep the current account per tab in
sessionStorage, and the last account used inlocalStorage. A tab with no account of its own starts on the last account used and keeps it, so switching in one tab never changes another. If the account it would use is no longer stored, its own or the last one used, it shows the account list. - Switch by saving the other account for the tab and reloading the page. Show who is signed in by email and app-tenant name, since two accounts can share an email, with the other accounts one click away.
- Add an account with
prompt=select_account. Sign an account back in withlogin_hintset to its email, and store the result under thesubandapp_tenant_idthat userinfo returns, since the user can pick someone else, or another app-tenant. Store it under that account's lock, so a refresh or sign-out in another tab can't overwrite or remove it. If the account already had a sign-in, revoke its refresh token first, so the old one ends too. Give each sign-in its own id, kept through refreshes. The new sign-in can differ from the one it replaces, as one that can only read does, so a tab using that account must redraw from scratch when it sees a new id, not keep what it loaded as the old one. The other tabs hear of the new sign-in only after it is stored, so give every action the id of the page it was taken on and check it before each request that carries the account's token, later steps included: on a mismatch, stop the action before that request, redraw, and tell the user. Take the id and the token from one read of the stored account, so a sign-in stored between two reads can't pair the old id with the new token, and drop any data that comes back for a page that has since been redrawn. - Refresh one account at a time across tabs. Using a refresh token twice revokes the sign-in, so take a Web Lock named for the account (
navigator.locks.request). Inside it, read the tokens again and refresh only if they're still due. Store the new pair before the lock is released, so no other tab reads and spends the old refresh token. If the server refuses the refresh, with an OAuth error at 400, or 401 for an unknown client, revoke the refresh token and treat the sign-in as ended. Give every request you make under an account's lock a timeout, here and when adding or signing out an account, so one that never settles can't hold the lock. Make it longer than the token endpoint can take to answer, which is up to 30 seconds after a quiet spell; an app that gives up first keeps a token the server has already rotated. Any other failure says nothing about the token, so keep it: no answer in time, a 5xx, a 429 or an answer you can't read. The next refresh spends it again, and if the server had already rotated it before the answer was lost, a retry within 60 seconds gets a new pair; after that the server refuses it as reused and the sign-in ends. Web Locks needs HTTPS, orlocalhostwhile developing. - Keep an ended sign-in in the list. When the server refuses a refresh, keep the account with no tokens, offer to sign it back in, and never fall back to another account.
- Sign out of one account by revoking its refresh token under the same lock, and removing only that account. An ended sign-in has no refresh token to revoke, so just remove it. Other tabs can listen for the
storageevent: a tab on the removed account shows the account list and never falls back to another account, and every other tab drops it from its list. - Don't let a failed revoke stop you. Store the new sign-in, end the account's sign-in after a refused refresh, or remove the account anyway, so a network error can't lose a sign-in or keep one the user signed out of. The sign-in a failed revoke leaves on the server stays usable until it ends on its own: its refresh token for at most 7 days after the sign-in began, and the last access token it issued for up to an hour after that.
Background jobs
A job with no user signed in uses an app identity instead. Run background jobs covers it.
More
multipaas oauth-client --help- Using Claude Code? The MultiPaaS plugin does these steps for you.