feat(auth): add guest login option with environment variables and distinct loading states
All checks were successful
Deploy Flowstate to VPS / deploy (push) Successful in 44s
All checks were successful
Deploy Flowstate to VPS / deploy (push) Successful in 44s
This commit is contained in:
parent
3db1a18086
commit
361551a022
1 changed files with 73 additions and 22 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
/**
|
/**
|
||||||
* @file LoginPage.tsx
|
* @file (auth)/login/page.tsx
|
||||||
* @description Client component providing a user login interface with NextAuth credentials authentication.
|
* @description Client component providing a user login interface with NextAuth credentials authentication.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
@ -19,28 +19,30 @@ import Link from "next/link";
|
||||||
export default function LoginPage() {
|
export default function LoginPage() {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
const [loading, setLoading] = useState(false);
|
const [loadingType, setLoadingType] = useState<
|
||||||
|
"credentials" | "guest" | null
|
||||||
|
>(null);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles the submission of the login form, validates credentials via NextAuth,
|
* Universal sign-in handler for both manual credentials and guest login.
|
||||||
* and manages loading/error states or redirects upon success.
|
|
||||||
*
|
*
|
||||||
* @async
|
* @async
|
||||||
* @param {React.SubmitEvent<HTMLFormElement>} e - The form submission event.
|
* @param {string} email - The user email.
|
||||||
|
* @param {string} password - The user password.
|
||||||
|
* @param {"credentials" | "guest"} type - The login method type for specific loading states.
|
||||||
* @returns {Promise<void>}
|
* @returns {Promise<void>}
|
||||||
*/
|
*/
|
||||||
const handleSubmit = async (e: React.SubmitEvent<HTMLFormElement>) => {
|
const handleSignIn = async (
|
||||||
e.preventDefault();
|
email: string,
|
||||||
|
password: string,
|
||||||
|
type: "credentials" | "guest",
|
||||||
|
) => {
|
||||||
setError(null);
|
setError(null);
|
||||||
setLoading(true);
|
setLoadingType(type);
|
||||||
|
|
||||||
const formData = new FormData(e.currentTarget);
|
|
||||||
const email = formData.get("email") as string;
|
|
||||||
const password = formData.get("password") as string;
|
|
||||||
const defaultErrorMessage = "An unexpected error has occurred.";
|
const defaultErrorMessage = "An unexpected error has occurred.";
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Attempt to sign in using Auth.js credentials provider
|
|
||||||
const res = await signIn("credentials", {
|
const res = await signIn("credentials", {
|
||||||
email,
|
email,
|
||||||
password,
|
password,
|
||||||
|
|
@ -48,33 +50,63 @@ export default function LoginPage() {
|
||||||
});
|
});
|
||||||
|
|
||||||
if (res?.error) {
|
if (res?.error) {
|
||||||
// Handle specific authentication errors
|
|
||||||
if (res.error === "CredentialsSignin") {
|
if (res.error === "CredentialsSignin") {
|
||||||
setError("Incorrect email address or password.");
|
setError("Incorrect email address or password.");
|
||||||
} else {
|
} else {
|
||||||
setError(defaultErrorMessage);
|
setError(defaultErrorMessage);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Redirect user
|
|
||||||
router.push("/summary");
|
router.push("/summary");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
// Handle network or connection errors vs unexpected errors
|
|
||||||
if (err?.message?.includes("fetch") || err?.name === "TypeError") {
|
if (err?.message?.includes("fetch") || err?.name === "TypeError") {
|
||||||
setError("Server error: API endpoint not available");
|
setError("Server error: API endpoint not available");
|
||||||
} else {
|
} else {
|
||||||
setError(defaultErrorMessage);
|
setError(defaultErrorMessage);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoadingType(null);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles regular form submission.
|
||||||
|
*
|
||||||
|
* @async
|
||||||
|
* @param {React.SubmitEvent<HTMLFormElement>} e - The form submission event.
|
||||||
|
* @returns {Promise<void>}
|
||||||
|
*/
|
||||||
|
const handleSubmit = async (e: React.SubmitEvent<HTMLFormElement>) => {
|
||||||
|
e.preventDefault();
|
||||||
|
const formData = new FormData(e.currentTarget);
|
||||||
|
const email = formData.get("email") as string;
|
||||||
|
const password = formData.get("password") as string;
|
||||||
|
await handleSignIn(email, password, "credentials");
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles quick guest login using environment variables.
|
||||||
|
*
|
||||||
|
* @async
|
||||||
|
* @returns {Promise<void>}
|
||||||
|
*/
|
||||||
|
const handleGuestLogin = async () => {
|
||||||
|
const guestEmail = process.env.NEXT_PUBLIC_GUEST_EMAIL;
|
||||||
|
const guestPassword = process.env.NEXT_PUBLIC_GUEST_PASSWORD;
|
||||||
|
|
||||||
|
if (!guestEmail || !guestPassword) {
|
||||||
|
setError("Guest login is not configured.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await handleSignIn(guestEmail, guestPassword, "guest");
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex h-dvh w-full items-center justify-center">
|
<div className="flex h-dvh w-full items-center justify-center">
|
||||||
{/* Login Card Container */}
|
{/* Login Card Container */}
|
||||||
<div className="w-full max-w-sm p-6 space-y-6 border border-border rounded-xl bg-card backdrop-blur-md">
|
<div className="w-full max-w-sm p-6 space-y-4 border border-border rounded-xl bg-card backdrop-blur-md">
|
||||||
{/* Header Section */}
|
{/* Header Section */}
|
||||||
<div className="space-y-2 text-center">
|
<div className="space-y-2 text-center">
|
||||||
<h1 className="text-2xl font-bold tracking-tight">Flowstate Login</h1>
|
<h1 className="text-2xl font-bold tracking-tight">Flowstate Login</h1>
|
||||||
|
|
@ -101,7 +133,6 @@ export default function LoginPage() {
|
||||||
name="email"
|
name="email"
|
||||||
type="email"
|
type="email"
|
||||||
placeholder="name@example.com"
|
placeholder="name@example.com"
|
||||||
defaultValue="test@flowstate.io"
|
|
||||||
required
|
required
|
||||||
className="w-full px-3 py-2 text-sm rounded-lg border border-border bg-background focus:outline-none focus:border-foreground"
|
className="w-full px-3 py-2 text-sm rounded-lg border border-border bg-background focus:outline-none focus:border-foreground"
|
||||||
/>
|
/>
|
||||||
|
|
@ -114,8 +145,7 @@ export default function LoginPage() {
|
||||||
<input
|
<input
|
||||||
name="password"
|
name="password"
|
||||||
type="password"
|
type="password"
|
||||||
placeholder="••••••••"
|
placeholder="•••••••••••••"
|
||||||
defaultValue="password"
|
|
||||||
required
|
required
|
||||||
className="w-full px-3 py-2 text-sm rounded-lg border border-border bg-background focus:outline-none focus:border-foreground"
|
className="w-full px-3 py-2 text-sm rounded-lg border border-border bg-background focus:outline-none focus:border-foreground"
|
||||||
/>
|
/>
|
||||||
|
|
@ -123,13 +153,34 @@ export default function LoginPage() {
|
||||||
|
|
||||||
<button
|
<button
|
||||||
type="submit"
|
type="submit"
|
||||||
disabled={loading}
|
disabled={loadingType !== null}
|
||||||
className="w-full py-2 text-sm font-medium rounded-lg bg-foreground text-background hover:opacity-70 transition-opacity disabled:opacity-20 cursor-pointer disabled:cursor-auto"
|
className="w-full py-2 text-sm font-medium rounded-lg bg-foreground text-background hover:opacity-70 transition-opacity disabled:opacity-20 cursor-pointer disabled:cursor-auto"
|
||||||
>
|
>
|
||||||
{loading ? "Logging in..." : "Sign In"}
|
{loadingType === "credentials" ? "Signing in..." : "Sign In"}
|
||||||
</button>
|
</button>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
|
{/* Divider */}
|
||||||
|
<div className="relative flex items-center">
|
||||||
|
<div className="grow border-t border-border"></div>
|
||||||
|
<span className="shrink mx-4 text-xs text-foreground-muted uppercase">
|
||||||
|
or
|
||||||
|
</span>
|
||||||
|
<div className="grow border-t border-border"></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Guest Login Button */}
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
disabled={loadingType !== null}
|
||||||
|
onClick={handleGuestLogin}
|
||||||
|
className="w-full py-2 text-sm font-medium rounded-lg bg-background hover:opacity-70 transition-opacity disabled:opacity-20 cursor-pointer disabled:cursor-auto border border-border"
|
||||||
|
>
|
||||||
|
{loadingType === "guest"
|
||||||
|
? "Signing in as Guest..."
|
||||||
|
: "Sign in as Guest"}
|
||||||
|
</button>
|
||||||
|
|
||||||
{/* Registration Link Footer */}
|
{/* Registration Link Footer */}
|
||||||
<div className="text-center text-xs text-foreground-muted">
|
<div className="text-center text-xs text-foreground-muted">
|
||||||
Don't have an account yet?{" "}
|
Don't have an account yet?{" "}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue