Skip to content

Commit

Permalink
redirect on / to login or dashboard dep on session
Browse files Browse the repository at this point in the history
  • Loading branch information
psiddharthdesign committed Jul 29, 2024
1 parent 301b554 commit 9de9523
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
5 changes: 0 additions & 5 deletions src/app/(external-pages)/page.tsx

This file was deleted.

4 changes: 2 additions & 2 deletions src/app/logout/page.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
'use client';
import { T } from '@/components/ui/Typography';
import { supabaseUserClientComponentClient } from '@/supabase-clients/user/supabaseUserClientComponentClient';
import { useDidMount } from 'rooks';
import { useRouter } from 'next/navigation';
import { useDidMount } from 'rooks';

export default function Logout() {
const router = useRouter();
useDidMount(async () => {
await supabaseUserClientComponentClient.auth.signOut();
router.refresh();
router.replace('/');
router.replace('/login');
});

return <T.P>Signing out...</T.P>;
Expand Down
33 changes: 33 additions & 0 deletions src/app/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { createSupabaseUserRouteHandlerClient } from '@/supabase-clients/user/createSupabaseUserRouteHandlerClient';
import { NextResponse } from 'next/server';

export const dynamic = 'force-dynamic';

export async function GET() {
const supabase = createSupabaseUserRouteHandlerClient();

const {
data: { user },
error,
} = await supabase.auth.getUser();

if (error) {
console.error('Error checking user authentication:', error);
// In case of an error, redirect to login as a fallback
return NextResponse.redirect(
new URL('/login', process.env.NEXT_PUBLIC_SITE_URL),
);
}

if (user) {
// User is logged in, redirect to dashboard
return NextResponse.redirect(
new URL('/dashboard', process.env.NEXT_PUBLIC_SITE_URL),
);
} else {
// User is not logged in, redirect to login
return NextResponse.redirect(
new URL('/login', process.env.NEXT_PUBLIC_SITE_URL),
);
}
}

0 comments on commit 9de9523

Please sign in to comment.