feat: redirect first-time users to signup page instead of login
This commit is contained in:
parent
5e00badb04
commit
d752c31baf
2 changed files with 25 additions and 2 deletions
|
|
@ -12,9 +12,21 @@ async function hasActiveSession(): Promise<boolean> {
|
|||
return refreshSession();
|
||||
}
|
||||
|
||||
async function checkAuthInitialized(): Promise<boolean> {
|
||||
try {
|
||||
const res = await fetch("/api/auth/status");
|
||||
if (!res.ok) return true; // fallback to login on error
|
||||
const data = (await res.json()) as { initialized: boolean };
|
||||
return data.initialized;
|
||||
} catch {
|
||||
return true; // fallback to login on error
|
||||
}
|
||||
}
|
||||
|
||||
export async function requireAuth(): Promise<void> {
|
||||
if (await hasActiveSession()) return;
|
||||
throw redirect({ to: "/login" });
|
||||
const initialized = await checkAuthInitialized();
|
||||
throw redirect({ to: initialized ? "/login" : "/signup" });
|
||||
}
|
||||
|
||||
export async function requireGuest(): Promise<void> {
|
||||
|
|
|
|||
|
|
@ -63,7 +63,18 @@ export function AuthForm({ mode }: AuthFormProps): ReactElement | null {
|
|||
const response = await fetch("/api/auth/status");
|
||||
if (!response.ok) throw new Error("Failed to load auth status.");
|
||||
const result = (await response.json()) as AuthStatusResponse;
|
||||
if (!canceled) setInitialized(result.initialized);
|
||||
if (!canceled) {
|
||||
setInitialized(result.initialized);
|
||||
// Auto-redirect to the correct page based on init state
|
||||
if (mode === "login" && result.initialized === false) {
|
||||
navigate({ to: "/signup" });
|
||||
return;
|
||||
}
|
||||
if (mode === "signup" && result.initialized === true) {
|
||||
navigate({ to: "/login" });
|
||||
return;
|
||||
}
|
||||
}
|
||||
} catch (err: unknown) {
|
||||
if (!canceled) {
|
||||
setError(err instanceof Error ? err.message : "Failed to load.");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue