From c4df4e9c37931eb3dc354a929a4cbcb651edb811 Mon Sep 17 00:00:00 2001 From: d-ivashchuk Date: Sun, 7 Apr 2024 12:55:55 +0200 Subject: [PATCH] move application structure to /app route --- src/app/(landing)/page.tsx | 2 +- .../(lemon-squeezy)/ls-setup/page.tsx | 0 .../(lemon-squeezy)/subscriptions/page.tsx | 2 +- .../(user-scope)/billing/page.tsx | 0 .../(authenticated-routes)/usage/page.tsx | 0 .../user-management/page.tsx | 0 src/app/app/layout.tsx | 35 +++++++++++++++++++ src/app/app/login/page.tsx | 21 +++++++++++ src/app/layout.tsx | 32 ++--------------- src/app/login/page.tsx | 12 ------- src/components/patterns/layout.tsx | 25 +++++++------ .../patterns/login-logout-button.tsx | 2 +- src/components/patterns/login-screen.tsx | 4 +-- src/components/patterns/splash-screen.tsx | 13 ++----- 14 files changed, 80 insertions(+), 68 deletions(-) rename src/app/{ => app}/(authenticated-routes)/(lemon-squeezy)/ls-setup/page.tsx (100%) rename src/app/{ => app}/(authenticated-routes)/(lemon-squeezy)/subscriptions/page.tsx (98%) rename src/app/{ => app}/(authenticated-routes)/(user-scope)/billing/page.tsx (100%) rename src/app/{ => app}/(authenticated-routes)/usage/page.tsx (100%) rename src/app/{ => app}/(super-admin-routes)/user-management/page.tsx (100%) create mode 100644 src/app/app/layout.tsx create mode 100644 src/app/app/login/page.tsx delete mode 100644 src/app/login/page.tsx diff --git a/src/app/(landing)/page.tsx b/src/app/(landing)/page.tsx index 3f008c7..6e847bd 100644 --- a/src/app/(landing)/page.tsx +++ b/src/app/(landing)/page.tsx @@ -98,7 +98,7 @@ export default function Home() {
- + diff --git a/src/app/(authenticated-routes)/(lemon-squeezy)/ls-setup/page.tsx b/src/app/app/(authenticated-routes)/(lemon-squeezy)/ls-setup/page.tsx similarity index 100% rename from src/app/(authenticated-routes)/(lemon-squeezy)/ls-setup/page.tsx rename to src/app/app/(authenticated-routes)/(lemon-squeezy)/ls-setup/page.tsx diff --git a/src/app/(authenticated-routes)/(lemon-squeezy)/subscriptions/page.tsx b/src/app/app/(authenticated-routes)/(lemon-squeezy)/subscriptions/page.tsx similarity index 98% rename from src/app/(authenticated-routes)/(lemon-squeezy)/subscriptions/page.tsx rename to src/app/app/(authenticated-routes)/(lemon-squeezy)/subscriptions/page.tsx index d82419b..e5e612b 100644 --- a/src/app/(authenticated-routes)/(lemon-squeezy)/subscriptions/page.tsx +++ b/src/app/app/(authenticated-routes)/(lemon-squeezy)/subscriptions/page.tsx @@ -82,7 +82,7 @@ const Subscriptions = () => { {userSubscriptionsQuery.data?.subscription ? (

You are already subscribed. View your subscription{" "} - + here

diff --git a/src/app/(authenticated-routes)/(user-scope)/billing/page.tsx b/src/app/app/(authenticated-routes)/(user-scope)/billing/page.tsx similarity index 100% rename from src/app/(authenticated-routes)/(user-scope)/billing/page.tsx rename to src/app/app/(authenticated-routes)/(user-scope)/billing/page.tsx diff --git a/src/app/(authenticated-routes)/usage/page.tsx b/src/app/app/(authenticated-routes)/usage/page.tsx similarity index 100% rename from src/app/(authenticated-routes)/usage/page.tsx rename to src/app/app/(authenticated-routes)/usage/page.tsx diff --git a/src/app/(super-admin-routes)/user-management/page.tsx b/src/app/app/(super-admin-routes)/user-management/page.tsx similarity index 100% rename from src/app/(super-admin-routes)/user-management/page.tsx rename to src/app/app/(super-admin-routes)/user-management/page.tsx diff --git a/src/app/app/layout.tsx b/src/app/app/layout.tsx new file mode 100644 index 0000000..5d16bbe --- /dev/null +++ b/src/app/app/layout.tsx @@ -0,0 +1,35 @@ +"use client"; + +import { useSession } from "next-auth/react"; +import { usePathname, redirect } from "next/navigation"; +import { useEffect } from "react"; +import SplashScreen from "~/components/patterns/splash-screen"; + +const ProtectedRoutes = ({ children }: { children: React.ReactNode }) => { + const { status } = useSession(); + const pathname = usePathname(); + + useEffect(() => { + const allowedUnauthenticatedPaths = ["/app/login", "/"]; + + if ( + status !== "loading" && + status !== "authenticated" && + !allowedUnauthenticatedPaths.includes(pathname) + ) { + redirect("/app/login"); + } + }, [status, pathname]); + + if (status === "loading" && pathname.includes("/app")) + return ; + return <>{children}; +}; + +export default function RootLayout({ + children, +}: { + children: React.ReactNode; +}) { + return {children}; +} diff --git a/src/app/app/login/page.tsx b/src/app/app/login/page.tsx new file mode 100644 index 0000000..e18b3c7 --- /dev/null +++ b/src/app/app/login/page.tsx @@ -0,0 +1,21 @@ +import React from "react"; + +import { redirect } from "next/navigation"; +import { LoginScreen } from "~/components/patterns/login-screen"; +import { getServerAuthSession } from "~/server/auth"; + +const LoginPage = async () => { + const session = await getServerAuthSession(); + + if (session?.user) { + redirect("/app/usage"); + } + + return ( +
+ +
+ ); +}; + +export default LoginPage; diff --git a/src/app/layout.tsx b/src/app/layout.tsx index fdcee33..4c2cc25 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -6,11 +6,9 @@ import { Inter } from "next/font/google"; import { Layout } from "~/components/patterns/layout"; import Providers from "~/components/providers"; -import { Suspense, useEffect } from "react"; +import { Suspense } from "react"; import Script from "next/script"; -import { useSession } from "next-auth/react"; -import { redirect, usePathname } from "next/navigation"; -import SplashScreen from "~/components/patterns/splash-screen"; + import { Toaster } from "~/components/ui/sonner"; import { TailwindIndicator } from "~/components/patterns/tailwind-indicator"; @@ -19,28 +17,6 @@ const inter = Inter({ variable: "--font-sans", }); - - -const ProtectedRoutes = ({ children }: { children: React.ReactNode }) => { - const { status } = useSession(); - const pathname = usePathname(); - - useEffect(() => { - const allowedUnauthenticatedPaths = ["/login", "/"]; - - if ( - status !== "loading" && - status !== "authenticated" && - !allowedUnauthenticatedPaths.includes(pathname) - ) { - redirect("/login"); - } - }, [status, pathname]); - - if (status === "loading" && pathname !== "/") return ; - return <>{children}; -}; - export default function RootLayout({ children, }: { @@ -55,9 +31,7 @@ export default function RootLayout({ /> - - {children} - + {children} diff --git a/src/app/login/page.tsx b/src/app/login/page.tsx deleted file mode 100644 index d16ba79..0000000 --- a/src/app/login/page.tsx +++ /dev/null @@ -1,12 +0,0 @@ -import React from "react"; -import { LoginScreen } from "~/components/patterns/login-screen"; - -const LoginPage = () => { - return ( -
- -
- ); -}; - -export default LoginPage; diff --git a/src/components/patterns/layout.tsx b/src/components/patterns/layout.tsx index 3f49e49..cf8b729 100644 --- a/src/components/patterns/layout.tsx +++ b/src/components/patterns/layout.tsx @@ -20,6 +20,9 @@ import { usePathname } from "next/navigation"; export async function Layout({ children }: { children: React.ReactNode }) { const pathname = usePathname(); + + const isInApplicationRoute = pathname.includes("/app"); + return (
@@ -33,28 +36,28 @@ export async function Layout({ children }: { children: React.ReactNode }) { /> - {pathname === "/" ? ( + {isInApplicationRoute ? (