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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 87x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 97x 97x 97x 29x 29x 29x 29x 29x 3x 29x 29x 29x 29x 29x 97x 97x 97x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 97x 97x 1x 1x 1x 1x 97x 97x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 68x 97x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 66x 97x 59x 59x 59x 59x 59x 59x 59x 59x 25x 25x 25x 25x 25x 25x 25x 25x 25x 47x 1x 1x 1x 1x 1x 1x 34x 33x 33x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 34x 34x 34x 34x 34x 34x 34x 59x 59x 59x 59x 59x 59x 59x 59x 59x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 59x 59x 59x 59x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 31x 97x 97x 97x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 2x 6x 6x 8x 8x 8x 8x 1x 1x 1x 1x 1x 1x 1x 1x | import { useEffect, useRef, useState, type ReactNode } from "react";
import { signIn, signOut } from "../services/auth/authService";
import {
onAuthChange,
readAuthSnapshot,
type AuthSnapshot,
} from "../services/auth/authState";
import { detectLegacyData, type LegacyData } from "../services/migrationService";
import { AuthError } from "../types/auth";
import { MigrationPrompt } from "./MigrationPrompt";
const CONTACT_DEVELOPER_MAILTO =
"mailto:kippolitov@gmail.com?subject=Job%20Posting%20Analyzer%20account%20access";
/**
* Wraps every user surface (FR-001): children render only for a signed-in
* session. Session expiry while children are mounted shows a re-sign-in
* overlay WITHOUT unmounting them, so in-progress form input survives
* re-authentication (FR-014). Explicit sign-out and revocation unmount
* children — personal data leaves the view.
*/
export function AuthGate({
children,
showSignOut = true,
}: {
children: ReactNode;
/** The options page hides sign-out — the side panel is the account surface. */
showSignOut?: boolean;
}) {
const [snapshot, setSnapshot] = useState<AuthSnapshot | null>(null);
const [signingIn, setSigningIn] = useState(false);
const [error, setError] = useState<string | null>(null);
// True while children have been shown this session: distinguishes expiry
// (overlay, keep children) from a fresh signed-out visit (full gate).
const wasSignedInRef = useRef(false);
// One-time migration offer (FR-010): checked once after sign-in, before
// the features render.
const [migration, setMigration] = useState<{
checked: boolean;
data: LegacyData | null;
}>({ checked: false, data: null });
useEffect(() => {
if (snapshot?.status !== "signed-in" || migration.checked) return;
let mounted = true;
detectLegacyData()
.then((data) => {
if (mounted) setMigration({ checked: true, data });
})
.catch(() => {
// Detection failure must not block the app; the offer is best-effort
// and will fire on the next panel open.
if (mounted) setMigration({ checked: true, data: null });
});
return () => {
mounted = false;
};
}, [snapshot, migration.checked]);
useEffect(() => {
let mounted = true;
void readAuthSnapshot().then((s) => {
if (mounted) setSnapshot(s);
});
const unsubscribe = onAuthChange((s) => {
if (mounted) setSnapshot(s);
});
return () => {
mounted = false;
unsubscribe();
};
}, []);
const handleSignIn = async () => {
setSigningIn(true);
setError(null);
try {
await signIn();
setSnapshot(await readAuthSnapshot());
} catch (err) {
setError(
err instanceof AuthError && err.code === "sign-in-canceled"
? "Sign-in was canceled."
: "Sign-in failed. Please try again."
);
} finally {
setSigningIn(false);
}
};
const handleSignOut = async () => {
wasSignedInRef.current = false;
await signOut();
setSnapshot(await readAuthSnapshot());
};
if (snapshot === null) {
return (
<div
className="flex h-full min-h-32 items-center justify-center"
role="status"
aria-label="Checking sign-in status"
>
<span className="text-sm text-gray-400 dark:text-gray-500">Loading…</span>
</div>
);
}
if (snapshot.status === "not-authorized") {
wasSignedInRef.current = false;
return (
<GateCard>
<h2 className="text-sm font-semibold text-gray-800 dark:text-gray-100">
Access unavailable
</h2>
<p className="mt-2 text-sm text-gray-500 dark:text-gray-400">
Sign-in requires a verified Google email address — verify it in
your Google Account settings and try again. If your access was
suspended, contact the developer to request it back.
</p>
<a
href={CONTACT_DEVELOPER_MAILTO}
className="mt-4 inline-block rounded-lg bg-blue-600 px-4 py-2 text-sm font-semibold text-white transition-colors hover:bg-blue-700"
>
Contact the developer
</a>
<button
onClick={() => void handleSignIn()}
className="mt-2 block text-xs text-gray-500 underline hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200"
>
Sign in with a different account
</button>
</GateCard>
);
}
if (snapshot.status === "signed-in" || wasSignedInRef.current) {
// One stable tree for both the live session and the expired-session
// overlay: children keep their position, so React never remounts them and
// in-progress input survives re-authentication (FR-014).
const expired = snapshot.status !== "signed-in";
if (!expired) wasSignedInRef.current = true;
let body: ReactNode;
if (!expired && !migration.checked) {
body = (
<div
role="status"
aria-label="Checking for local data to migrate"
className="flex h-full min-h-32 items-center justify-center"
>
<span className="text-sm text-gray-400 dark:text-gray-500">Loading…</span>
</div>
);
} else if (!expired && migration.data) {
body = (
<MigrationPrompt
data={migration.data}
onDone={() => setMigration({ checked: true, data: null })}
/>
);
} else {
body = children;
}
return (
<div className="relative flex h-full flex-col">
<div className="flex shrink-0 items-center justify-between gap-2 border-b border-gray-200/70 bg-white px-3 py-1.5 dark:border-gray-800 dark:bg-gray-900">
<span
className="truncate text-xs text-gray-500 dark:text-gray-400"
title={snapshot.user?.email}
>
{expired ? "Signed out" : snapshot.user?.email}
</span>
{!expired && showSignOut && (
<button
onClick={() => void handleSignOut()}
aria-label="Sign out"
className="shrink-0 rounded-md px-2 py-1 text-xs font-medium text-gray-500 transition-colors hover:bg-gray-100 hover:text-gray-700 dark:text-gray-400 dark:hover:bg-gray-800 dark:hover:text-gray-200"
>
Sign out
</button>
)}
</div>
<div
className={`min-h-0 flex-1 ${expired ? "pointer-events-none opacity-40" : ""}`}
aria-hidden={expired || undefined}
>
{body}
</div>
{expired && (
<div className="absolute inset-0 flex items-center justify-center bg-gray-950/30 p-4">
<div
role="dialog"
aria-label="Session ended"
className="w-full max-w-xs rounded-xl border border-gray-200 bg-white p-4 text-center shadow-lg dark:border-gray-700 dark:bg-gray-900"
>
<p className="text-sm font-medium text-gray-800 dark:text-gray-100">
Your session ended.
</p>
<p className="mt-1 text-xs text-gray-500 dark:text-gray-400">
Sign in to continue. Your unsaved changes are still here.
</p>
<SignInButton signingIn={signingIn} onClick={() => void handleSignIn()} />
{error && <GateError message={error} />}
</div>
</div>
)}
</div>
);
}
return (
<GateCard>
<h2 className="text-sm font-semibold text-gray-800 dark:text-gray-100">
Sign in with Google to get started
</h2>
<p className="mt-2 text-sm text-gray-500 dark:text-gray-400">
Create your free account instantly — no invitation needed. Your
profile and saved postings follow your account across devices.
</p>
<DataPracticesDisclosure />
<SignInButton signingIn={signingIn} onClick={() => void handleSignIn()} />
{error && <GateError message={error} />}
</GateCard>
);
}
/**
* CWS Disclosure Requirements (docs/compliance/prominent-disclosure.md):
* prominent, affirmative-consent disclosure of the account/payment data
* practices introduced by self-serve signup + Premium, shown before the
* first sign-in — not buried behind a link. Clicking "Sign in with Google"
* is the affirmative consent action.
*/
function DataPracticesDisclosure() {
return (
<p className="mt-3 text-left text-xs leading-relaxed text-gray-500 dark:text-gray-400">
<strong className="text-gray-700 dark:text-gray-300">
Signing in creates a free account.
</strong>{" "}
We store your email address, entitlement tier (free or Premium), and
monthly analysis usage count. If you subscribe to Premium ($5/month),
Paddle — our payment processor and merchant of record — handles
payment; we never see your card details, only your subscription
status. By continuing, you agree to our Privacy Policy and Terms of
Service, including the terms of sale for Premium.
</p>
);
}
function GateCard({ children }: { children: ReactNode }) {
return (
<div className="flex h-full min-h-64 items-center justify-center bg-gray-50 p-6 dark:bg-gray-950">
<div className="w-full max-w-sm rounded-xl border border-gray-200 bg-white p-5 text-center dark:border-gray-800 dark:bg-gray-900">
{children}
</div>
</div>
);
}
function SignInButton({
signingIn,
onClick,
}: {
signingIn: boolean;
onClick: () => void;
}) {
return (
<button
onClick={onClick}
disabled={signingIn}
aria-label="Sign in with Google"
aria-busy={signingIn}
className="mt-4 w-full rounded-lg bg-blue-600 px-4 py-2 text-sm font-semibold text-white transition-colors hover:bg-blue-700 disabled:cursor-wait disabled:opacity-70"
>
{signingIn ? (
<span role="status">Signing in…</span>
) : (
"Sign in with Google"
)}
</button>
);
}
function GateError({ message }: { message: string }) {
return (
<p role="alert" className="mt-3 text-xs text-red-600 dark:text-red-400">
{message}
</p>
);
}
|