-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
217 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
"use client"; | ||
import React, { useRef } from "react"; | ||
import Header from "@/components/Header"; | ||
import Link from "next/link"; | ||
import { signIn } from "next-auth/react"; | ||
|
||
const Login = () => { | ||
const email = useRef(""); | ||
const pass = useRef(""); | ||
|
||
const onSubmit = async () => { | ||
const result = await signIn("credentials", { | ||
email: email.current, | ||
password: pass.current, | ||
redirect: true, | ||
callbackUrl: "/dashboard/user", | ||
}); | ||
}; | ||
|
||
return ( | ||
<> | ||
<Header /> | ||
<div className="w-5/6 lg:w-1/2 xl:w-1/3 mx-auto my-12 flex flex-col items-center font-medium"> | ||
<p className="text-5xl mb-12">Login</p> | ||
<div className="container flex flex-col gap-8"> | ||
<div className="flex flex-col"> | ||
<label htmlFor="email">Email</label> | ||
<input | ||
name="email" | ||
id="email" | ||
className="border border-black h-10 px-2" | ||
type="email" | ||
required | ||
onChange={(e) => (email.current = e.target.value)} | ||
/> | ||
</div> | ||
<div className="flex flex-col"> | ||
<label htmlFor="password">Password</label> | ||
<input | ||
name="password" | ||
id="password" | ||
className="border border-black h-10 px-2" | ||
type="password" | ||
required | ||
onChange={(e) => (pass.current = e.target.value)} | ||
/> | ||
</div> | ||
|
||
<button | ||
type="submit" | ||
className="w-full h-12 mt-4 bg-rose-600 text-white hover:bg-rose-700" | ||
onClick={onSubmit} | ||
> | ||
Login | ||
</button> | ||
</div> | ||
<p className="mt-2 font-normal"> | ||
Not Registered? | ||
<span> | ||
<Link | ||
href="/register" | ||
passHref | ||
className="text-blue-700 font-bold hover:text-blue-500" | ||
> | ||
Sign up | ||
</Link> | ||
</span> | ||
</p> | ||
</div> | ||
</> | ||
); | ||
}; | ||
|
||
export default Login; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
"use client"; | ||
import Header from "@/components/Header"; | ||
import Link from "next/link"; | ||
import { signIn } from "next-auth/react"; | ||
import { ChangeEvent, useState } from "react"; | ||
|
||
export default function RegisterPage() { | ||
let [loading, setLoading] = useState(false); | ||
let [formValues, setFormValues] = useState({ | ||
name: "", | ||
email: "", | ||
password: "", | ||
}); | ||
|
||
const onSubmit = async (e: React.FormEvent) => { | ||
e.preventDefault(); | ||
setLoading(true); | ||
|
||
try { | ||
const res = await fetch("/api/register", { | ||
method: "POST", | ||
body: JSON.stringify(formValues), | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
|
||
setLoading(false); | ||
if (!res.ok) { | ||
alert((await res.json()).message); | ||
return; | ||
} | ||
|
||
signIn(undefined, { callbackUrl: "/" }); | ||
} catch (error: any) { | ||
setLoading(false); | ||
console.error(error); | ||
alert(error.message); | ||
} | ||
}; | ||
|
||
const handleChange = (event: ChangeEvent<HTMLInputElement>) => { | ||
const { name, value } = event.target; | ||
setFormValues({ ...formValues, [name]: value }); | ||
}; | ||
return ( | ||
<> | ||
<Header /> | ||
<div className="w-5/6 lg:w-1/2 xl:w-1/3 mx-auto my-12 text-center font-medium"> | ||
<h1 className="text-5xl mb-12">Create Account</h1> | ||
<form | ||
className="container flex flex-col gap-8 text-left items-center" | ||
onSubmit={onSubmit} | ||
> | ||
<div className="flex flex-col w-full"> | ||
<label htmlFor="name"> | ||
Name<span className="text-rose-600">*</span> | ||
</label> | ||
<input | ||
name="name" | ||
id="name" | ||
className="border border-black h-10 px-2" | ||
type="text" | ||
required | ||
value={formValues.name} | ||
onChange={handleChange} | ||
/> | ||
</div> | ||
<div className="flex flex-col w-full"> | ||
<label htmlFor="email" className="font-medium"> | ||
</label> | ||
<input | ||
name="email" | ||
id="email" | ||
className="border border-black h-10 px-2" | ||
type="email" | ||
required | ||
value={formValues.email} | ||
onChange={handleChange} | ||
/> | ||
</div> | ||
<div className="flex flex-col w-full"> | ||
<label htmlFor="password"> | ||
Password<span className="text-rose-600">*</span> | ||
</label> | ||
<input | ||
name="password" | ||
id="password" | ||
className="border border-black h-10 px-2" | ||
type="password" | ||
minLength={8} | ||
required | ||
value={formValues.password} | ||
onChange={handleChange} | ||
/> | ||
</div> | ||
<button | ||
type="submit" | ||
className="w-full h-12 mt-4 bg-rose-600 text-white hover:bg-rose-700" | ||
disabled={loading} | ||
> | ||
{loading ? "Loading..." : "Register"} | ||
</button> | ||
</form> | ||
<p className="mt-2 font-normal"> | ||
Already have an account? | ||
<span> | ||
<Link | ||
href="/login" | ||
passHref | ||
className="text-blue-700 font-bold hover:text-blue-500" | ||
> | ||
Sign in | ||
</Link> | ||
</span> | ||
</p> | ||
</div> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { prisma } from "@/lib/prisma"; | ||
import { compare } from "bcrypt"; | ||
import type { NextAuthOptions } from "next-auth"; | ||
import type { NextAuthOptions, User } from "next-auth"; | ||
import CredentialsProvider from "next-auth/providers/credentials"; | ||
|
||
export const authOptions: NextAuthOptions = { | ||
|
@@ -17,29 +17,30 @@ export const authOptions: NextAuthOptions = { | |
placeholder: "[email protected]", | ||
}, | ||
password: { label: "Password", type: "password" }, | ||
|
||
}, | ||
async authorize(credentials) { | ||
async authorize(credentials, req) { | ||
if (!credentials?.email || !credentials.password) { | ||
return null; | ||
} | ||
|
||
const user = await prisma.user.findUnique({ | ||
where: { | ||
email: credentials.email, | ||
}, | ||
}); | ||
|
||
if (!user || !(await compare(credentials.password, user.password))) { | ||
return null; | ||
} | ||
|
||
return { | ||
id: user.id, | ||
email: user.email, | ||
name: user.name, | ||
randomKey: "Hey cool", | ||
}; | ||
}, | ||
} as unknown as User; // Cast the return value to the User type | ||
} | ||
}), | ||
], | ||
callbacks: { | ||
|
@@ -67,4 +68,8 @@ export const authOptions: NextAuthOptions = { | |
return token; | ||
}, | ||
}, | ||
pages: { | ||
signIn: "/login", | ||
signOut: "/register", | ||
} | ||
}; |