Skip to content

Commit

Permalink
feedback para user
Browse files Browse the repository at this point in the history
  • Loading branch information
JoaoCampanher committed Aug 6, 2024
1 parent 745c57c commit 0cdfe6c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/app/api/register/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@ export async function POST(request: NextRequest, response: NextResponse) {
try {
const data = await request.json();
const encryptedPassword = await bcrypt.hash(data.password, 1);
const user = await prisma.user.create({
const existingUser = await prisma.user.findUnique({
where: { email: data.email },
});

if (existingUser) {
return NextResponse.json({ error: "Usuário já existe" }, { status: 400 });
}

await prisma.user.create({
data: {
email: data.email,
name: data.name,
Expand Down
3 changes: 3 additions & 0 deletions src/app/login/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import TextInput from "@/components/TextInput";
import APICaller from "@/utils/APICaller";
import { useRouter } from "next/navigation";
import { useState } from "react";
import toast from "react-hot-toast";

export default function Login() {
const [email, setEmail] = useState("");
Expand All @@ -22,6 +23,8 @@ export default function Login() {
if (response.success) {
localStorage.setItem("token", response.token);
router.push("/");
} else {
toast.error("Usuário ou senha inválidos");
}
} catch (error) {
console.error("Erro ao fazer login:", error);
Expand Down
1 change: 1 addition & 0 deletions src/app/register/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export default function Register() {
toast.success("Registrado com sucesso!");
router.replace("/login");
}
response.error && toast.error(response.error);
} catch (error) {
toast.error("Erro ao registrar!");
console.error("Error ao registrar:", error);
Expand Down

0 comments on commit 0cdfe6c

Please sign in to comment.