Skip to content

Commit

Permalink
feat: implement sign out button and redirect to sign in page when log…
Browse files Browse the repository at this point in the history
…ging out
  • Loading branch information
ixartz committed Aug 10, 2023
1 parent c663d1c commit 45ed137
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
9 changes: 1 addition & 8 deletions src/app/(auth)/dashboard/layout.tsx
Expand Up @@ -44,14 +44,7 @@ export default function DashboardLayout({
<nav>
<ul className="flex flex-wrap text-xl">
<li className="mr-6">
<SignOutButton>
<button
className="border-none text-gray-700 hover:text-gray-900"
type="button"
>
Sign out
</button>
</SignOutButton>
<SignOutButton />
</li>
</ul>
</nav>
Expand Down
17 changes: 17 additions & 0 deletions src/components/SignOutButton.tsx
@@ -0,0 +1,17 @@
'use client';

import { SignOutButton as SignOutButtonClerk } from '@clerk/nextjs';
import { redirect } from 'next/navigation';

const SignOutButton = () => (
<SignOutButtonClerk signOutCallback={() => redirect('/')}>
<button
className="border-none text-gray-700 hover:text-gray-900"
type="button"
>
Sign out
</button>
</SignOutButtonClerk>
);

export { SignOutButton };
11 changes: 9 additions & 2 deletions src/middleware.ts
@@ -1,9 +1,16 @@
import { authMiddleware } from '@clerk/nextjs';
import type { NextRequest } from 'next/server';
import { authMiddleware, redirectToSignIn } from '@clerk/nextjs';
import { type NextRequest } from 'next/server';

export default authMiddleware({
publicRoutes: (req: NextRequest) =>
!req.nextUrl.pathname.startsWith('/dashboard'),
// eslint-disable-next-line consistent-return
afterAuth(auth, req) {
// handle users who aren't authenticated
if (!auth.userId && !auth.isPublicRoute) {
return redirectToSignIn({ returnBackUrl: req.url });
}
},
});

export const config = {
Expand Down

0 comments on commit 45ed137

Please sign in to comment.