Files
FL-Akademie/components/login-form.tsx

65 lines
1.7 KiB
TypeScript

"use client";
import { useSearchParams, useRouter } from "next/navigation";
import { signIn } from "next-auth/react";
import { useState } from "react";
export function LoginForm() {
const search = useSearchParams();
const router = useRouter();
const callbackUrl = search.get("callbackUrl") || "/dashboard";
const [email, setEmail] = useState("lernender@akademie.local");
const [password, setPassword] = useState("devpassword");
const [error, setError] = useState<string | null>(null);
const [loading, setLoading] = useState(false);
async function onSubmit(e: React.FormEvent) {
e.preventDefault();
setLoading(true);
setError(null);
const res = await signIn("credentials", {
email,
password,
redirect: false,
callbackUrl,
});
setLoading(false);
if (res?.error) {
setError("Anmeldung fehlgeschlagen.");
return;
}
router.push(callbackUrl);
router.refresh();
}
return (
<form className="form panel" onSubmit={onSubmit}>
<label>
E-Mail
<input
type="email"
autoComplete="username"
value={email}
onChange={(e) => setEmail(e.target.value)}
required
/>
</label>
<label>
Passwort
<input
type="password"
autoComplete="current-password"
value={password}
onChange={(e) => setPassword(e.target.value)}
required
/>
</label>
{error ? <p className="error">{error}</p> : null}
<button type="submit" className="btn btn-primary" disabled={loading}>
{loading ? "…" : "Anmelden"}
</button>
</form>
);
}