Skip to content

⚡️⚡️⚡️ Self-hosted collaborative bookmark manager to collect, read, annotate, and fully preserve what matters, all in one place.

Route handling + Added main layout.

Daniel18387e2dde2a
diff --git a/components/Collections.tsx b/components/Collections.tsx
index bef54a42..a1b936a2 100644
--- a/components/Collections.tsx
+++ b/components/Collections.tsx
@@ -16,9 +16,9 @@ export default function Collections() {
return (
<div className="flex flex-wrap">
- {collections.map((e) => {
+ {collections.map((e, i) => {
return (
- <div className="p-5 bg-gray-100 m-2 w-max ">
+ <div className="p-5 bg-gray-100 m-2 w-max " key={i}>
<p>{e.name}</p>
</div>
);
diff --git a/components/Layouts/MainLayout.tsx b/components/Layouts/MainLayout.tsx
new file mode 100644
index 00000000..5b28d732
--- /dev/null
+++ b/components/Layouts/MainLayout.tsx
@@ -0,0 +1,51 @@
+import Head from "next/head";
+import Navbar from "@/components/Navbar";
+import Sidebar from "@/components/Sidebar";
+import { ReactNode } from "react";
+import { useSession } from "next-auth/react";
+import Loader from "../Loader";
+import useRedirection from "@/hooks/useRedirection";
+import { useRouter } from "next/router";
+
+interface LayoutProps {
+ children: ReactNode;
+}
+
+export default function Layout({ children }: LayoutProps) {
+ const { status } = useSession();
+ const router = useRouter();
+
+ const redirection = useRedirection();
+
+ const routeExists = router.route === "/_error" ? false : true;
+
+ if (status === "authenticated" && !redirection && routeExists)
+ return (
+ <>
+ <Head>
+ <title>Linkwarden</title>
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
+ <link rel="icon" href="/favicon.ico" />
+ </Head>
+ <div className="flex">
+ <Sidebar />
+ <div className="ml-80">
+ <Navbar />
+ {children}
+ </div>
+ </div>
+ </>
+ );
+ else if ((status === "unauthenticated" && !redirection) || !routeExists)
+ return (
+ <>
+ <Head>
+ <title>Linkwarden</title>
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
+ <link rel="icon" href="/favicon.ico" />
+ </Head>
+ {children}
+ </>
+ );
+ else return <Loader />;
+}
diff --git a/components/Loader.tsx b/components/Loader.tsx
new file mode 100644
index 00000000..f41c4d50
--- /dev/null
+++ b/components/Loader.tsx
@@ -0,0 +1,7 @@
+export default function Loader() {
+ return (
+ <div>
+ <p>Loading...</p>
+ </div>
+ );
+}
diff --git a/components/Navbar.tsx b/components/Navbar.tsx
new file mode 100644
index 00000000..17e78364
--- /dev/null
+++ b/components/Navbar.tsx
@@ -0,0 +1,3 @@
+export default function Navbar() {
+ return <div>Navbar</div>;
+}
diff --git a/components/Sidebar.tsx b/components/Sidebar.tsx
new file mode 100644
index 00000000..54f073e0
--- /dev/null
+++ b/components/Sidebar.tsx
@@ -0,0 +1,13 @@
+import { useSession } from "next-auth/react";
+
+export default function Sidebar() {
+ const { data: session, status } = useSession();
+
+ const user = session?.user;
+
+ return (
+ <div className="fixed bg-gray-100 top-0 bottom-0 left-0 w-80">
+ <p>{user?.name}</p>
+ </div>
+ );
+}
diff --git a/hooks/useRedirection.tsx b/hooks/useRedirection.tsx
new file mode 100644
index 00000000..f5d91ba6
--- /dev/null
+++ b/hooks/useRedirection.tsx
@@ -0,0 +1,30 @@
+import { ReactNode, useEffect, useState } from "react";
+import { useSession } from "next-auth/react";
+import { useRouter } from "next/router";
+
+export default function useRedirection() {
+ const router = useRouter();
+ const { status } = useSession();
+ const [redirect, setRedirect] = useState(true);
+
+ useEffect(() => {
+ if (
+ status === "authenticated" &&
+ (router.pathname === "/login" || router.pathname === "/register")
+ ) {
+ router.push("/").then(() => {
+ setRedirect(false);
+ });
+ } else if (
+ status === "unauthenticated" &&
+ !(router.pathname === "/login" || router.pathname === "/register")
+ ) {
+ router.push("/login").then(() => {
+ setRedirect(false);
+ });
+ } else if (status === "loading") setRedirect(true);
+ else setRedirect(false);
+ }, [status]);
+
+ return redirect;
+}
diff --git a/middleware.ts b/middleware.ts
deleted file mode 100644
index 0f4fc8ca..00000000
--- a/middleware.ts
+++ /dev/null
@@ -1,3 +0,0 @@
-export { default } from "next-auth/middleware";
-
-export const config = { matcher: ["/", "/dashboard"] };
diff --git a/pages/_app.tsx b/pages/_app.tsx
index cfb96307..4101ff05 100644
--- a/pages/_app.tsx
+++ b/pages/_app.tsx
@@ -1,17 +1,14 @@
+import MainLayout from "@/components/Layouts/MainLayout";
import "@/styles/globals.css";
import { SessionProvider } from "next-auth/react";
import type { AppProps } from "next/app";
-import Head from "next/head";
export default function App({ Component, pageProps }: AppProps) {
return (
<SessionProvider session={pageProps.session}>
- <Head>
- <title>Linkwarden</title>
- <meta name="viewport" content="width=device-width, initial-scale=1" />
- <link rel="icon" href="/favicon.ico" />
- </Head>
- <Component {...pageProps} />
+ <MainLayout>
+ <Component {...pageProps} />
+ </MainLayout>
</SessionProvider>
);
}
diff --git a/pages/dashboard.tsx b/pages/dashboard.tsx
index 5a255a1a..6a953c9a 100644
--- a/pages/dashboard.tsx
+++ b/pages/dashboard.tsx
@@ -6,15 +6,9 @@ export default function Dashboard() {
const user = session?.user;
- console.log();
return (
<div className="p-5">
- <div>
- <p className="text-3xl font-bold text-center mb-10">Linkwarden</p>
- </div>
- <div>
- <p>Welcome {user?.name?.toLocaleUpperCase()}!</p>
- </div>
+ <p className="text-3xl font-bold text-center mb-10">Linkwarden</p>
<Collections />
</div>
);
diff --git a/pages/login.tsx b/pages/login.tsx
index f4d568a9..7b499a76 100644
--- a/pages/login.tsx
+++ b/pages/login.tsx
@@ -1,8 +1,6 @@
import { signIn } from "next-auth/react";
import Link from "next/link";
-import { useEffect, useState } from "react";
-import { useSession } from "next-auth/react";
-import { useRouter } from "next/router";
+import { useState } from "react";
interface FormData {
email: string;
@@ -10,16 +8,6 @@ interface FormData {
}
export default function Login() {
- const session = useSession();
- const router = useRouter();
-
- useEffect(() => {
- if (session.status === "authenticated") {
- console.log("Already logged in.");
- router.push("/");
- }
- }, []);
-
const [form, setForm] = useState<FormData>({
email: "",
password: "",
@@ -40,7 +28,7 @@ export default function Login() {
password: "",
});
- router.push("/");
+ // router.push("/");
} else {
console.log("User not found or password does not match.", res);
}
@@ -72,7 +60,7 @@ export default function Login() {
>
Login
</div>
- <Link href={"/auth/register"} className="block mx-auto w-min">
+ <Link href={"/register"} className="block mx-auto w-min">
Register
</Link>
</div>
diff --git a/pages/register.tsx b/pages/register.tsx
index de5aa6f9..2da7f4a5 100644
--- a/pages/register.tsx
+++ b/pages/register.tsx
@@ -1,6 +1,5 @@
import Link from "next/link";
-import { useEffect, useState } from "react";
-import { useSession } from "next-auth/react";
+import { useState } from "react";
import { useRouter } from "next/router";
interface FormData {
@@ -10,16 +9,8 @@ interface FormData {
}
export default function Register() {
- const session = useSession();
const router = useRouter();
- useEffect(() => {
- if (session.status === "authenticated") {
- console.log("Already logged in.");
- router.push("/");
- }
- }, [session]);
-
const [form, setForm] = useState<FormData>({
name: "",
email: "",
@@ -51,7 +42,7 @@ export default function Register() {
password: "",
});
- router.push("/auth/login");
+ router.push("/login");
}
} else {
console.log("Please fill out all the fields.");
@@ -88,7 +79,7 @@ export default function Register() {
>
Register
</div>
- <Link href={"/auth/login"} className="block mx-auto w-min">
+ <Link href={"/login"} className="block mx-auto w-min">
Login
</Link>
</div>