|
| 1 | +import { t } from "@lingui/core/macro"; |
| 2 | +import { Trans } from "@lingui/react/macro"; |
| 3 | +import { NotFoundError } from "@repo/infrastructure/auth/routeGuards"; |
| 4 | +import { Button } from "@repo/ui/components/Button"; |
| 5 | +import { Field, FieldContent, FieldDescription, FieldLabel, FieldTitle } from "@repo/ui/components/Field"; |
| 6 | +import { Link } from "@repo/ui/components/Link"; |
| 7 | +import { RadioGroup, RadioGroupItem } from "@repo/ui/components/RadioGroup"; |
| 8 | +import { createFileRoute } from "@tanstack/react-router"; |
| 9 | +import { useState } from "react"; |
| 10 | + |
| 11 | +import logoMarkUrl from "@/shared/images/logo-mark.svg"; |
| 12 | +import { HorizontalHeroLayout } from "@/shared/layouts/HorizontalHeroLayout"; |
| 13 | + |
| 14 | +const MOCK_IDENTITY_IDS = ["admin", "support", "readonly", "plain"] as const; |
| 15 | + |
| 16 | +interface MockLoginSearch { |
| 17 | + returnPath: string; |
| 18 | +} |
| 19 | + |
| 20 | +export const Route = createFileRoute("/login")({ |
| 21 | + staticData: { trackingTitle: "Mock login" }, |
| 22 | + beforeLoad: () => { |
| 23 | + if (process.env.NODE_ENV === "production") { |
| 24 | + throw new NotFoundError(); |
| 25 | + } |
| 26 | + }, |
| 27 | + validateSearch: (search: Record<string, unknown>): MockLoginSearch => { |
| 28 | + const raw = search.returnPath; |
| 29 | + const returnPath = typeof raw === "string" && raw.startsWith("/") ? raw : "/"; |
| 30 | + return { returnPath }; |
| 31 | + }, |
| 32 | + component: MockLoginPage |
| 33 | +}); |
| 34 | + |
| 35 | +function MockLoginPage() { |
| 36 | + const { returnPath } = Route.useSearch(); |
| 37 | + const [selectedId, setSelectedId] = useState<string>("admin"); |
| 38 | + const [isPending, setIsPending] = useState(false); |
| 39 | + |
| 40 | + const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => { |
| 41 | + event.preventDefault(); |
| 42 | + setIsPending(true); |
| 43 | + const encodedReturnPath = encodeURIComponent(returnPath); |
| 44 | + globalThis.location.href = `/.auth/login/aad/callback?identity=${selectedId}&post_login_redirect_uri=${encodedReturnPath}`; |
| 45 | + }; |
| 46 | + |
| 47 | + return ( |
| 48 | + <HorizontalHeroLayout> |
| 49 | + <form onSubmit={handleSubmit} className="flex w-full max-w-[22rem] flex-col items-center gap-4 pt-8 pb-4"> |
| 50 | + <Link href="/" className="cursor-pointer"> |
| 51 | + <img src={logoMarkUrl} className="size-12" alt={t`Logo`} /> |
| 52 | + </Link> |
| 53 | + <h2> |
| 54 | + <Trans>BackOffice - Localhost</Trans> |
| 55 | + </h2> |
| 56 | + <div className="text-center text-sm text-muted-foreground"> |
| 57 | + <Trans> |
| 58 | + Local development sign-in. Production uses Azure Container Apps built-in Entra ID authentication. |
| 59 | + </Trans> |
| 60 | + </div> |
| 61 | + |
| 62 | + <RadioGroup className="w-full pt-2" value={selectedId} onValueChange={setSelectedId}> |
| 63 | + {MOCK_IDENTITY_IDS.map((id) => ( |
| 64 | + <FieldLabel key={id}> |
| 65 | + <Field orientation="horizontal"> |
| 66 | + <RadioGroupItem value={id} /> |
| 67 | + <FieldContent> |
| 68 | + <FieldTitle>{getIdentityName(id)}</FieldTitle> |
| 69 | + <FieldDescription>{getIdentityDescription(id)}</FieldDescription> |
| 70 | + </FieldContent> |
| 71 | + </Field> |
| 72 | + </FieldLabel> |
| 73 | + ))} |
| 74 | + </RadioGroup> |
| 75 | + |
| 76 | + <Button type="submit" isPending={isPending} className="mt-4 w-full text-center"> |
| 77 | + {isPending ? <Trans>Logging in...</Trans> : <Trans>Log in</Trans>} |
| 78 | + </Button> |
| 79 | + </form> |
| 80 | + </HorizontalHeroLayout> |
| 81 | + ); |
| 82 | +} |
| 83 | + |
| 84 | +function getIdentityName(id: string) { |
| 85 | + switch (id) { |
| 86 | + case "admin": |
| 87 | + return <Trans>Admin User</Trans>; |
| 88 | + case "support": |
| 89 | + return <Trans>Support User</Trans>; |
| 90 | + case "readonly": |
| 91 | + return <Trans>Read Only</Trans>; |
| 92 | + case "plain": |
| 93 | + return <Trans>Plain User</Trans>; |
| 94 | + default: |
| 95 | + return id; |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +function getIdentityDescription(id: string) { |
| 100 | + switch (id) { |
| 101 | + case "admin": |
| 102 | + return <Trans>Log in with admin rights</Trans>; |
| 103 | + case "support": |
| 104 | + return <Trans>Log in with support rights</Trans>; |
| 105 | + case "readonly": |
| 106 | + return <Trans>Log in with read-only rights</Trans>; |
| 107 | + case "plain": |
| 108 | + return <Trans>Log in without group claims</Trans>; |
| 109 | + default: |
| 110 | + return null; |
| 111 | + } |
| 112 | +} |
0 commit comments