Skip to content

Commit

Permalink
fixed login and register page
Browse files Browse the repository at this point in the history
  • Loading branch information
WTanardi committed May 24, 2023
1 parent 26ac6b3 commit eb1451c
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 54 deletions.
7 changes: 4 additions & 3 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,12 @@ model Menu {
}

model User {
id Int @id @default(autoincrement())
id Int @id @default(autoincrement())
name String
email String @unique
email String @unique
password String
Order Order[]
isAdmin Boolean
orders Order[]
}

model Order {
Expand Down
74 changes: 74 additions & 0 deletions src/app/(auth)/login/page.tsx
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"
>
&nbsp; Sign up
</Link>
</span>
</p>
</div>
</>
);
};

export default Login;
File renamed without changes.
121 changes: 121 additions & 0 deletions src/app/(auth)/register/page.tsx
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">
Email
</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>
</>
);
}
1 change: 1 addition & 0 deletions src/app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ import NextAuth from "next-auth";

const handler = NextAuth(authOptions);
export { handler as GET, handler as POST };

23 changes: 0 additions & 23 deletions src/app/api/login.ts

This file was deleted.

7 changes: 5 additions & 2 deletions src/app/api/register/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export async function POST(req: Request) {
name: string;
email: string;
password: string;
isAdmin: boolean;
};
const hashed_password = await hash(password, 12);

Expand All @@ -16,13 +17,15 @@ export async function POST(req: Request) {
name,
email: email.toLowerCase(),
password: hashed_password,
isAdmin: false,
},
});

return NextResponse.json({
user: {
name: user.name,
email: user.email,
isAdmin: user.isAdmin,
},
});
} catch (error: any) {
Expand All @@ -34,4 +37,4 @@ export async function POST(req: Request) {
{ status: 500 }
);
}
}
}
19 changes: 0 additions & 19 deletions src/app/register/page.tsx

This file was deleted.

19 changes: 12 additions & 7 deletions src/lib/auth.ts
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 = {
Expand All @@ -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: {
Expand Down Expand Up @@ -67,4 +68,8 @@ export const authOptions: NextAuthOptions = {
return token;
},
},
pages: {
signIn: "/login",
signOut: "/register",
}
};

0 comments on commit eb1451c

Please sign in to comment.