mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-12-03 18:43:38 +01:00
Exercised the full provider portal as a pilot MSP would and fixed what made it feel broken: - The signed-out portal promised "a sign-in link is on the way" even when the control plane has no email provider (the bundle default), and team invitations silently sent nothing. The portal bootstrap now carries email_sign_in_available and provider_hosted_mode; the sign-in page shows the host command that actually prints a link, and the invite panel says invitation emails are not sent and how to hand over a link instead. - New "provider-msp portal-link --email" CLI mints a one-time portal link for an account member or pending invitee, so teammates can sign in at all on email-less installs (bootstrap only covers the owner). - Portal sessions were fixed at 12h; CP_SESSION_TTL now configures them and provider-hosted MSP mode defaults to 7 days. - Creating a client past the license cap showed a generic "Failed to create workspace." toast: the limit error is now a JSON payload with current/limit, the API client no longer drops non-JSON error bodies (double body read), and the toast explains the license limit. - Copy polish: provider-mode sign-in intro (no refunds/privacy register), least-privilege default invite role, queue tile label matches "Client onboarding", softer Support tab with a docs/MSP.md pointer, setup.sh summary now prints the bootstrap next step and day-2 sign-in commands, .env.example and docs/MSP.md document portal sign-in and sessions. Contracts updated (cloud-paid, api-contracts, deployment-installability, security-privacy) with verification pins in tenant_handlers_test, config_test, magiclink_test, and provider_msp_deploy_test. Verified live against a dockerless control plane: portal-link for an invitee redeems, promotes the invitation, and sets a 7-day session; the at-cap toast shows the license copy; portal vitest suite and cloudcp/auth/account/installtests Go suites pass.
133 lines
4.4 KiB
TypeScript
133 lines
4.4 KiB
TypeScript
import { beginMutationState, failMutationState, resetMutationState, succeedMutationState } from './async_state';
|
|
import { PortalAPIError } from './api';
|
|
import type { PortalAPI } from './api';
|
|
import { createPortalLoginState, syncLoginStateBootstrapEmail } from './state';
|
|
import type { PortalLoginState } from './types';
|
|
import type { PortalStore } from './store';
|
|
|
|
type FormValueElement = HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement;
|
|
|
|
export interface AuthControllerDeps {
|
|
api: PortalAPI;
|
|
store: PortalStore;
|
|
}
|
|
|
|
export interface AuthController {
|
|
getLoginState: () => PortalLoginState;
|
|
}
|
|
|
|
function asHTMLElement(target: EventTarget | null): HTMLElement | null {
|
|
return target instanceof HTMLElement ? target : null;
|
|
}
|
|
|
|
function getElement<T extends HTMLElement = HTMLElement>(id: string): T | null {
|
|
return document.getElementById(id) as T | null;
|
|
}
|
|
|
|
export function installAuthController(deps: AuthControllerDeps): AuthController {
|
|
deps.store.updateLoginState(function(loginState) {
|
|
var bootstrap = deps.store.getBootstrap();
|
|
syncLoginStateBootstrapEmail(loginState, bootstrap.email || '');
|
|
}, { notify: false });
|
|
|
|
async function sendMagicLink() {
|
|
if (deps.store.getBootstrap().email_sign_in_available === false) {
|
|
// The signed-out view replaces the form with operator instructions in
|
|
// this state; this guard covers any stale markup.
|
|
return;
|
|
}
|
|
var loginState = deps.store.getLoginState();
|
|
var email = String(loginState.emailValue || '').trim();
|
|
if (!email) {
|
|
var input = getElement<FormValueElement>('portal-login-email');
|
|
if (input) input.focus();
|
|
return;
|
|
}
|
|
deps.store.updateLoginState(function(nextState) {
|
|
beginMutationState(nextState.request);
|
|
nextState.success = false;
|
|
nextState.successMessage = '';
|
|
});
|
|
try {
|
|
var response = await deps.api.requestMagicLink(email);
|
|
deps.store.updateLoginState(function(nextState) {
|
|
succeedMutationState(nextState.request);
|
|
nextState.success = true;
|
|
nextState.successMessage = String(response?.message || '').trim();
|
|
});
|
|
return;
|
|
} catch (error) {
|
|
if (error instanceof PortalAPIError && error.status === 404) {
|
|
deps.store.updateLoginState(function(nextState) {
|
|
succeedMutationState(nextState.request);
|
|
nextState.success = true;
|
|
nextState.successMessage = '';
|
|
});
|
|
return;
|
|
}
|
|
deps.store.updateLoginState(function(nextState) {
|
|
failMutationState(
|
|
nextState.request,
|
|
error instanceof PortalAPIError && error.status === 429
|
|
? 'Too many requests. Please wait a moment and try again.'
|
|
: 'Network error. Please check your connection and try again.'
|
|
);
|
|
});
|
|
}
|
|
}
|
|
|
|
document.addEventListener('click', function(event) {
|
|
var portalActionEl = asHTMLElement(event.target)?.closest('[data-portal-action]');
|
|
if (portalActionEl) {
|
|
var portalAction = portalActionEl.getAttribute('data-portal-action') || '';
|
|
switch (portalAction) {
|
|
case 'send-magic-link':
|
|
event.preventDefault();
|
|
void sendMagicLink();
|
|
return;
|
|
case 'resend-magic-link':
|
|
event.preventDefault();
|
|
deps.store.updateLoginState(function(nextState) {
|
|
nextState.success = false;
|
|
nextState.successMessage = '';
|
|
resetMutationState(nextState.request);
|
|
});
|
|
void sendMagicLink();
|
|
return;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
var logoutBtn = asHTMLElement(event.target)?.closest('#logout-btn') as HTMLButtonElement | null;
|
|
if (!logoutBtn) {
|
|
return;
|
|
}
|
|
event.preventDefault();
|
|
logoutBtn.disabled = true;
|
|
logoutBtn.textContent = 'Signing out…';
|
|
(async function() {
|
|
try {
|
|
await deps.api.logout();
|
|
} catch (_) {}
|
|
window.location.href = deps.store.getBootstrap().portal_path;
|
|
})();
|
|
});
|
|
|
|
document.addEventListener('input', function(event) {
|
|
var target = asHTMLElement(event.target) as FormValueElement | null;
|
|
if (!target) return;
|
|
if (target.getAttribute('data-portal-input') === 'login-email') {
|
|
deps.store.updateLoginState(function(nextState) {
|
|
nextState.emailValue = target.value;
|
|
}, { notify: false });
|
|
}
|
|
});
|
|
|
|
return {
|
|
getLoginState: function() {
|
|
return deps.store.getLoginState();
|
|
},
|
|
};
|
|
}
|