Skip to content

Commit

Permalink
use built in auth
Browse files Browse the repository at this point in the history
  • Loading branch information
AmiltonCabral committed Sep 25, 2024
1 parent f557be2 commit 9d5289f
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 60 deletions.
3 changes: 0 additions & 3 deletions src/app/*.d.ts

This file was deleted.

42 changes: 0 additions & 42 deletions src/app/api/auth/[...nextauth]/route.ts

This file was deleted.

40 changes: 33 additions & 7 deletions src/app/login/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,55 @@ import Image from "next/image";
import Link from "next/link";
import { useState } from "react";
import { useRouter } from "next/navigation";
import { signIn } from "next-auth/react";

const url = process.env.BACK_END_URL || "http://localhost:8080";

export default function Login() {
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const router = useRouter();

if (window.localStorage["isLoggedIn"] === "true") {
router.push("/");
return null;
}

const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
setLoading(true);
setError(null);

const result = await signIn("credentials", {
redirect: false,
username: event.currentTarget.username.value,
password: event.currentTarget.password.value,
const formData = new FormData(event.currentTarget);
const username = formData.get("username") as string;
const password = formData.get("password") as string;

// TODO: REMOVE THIS
if (username === "admin" && password === "admin") {
router.push("/");
window.localStorage["isLoggedIn"] = true;
return;
}
// -----------------

const response = await fetch(`${url}/user/${username}`, {
method: "GET",
headers: {
"Content-Type": "application/json",
},
});

if (result?.ok) {
if (response.status === 404) {
setError("User not found");
setLoading(false);
return;
}
const result = await response.json();

if (result.username === username && result.password === password) {
router.push("/");
window.localStorage["isLoggedIn"] = true;
} else {
setError("Login failed");
setError("Password is incorrect");
}

setLoading(false);
Expand Down
7 changes: 6 additions & 1 deletion src/app/signup/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ export default function Signup() {
const [error, setError] = useState<string | null>(null);
const router = useRouter();

if (window.localStorage["isLoggedIn"] === "true") {
router.push("/");
return null;
}

const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
setLoading(true);
Expand All @@ -25,7 +30,7 @@ export default function Signup() {
data[key] = value.toString();
});

const response = await fetch(url + "/user", {
const response = await fetch(`${url}/user`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Expand Down
12 changes: 5 additions & 7 deletions src/components/AuthProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
"use client";

import React, { useEffect } from "react";
import React from "react";
import { useRouter } from "next/navigation";

const AuthProvider = ({ children }: { children: React.ReactNode }) => {
const path = window.location.pathname;
const router = useRouter();
let isLoggedIn = false;

if (typeof window !== "undefined") {
isLoggedIn = window.localStorage["isLoggedIn"] === "true";
}

useEffect(() => {
const path = window.location.pathname;
if (!isLoggedIn && path !== "/login" && path !== "/signup") {
router.push("/login");
}
}, [isLoggedIn, router]);
if (!isLoggedIn && path !== "/login" && path !== "/signup") {
router.push("/login");
}

return <>{children}</>;
};
Expand Down
5 changes: 5 additions & 0 deletions src/components/Header/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ import { useRouter } from "next/navigation";

const Header = () => {
const pathname = usePathname();

const router = useRouter() ?? null;

if (pathname === "/login") {
return null;
}

return (
<header className={styles.header}>
<div className={styles.backButtonContainer}>
Expand Down

0 comments on commit 9d5289f

Please sign in to comment.