Skip to content

Commit

Permalink
feat: move authorization to individual pages in app so unauthenticate…
Browse files Browse the repository at this point in the history
…d users can view profile
  • Loading branch information
dinkelspiel committed May 11, 2024
1 parent 344e1f0 commit ec96dba
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 38 deletions.
5 changes: 5 additions & 0 deletions app/(app)/dashboard/page.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { validateSessionToken } from '@/server/auth/validateSession';
import prisma from '@/server/db';
import Dashboard from './client';
import { redirect } from 'next/navigation';

const Page = async () => {
const user = await validateSessionToken();

if (!user) {
return redirect('/auth/login');
}

const userEntries = await prisma.userEntry.findMany({
where: {
userId: user?.id,
Expand Down
77 changes: 41 additions & 36 deletions app/(app)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,53 @@ import { Toaster } from 'sonner';
const Layout = async ({ children }: { children: ReactNode }) => {
const user = await validateSessionToken();

if (!user) {
return redirect('/auth/login');
}

return (
<SidebarLayout>
<Sidebar
header={
<div className="flex w-full justify-between">
<UserDisplay user={user} />
if (user) {
return (
<SidebarLayout>
<Sidebar
header={
<div className="flex w-full justify-between">
<UserDisplay user={user} />
<AddLog>
<Button className="flex min-w-[40px] justify-center lg:hidden">
<Plus />
Log Media
</Button>
</AddLog>
</div>
}
headerProps={{ className: '[&>svg]:size-7 p-0' }}
>
<SidebarButton href="/dashboard">
<Home className="size-3" />
Home
</SidebarButton>
<SidebarButton href="/community">
<UsersRound className="size-3" />
Community
</SidebarButton>
<SidebarFooter>
<AddLog>
<Button className="flex min-w-[40px] justify-center lg:hidden">
<Button className="hidden lg:flex">
<Plus />
Log Media
</Button>
</AddLog>
</div>
}
headerProps={{ className: '[&>svg]:size-7 p-0' }}
>
<SidebarButton href="/dashboard">
<Home className="size-3" />
Home
</SidebarButton>
<SidebarButton href="/community">
<UsersRound className="size-3" />
Community
</SidebarButton>
<SidebarFooter>
<AddLog>
<Button className="hidden lg:flex">
<Plus />
Log Media
</Button>
</AddLog>
</SidebarFooter>
</Sidebar>
</SidebarFooter>
</Sidebar>

<main className="flex flex-col gap-4 px-5 py-4">{children}</main>
<Toaster />
</SidebarLayout>
);
<main className="flex flex-col gap-4 px-5 py-4">{children}</main>
<Toaster />
</SidebarLayout>
);
} else {
return (
<>
<main className="flex flex-col gap-4 px-5 py-4">{children}</main>
<Toaster />
</>
);
}
};

export default Layout;
2 changes: 0 additions & 2 deletions app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import '@/styles/globals.css';
import BaseLayout from '@/components/layouts/base';

export default function RootLayout({
// Layouts must accept a children prop.
// This will be populated with nested layouts or pages
children,
}: {
children: React.ReactNode;
Expand Down

0 comments on commit ec96dba

Please sign in to comment.