All files / blong-browser/src/components/Login Login.tsx

0% Statements 0/406
0% Branches 0/1
0% Functions 0/1
0% Lines 0/406

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
/**
 * Login — full-screen authentication form.
 *
 * The screen is divided into three vertically symmetric sections:
 *
 *  ┌──────────────────────────────────────────┐
 *  │                              [Register]  │  ← fixed top-right
 *  │  ┌── product area (1fr, end-aligned) ──┐ │  ← titleComponent + brand
 *  │  │  [titleComponent slot]              │ │
 *  │  │  [icon]  Product Title              │ │
 *  │  └─────────────────────────────────────┘ │
 *  │  ┌── card (auto) ──────────────────────┐ │  ← login form
 *  │  │  Login with password                │ │
 *  │  │  Username / Password / Login btn    │ │
 *  │  └─────────────────────────────────────┘ │
 *  │  ┌── org brand area (1fr, start) ──────┐ │  ← orgComponent / orgTitle
 *  │  │  [org icon]  Organisation Name      │ │
 *  │  └─────────────────────────────────────┘ │
 *  └──────────────────────────────────────────┘
 *
 * Product area (above) and org brand area (below) both receive `1fr` height,
 * keeping the card centred regardless of their content size.
 *
 * Registration mechanism:
 *   Pass `registerPage` (an action name) and the framework dispatches
 *   `component/${registerPage}` when the Register button is clicked.
 *   The resolved component replaces the login form.
 */
import './Login.css';

import {InputText, Message, Password} from '../../primereact/index.js';

import React, {useState} from 'react';
import {useBlong} from '../../context/BlongContext.js';
import {Button} from '../Button/Button.js';

type LoginStep = 'credentials' | 'otp' | 'newPassword';

interface ILoginCredentials {
    username: string;
    password: string;
}

export interface ILoginProps {
    onLogin: (credentials: ILoginCredentials) => Promise<void>;
    onOtp?: (otp: string) => Promise<void>;
    onNewPassword?: (passwords: {newPassword: string; confirmPassword: string}) => Promise<void>;
    /**
     * Application / brand name displayed next to the logo icon.
     * Also used as the document-level page title on the login screen.
     */
    title?: string;
    /** PrimeIcons class for the brand icon (e.g. 'pi pi-cog'). Default: 'pi pi-circle'. */
    logoIcon?: string;
    /** URL for a brand logo image (renders instead of logoIcon when set). */
    logoUrl?: string;
    /**
     * Pluggable React component rendered above the brand area,
     * e.g. for tenant banners, announcements, or extra branding.
     */
    titleComponent?: React.ComponentType;
    /**
     * Organisation brand area rendered below the login card.
     * Equal height to the product area above — keeps the card centred.
     *
     * Pass a fully custom component via `orgComponent`, or use the
     * built-in row layout via `orgTitle` + `orgIcon` / `orgLogoUrl`.
     */
    orgComponent?: React.ComponentType;
    /** Organisation name shown in the footer brand area. */
    orgTitle?: string;
    /** PrimeIcons class for the org icon (e.g. 'pi pi-building'). */
    orgIcon?: string;
    /** URL for the org logo image (renders instead of orgIcon when set). */
    orgLogoUrl?: string;
    /**
     * Action name dispatched as `component/${registerPage}` when the Register button is
     * clicked.  The resolved component is shown in place of the login form.
     *
     * When set, a "Register" button appears in the top-right corner.
     */
    registerPage?: string;
    /** Label for the Register button.  Default: 'Register'. */
    registerLabel?: string;
    /** External error to display above the form. */
    error?: string;
    loading?: boolean;
    /** Initial step — useful for Storybook previews and unit tests. */
    initialStep?: LoginStep;
}

export function Login({
    onLogin,
    onOtp,
    onNewPassword,
    title,
    logoIcon = 'pi pi-circle',
    logoUrl,
    titleComponent: TitleComponent,
    orgComponent: OrgComponent,
    orgTitle,
    orgIcon,
    orgLogoUrl,
    registerPage,
    registerLabel = 'Register',
    error: externalError,
    loading: externalLoading,
    initialStep = 'credentials',
}: ILoginProps) {
    const {handler} = useBlong();
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    const [step, setStep] = useState<LoginStep>(initialStep);
    const [username, setUsername] = useState('');
    const [password, setPassword] = useState('');
    const [otp, setOtp] = useState('');
    const [newPassword, setNewPassword] = useState('');
    const [confirmPassword, setConfirmPassword] = useState('');
    const [error, setError] = useState('');
    const [loading, setLoading] = useState(false);
    const [RegisterComp, setRegisterComp] = useState<React.ComponentType | null>(null);

    const isLoading = externalLoading ?? loading;
    const displayError = externalError ?? error;

    const handleCredentials = async (e: React.FormEvent) => {
        e.preventDefault();
        setError('');
        if (!username.trim()) {
            setError('Username is required');
            return;
        }
        if (!password.trim()) {
            setError('Password is required');
            return;
        }
        setLoading(true);
        try {
            await onLogin({username, password});
        } catch (err) {
            setError(String((err as Error).message ?? err));
        } finally {
            setLoading(false);
        }
    };

    const handleOtp = async (e: React.FormEvent) => {
        e.preventDefault();
        if (!otp.trim()) {
            setError('OTP is required');
            return;
        }
        setLoading(true);
        try {
            await onOtp?.(otp);
        } catch (err) {
            setError(String((err as Error).message ?? err));
        } finally {
            setLoading(false);
        }
    };

    const handleNewPassword = async (e: React.FormEvent) => {
        e.preventDefault();
        if (newPassword !== confirmPassword) {
            setError('Passwords do not match');
            return;
        }
        setLoading(true);
        try {
            await onNewPassword?.({newPassword, confirmPassword});
        } catch (err) {
            setError(String((err as Error).message ?? err));
        } finally {
            setLoading(false);
        }
    };

    const handleRegister = async () => {
        if (!registerPage) return;
        try {
            const comp = await handler[`component/${registerPage}`]({}, {});
            if (comp) setRegisterComp(() => comp as React.ComponentType);
        } catch {
            /* ignore — dispatch may show its own error */
        }
    };

    // ── Registration view ────────────────────────────────────────────────────
    if (RegisterComp) {
        return (
            <div className="blong-login p-component">
                <div className="blong-login__register-area">
                    <Button
                        label="Back to Login"
                        icon="pi pi-arrow-left"
                        className="p-button-text"
                        onClick={() => setRegisterComp(null)}
                    />
                </div>
                <div className="blong-login__register-view">
                    <RegisterComp />
                </div>
            </div>
        );
    }

    // ── Login view ────────────────────────────────────────────────────────────
    return (
        <div className="blong-login p-component">
            {registerPage && (
                <div className="blong-login__register-area">
                    <Button
                        label={registerLabel}
                        className="p-button-outlined"
                        onClick={() => void handleRegister()}
                    />
                </div>
            )}

            {/* Product area — 1fr row above the card */}
            <div className="blong-login__top">
                {TitleComponent && (
                    <div className="blong-login__title-slot">
                        <TitleComponent />
                    </div>
                )}
                {(title || logoUrl || logoIcon) && (
                    <div className="blong-login__brand">
                        {logoUrl ? (
                            <img
                                src={logoUrl}
                                alt={title ?? 'logo'}
                                className="blong-login__brand-img"
                            />
                        ) : (
                            <i className={`blong-login__brand-icon ${logoIcon}`} />
                        )}
                        {title && <span className="blong-login__brand-name">{title}</span>}
                    </div>
                )}
            </div>

            {/* Login card — auto-height middle row */}
            <div className="blong-login__card">
                <p className="blong-login__card-heading">Login with password</p>

                {displayError && (
                    <Message
                        severity="error"
                        text={displayError}
                        className="blong-login__error"
                    />
                )}

                {step === 'credentials' && (
                    <form
                        className="blong-login__form"
                        onSubmit={e => void handleCredentials(e)}
                    >
                        <div className="blong-field">
                            <label
                                htmlFor="login-username"
                                className="blong-field__label"
                            >
                                Username
                            </label>
                            <InputText
                                id="login-username"
                                name="username"
                                value={username}
                                onChange={e => setUsername(e.target.value)}
                                className="blong-login__input w-full"
                                autoFocus
                                disabled={isLoading}
                            />
                        </div>
                        <div className="blong-field">
                            <label
                                htmlFor="login-password"
                                className="blong-field__label"
                            >
                                Password
                            </label>
                            <Password
                                inputId="login-password"
                                name="password"
                                value={password}
                                onChange={e => setPassword(e.target.value)}
                                className="blong-login__input w-full"
                                inputClassName="w-full"
                                toggleMask
                                disabled={isLoading}
                            />
                        </div>
                        <Button
                            type="submit"
                            label="Login"
                            loading={isLoading}
                            className="blong-login__submit"
                            data-testid="login-submit"
                        />
                    </form>
                )}

                {step === 'otp' && (
                    <form
                        className="blong-login__form"
                        onSubmit={e => void handleOtp(e)}
                    >
                        <p className="blong-login__hint">
                            Enter the one-time password sent to your device.
                        </p>
                        <div className="blong-field">
                            <label
                                htmlFor="login-otp"
                                className="blong-field__label"
                            >
                                One-Time Password
                            </label>
                            <InputText
                                id="login-otp"
                                value={otp}
                                onChange={e => setOtp(e.target.value)}
                                autoFocus
                                disabled={isLoading}
                            />
                        </div>
                        <Button
                            type="submit"
                            label="Verify"
                            loading={isLoading}
                            className="blong-login__submit"
                        />
                    </form>
                )}

                {step === 'newPassword' && (
                    <form
                        className="blong-login__form"
                        onSubmit={e => void handleNewPassword(e)}
                    >
                        <p className="blong-login__hint">
                            Your password has expired. Please set a new password.
                        </p>
                        <div className="blong-field">
                            <label
                                htmlFor="login-new-pass"
                                className="blong-field__label"
                            >
                                New Password
                            </label>
                            <Password
                                inputId="login-new-pass"
                                value={newPassword}
                                onChange={e => setNewPassword(e.target.value)}
                                toggleMask
                                disabled={isLoading}
                            />
                        </div>
                        <div className="blong-field">
                            <label
                                htmlFor="login-confirm-pass"
                                className="blong-field__label"
                            >
                                Confirm Password
                            </label>
                            <Password
                                inputId="login-confirm-pass"
                                value={confirmPassword}
                                onChange={e => setConfirmPassword(e.target.value)}
                                toggleMask
                                disabled={isLoading}
                            />
                        </div>
                        <Button
                            type="submit"
                            label="Set Password"
                            loading={isLoading}
                            className="blong-login__submit"
                        />
                    </form>
                )}
            </div>

            {/* Org brand area — 1fr row below the card, same height as product area */}
            <div className="blong-login__bottom">
                {OrgComponent ? (
                    <OrgComponent />
                ) : orgTitle || orgLogoUrl || orgIcon ? (
                    <div className="blong-login__org-brand">
                        {orgLogoUrl ? (
                            <img
                                src={orgLogoUrl}
                                alt={orgTitle ?? 'logo'}
                                className="blong-login__org-img"
                            />
                        ) : orgIcon ? (
                            <i className={`blong-login__org-icon ${orgIcon}`} />
                        ) : null}
                        {orgTitle && <span className="blong-login__org-name">{orgTitle}</span>}
                    </div>
                ) : null}
            </div>
        </div>
    );
}