Skip to content

Commit

Permalink
feat: better auth routes organisation
Browse files Browse the repository at this point in the history
  • Loading branch information
ixahmedxi committed May 26, 2024
1 parent 27dd6a0 commit 7580f4a
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 27 deletions.
20 changes: 20 additions & 0 deletions src/app/(dashboard)/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { TRPCReactProvider } from '@/lib/trpc/react';
import { ClerkProvider } from '@clerk/nextjs';
import { dark } from '@clerk/themes';
import type { PropsWithChildren } from 'react';

/**
* The layout of the dashboard routes, this supplies the clerk & trpc providers mainly.
* @param props The props of the layout.
* @param props.children The children of the layout, which are all the routes under this route group.
* @returns A react component.
*/
export default function DashboardLayout({ children }: PropsWithChildren) {
return (
<ClerkProvider
appearance={{ baseTheme: dark, variables: { colorPrimary: '#F9617B' } }}
>
<TRPCReactProvider>{children}</TRPCReactProvider>
</ClerkProvider>
);
}
11 changes: 1 addition & 10 deletions src/app/(marketing)/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import Link from 'next/link';
import { redirect } from 'next/navigation';

import { currentUser } from '@clerk/nextjs/server';
import { ChevronRightIcon, StarIcon } from 'lucide-react';

import { constants } from '@/constants';
Expand All @@ -13,13 +10,7 @@ import { HomePreview } from './_components/home-preview';
* The marketing home page.
* @returns A react component representing the marketing home page.
*/
export default async function Home() {
const session = await currentUser();

if (session) {
redirect('/app');
}

export default function Home() {
return (
<main className="flex flex-col items-center justify-center gap-6 pt-24">
<Button variant="outline" asChild className="rounded-full font-normal">
Expand Down
27 changes: 10 additions & 17 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import type { Metadata } from 'next';

import { ClerkProvider } from '@clerk/nextjs';
import { dark } from '@clerk/themes';
import { GeistMono } from 'geist/font/mono';
import { GeistSans } from 'geist/font/sans';

Expand All @@ -12,7 +10,6 @@ import { ThemeProvider } from 'next-themes';
import type { PropsWithChildren } from 'react';

import { constructMetadata } from '@/lib/utils';
import { TRPCReactProvider } from '@/lib/trpc/react';

export const metadata: Metadata = constructMetadata();

Expand All @@ -25,20 +22,16 @@ export const metadata: Metadata = constructMetadata();
*/
export default function RootLayout({ children }: PropsWithChildren) {
return (
<ClerkProvider
appearance={{ baseTheme: dark, variables: { colorPrimary: '#F9617B' } }}
<html
lang="en"
suppressHydrationWarning
className={`${GeistSans.variable} ${GeistMono.variable}`}
>
<html
lang="en"
suppressHydrationWarning
className={`${GeistSans.variable} ${GeistMono.variable}`}
>
<body>
<ThemeProvider attribute="class" disableTransitionOnChange>
<TRPCReactProvider>{children}</TRPCReactProvider>
</ThemeProvider>
</body>
</html>
</ClerkProvider>
<body>
<ThemeProvider attribute="class" disableTransitionOnChange>
{children}
</ThemeProvider>
</body>
</html>
);
}
8 changes: 8 additions & 0 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import { clerkMiddleware, createRouteMatcher } from '@clerk/nextjs/server';
import { NextResponse } from 'next/server';

const isProtectedRoute = createRouteMatcher(['/app(.*)']);

export default clerkMiddleware((auth, req) => {
if (isProtectedRoute(req)) {
auth().protect();
}

// if the user is authenticated and is trying to access anything other than the dashboard, redirect them to the dashboard
if (auth().userId && !isProtectedRoute(req)) {
return NextResponse.redirect(new URL(`${req.nextUrl.origin}/app`));
}

return NextResponse.next();
});

export const config = {
Expand Down

0 comments on commit 7580f4a

Please sign in to comment.