committing summative

This commit is contained in:
Archie committed 2026-11-06 10:02:29 +00:00
1 parent 73ac8e85be
commit 95b138f29e
44 files changed
+5319 -755

No files matched your search

+11
View File
@@ -0,0 +1,11 @@
export default function AuthLayout({ children }: { children: React.ReactNode }) {
return (
<div className="min-h[calc(100vh-4rem)] flex items-center justify-center p-4">
<div className="card w-full max-w-md bg-base-200 shadow-xl">
<div className="card-body">
{children}
</div>
</div>
</div>
)
}
+95
View File
@@ -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>
</>
);
}
+24
View File
@@ -0,0 +1,24 @@
export default function SigninButton({
text,
style,
callback,
formAction
}: {
text: string;
style: string;
callback?: () => void;
formAction?: any;
}) {
return (
<div className="form-control mt-6">
<button
type="submit"
formAction={formAction}
className={style}
onClick={callback}
>
{text}
</button>
</div>
);
};
+60
View File
@@ -0,0 +1,60 @@
"use client";
import { Suspense, useState, useEffect } from "react";
import AuthLoginForm from "./AuthLoginForm"
import SigninButton from "./SigninButton";
import { handleGoogleLogin } from "@/app/actions/GoogleAction";
import { useSearchParams } from "next/navigation";
function LoginContent() {
const [elogin, setElogin] = useState(false);
const showEmailLogin = () => { // lets you toggle on and off showEmailLogin which is used to decide if the authloginform is shown
setElogin(!elogin);
};
const searchParams = useSearchParams();
const isJustRegisted = searchParams.get("registered") === "true"; // if redirected with reigstered = true
useEffect(() => {
if (isJustRegisted) {
setElogin(true);
}
}, [isJustRegisted]);
return (
<>
<h2 className="card-title text-2xl justify-center">Please Sign In</h2>
<form className="mt-4">
{isJustRegisted && ( // display if redirect with search param
<div className="p-10 bg-emerald-1001 text-emerald-700 mb-15 rounded-md">
Account created successfully. You can now sign in.
</div>
)}
<SigninButton
text="Google"
style="btn btn-primary w-full"
formAction={handleGoogleLogin}
/>
</form>
<div className="mt-4">
<SigninButton
text="Email"
style="btn btn-primary w-full"
callback={showEmailLogin}
/>
</div>
<div className="mt-4">{elogin ? <AuthLoginForm /> : ""}</div>
</>
)
}
export default function LoginPage() { // displays fallback while url dependent content loads (searchparams), errors if it tries to prepare page in advance
return (
<>
<Suspense fallback={<p>Loading login..</p>}>
<LoginContent />
</Suspense>
</>
);
}
+124
View File
@@ -0,0 +1,124 @@
'use client';
import Link from "next/link";
import { useState, useEffect } from "react";
import { register } from "../../lib/client/register";
import { useRouter } from "next/navigation"
export default function RegisterPage() {
const [name, setName] = useState(""); // creating state objects and getting setters
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [confirmPassword, setConfirmPassword] = 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
if (password !== confirmPassword) {
setError("Passwords do not match")
return;
}
if (password.length < 16 || password === password.toLowerCase() || password === password.toUpperCase()) { // rejects password if its not 16+ characters or if its all in the same case
setError("Password must be at least 16 characters and a mix of lower and upper case. To keep your account secure also include special characters and dont use predictable phrases.")
return;
}
setIsSubmitting(true); // once password is valid
try {
await register({ email, password, name })
router.push("/login?registered=true")
} catch (error) {
setError(error instanceof Error ? error.message : "Registration failed")
} finally {
setIsSubmitting(false);
}
}
// useEffect(() => { // on page load
// if (!isLoading && isAuthenticated) {
// router.push("/")
// }
// },[isLoading, isAuthenticated, router])
return (
<>
<h2 className="card-title text-2xl justify-center">Create Account</h2>
<form onSubmit={handleSubmit} className="mt-4">
{error && (
<div className="alert alert-error mb-4">
<span>{error}</span>
</div>
)}
{" "}
<div className="form-control mt-4">
<label className="label">
<span className="label-text">Name</span>
</label>
<input
type="text"
placeholder="Enter your name"
className="input input-bordered w-full"
value={name}
onChange={(e) => setName(e.target.value)}
/>
</div>
<div className="form-control mt-4">
<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-4">
<label className="label">
<span className="label-text">Confirm Password</span>
</label>
<input
type="password"
placeholder="Confirm your password"
className="input input-bordered w-full"
value={confirmPassword}
onChange={(e) => setConfirmPassword(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>
) : (
"Create Account"
)}
</button>
</div>
</form>
</>
)
};