committing summative
This commit is contained in:
1 parent
73ac8e85be
commit
95b138f29e
44 files changed
+5319
-755
No files matched your search
@@ -0,0 +1,95 @@
|
||||
"use client"
|
||||
|
||||
import { signIn } from "next-auth/react"; // client function
|
||||
import { useState } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import Link from "next/link";
|
||||
|
||||
export default function AuthLoginForm() {
|
||||
const [email, setEmail] = useState("");
|
||||
const [password, setPassword] = useState("");
|
||||
const [error, setError] = useState("");
|
||||
const [isSubmitting, setIsSubmitting] = useState(false);
|
||||
|
||||
const router = useRouter();
|
||||
|
||||
const handleSubmit = async (e: React.SubmitEvent<HTMLFormElement>) => { // event object from form element
|
||||
e.preventDefault(); // stops default submission to send through the js instead
|
||||
setError(""); // clear state
|
||||
setIsSubmitting(true);
|
||||
|
||||
try {
|
||||
const res = await signIn("credentials", { // sends to auth.js server endpoint selecting credentials provider and supplies obj argument which it passes into authorize function, returns the user
|
||||
email,
|
||||
password,
|
||||
redirect: false, // prevents auto redirect
|
||||
});
|
||||
|
||||
if (res.error || !res) {
|
||||
setError("Sign in failed");
|
||||
} else {
|
||||
router.push("/"); // if signin function doesnt error
|
||||
}
|
||||
} catch {
|
||||
setError("Unable to sign in. Please try again.")
|
||||
} finally {
|
||||
setIsSubmitting(false);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<form onSubmit={handleSubmit} className="mt-4">
|
||||
{error && (
|
||||
<div className="alert alert-error mb-4">
|
||||
<span>{error}</span>
|
||||
</div>
|
||||
)}
|
||||
{" "}
|
||||
<div className="form-control">
|
||||
<label className="label">
|
||||
<span className="label-text">Email</span>
|
||||
</label>
|
||||
<input
|
||||
type="email"
|
||||
placeholder="Enter your email"
|
||||
className="input input-bordered w-full"
|
||||
value={email}
|
||||
onChange={(e) => setEmail(e.target.value)}
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
<div className="form-control mt-4">
|
||||
<label className="label">
|
||||
<span className="label-text">Password</span>
|
||||
</label>
|
||||
<input
|
||||
type="password"
|
||||
placeholder="Enter your password"
|
||||
className="input input-bordered w-full"
|
||||
value={password}
|
||||
onChange={(e) => setPassword(e.target.value)}
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
<div className="form-control mt-6">
|
||||
<button type="submit" className="btn btn-primary w-full" disabled={isSubmitting}>
|
||||
{isSubmitting ? (
|
||||
<span className="loading loading-spinner loading-sm"></span>
|
||||
) : (
|
||||
"Sign In"
|
||||
)}
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
<div className="divider">OR</div>
|
||||
<p className="text-cent">
|
||||
Don't have an account?{" "}
|
||||
<Link href="/register" className="link link-primary">
|
||||
Sign Up
|
||||
</Link>
|
||||
</p>
|
||||
</>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user