Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions apps/web/app/dashboard/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use client';

import { useRouter } from 'next/navigation';
import { useEffect, type ReactNode } from 'react';
import { Sidebar } from '../../components/Sidebar';
import { useAuth } from '../../lib/auth-context';

export default function DashboardLayout({ children }: { children: ReactNode }) {
const { user, isLoading } = useAuth();
const router = useRouter();

useEffect(() => {
if (!isLoading && !user) {
router.replace('/login');
}
}, [isLoading, user, router]);

if (isLoading || !user) return null;

return (
Comment on lines +12 to +20

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Auth guard destructs user from useAuth() but ignores isLoading. On first render user is null (localStorage hasn't been read yet), so the guard fires router.replace('/login') immediately — an authenticated user gets bounced to login and back in a flash. The fix is to wait until isLoading === false before evaluating user.

<div style={{ display: 'flex', minHeight: '100vh' }}>
<Sidebar />
<main style={{ flex: 1, padding: '2rem' }}>{children}</main>
</div>
);
}
8 changes: 8 additions & 0 deletions apps/web/app/dashboard/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default function DashboardPage() {
return (
<div>
<h1 style={{ fontWeight: 700, fontSize: '1.5rem', marginBottom: '0.5rem' }}>Dashboard</h1>
<p style={{ color: '#6b7280' }}>Welcome back. More stats coming soon.</p>
</div>
);
}
70 changes: 70 additions & 0 deletions apps/web/components/Sidebar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
'use client';

import Link from 'next/link';
import { usePathname } from 'next/navigation';
import { useAuth } from '../lib/auth-context';

const NAV_LINKS = [
{ href: '/dashboard', label: 'Dashboard' },
{ href: '/dashboard/restaurants', label: 'Restaurants' },
{ href: '/dashboard/menu', label: 'Menu' },
{ href: '/dashboard/orders', label: 'Orders' },
{ href: '/dashboard/settings', label: 'Settings' },
] as const;

export function Sidebar() {
const pathname = usePathname();
const { user, logout } = useAuth();

return (
<aside
style={{
width: '220px',
minHeight: '100vh',
borderRight: '1px solid #e5e7eb',
display: 'flex',
flexDirection: 'column',
padding: '1.5rem 1rem',
gap: '0.25rem',
}}
>
<p style={{ fontWeight: 700, fontSize: '1.1rem', marginBottom: '1rem' }}>Discoverly</p>

<nav style={{ display: 'flex', flexDirection: 'column', gap: '0.25rem', flex: 1 }}>
{NAV_LINKS.map(({ href, label }) => {
const isActive = pathname === href;
return (
<Link
key={href}
href={href}
aria-current={isActive ? 'page' : undefined}
style={{
padding: '0.5rem 0.75rem',
borderRadius: '6px',
textDecoration: 'none',
fontWeight: isActive ? 600 : 400,
background: isActive ? '#f3f4f6' : 'transparent',
color: '#111827',
}}
>
{label}
</Link>
);
})}
</nav>

{user && (
<div style={{ borderTop: '1px solid #e5e7eb', paddingTop: '1rem' }}>
<p style={{ fontSize: '0.8rem', color: '#6b7280', marginBottom: '0.5rem' }}>{user.email}</p>
<button
type="button"
onClick={logout}
style={{ fontSize: '0.85rem', cursor: 'pointer', background: 'none', border: 'none', color: '#ef4444' }}
>
Sign out
</button>
</div>
)}
</aside>
);
}
Loading