Skip to content

Commit b9474bb

Browse files
fix(auth): refactor user authentication checks to use getAuthUserIdOrNull function
1 parent 0caa018 commit b9474bb

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

src/app/actions/authActions.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,23 @@ export async function getUserByEmail(email: string){
130130
return prisma.user.findUnique({where: {email}});
131131
}
132132

133+
export async function getAuthUserIdOrNull() {
134+
const session = await auth();
135+
const userId = session?.user?.id;
136+
137+
if (!userId) return null;
138+
139+
const existingUser = await prisma.user.findUnique({
140+
where: { id: userId },
141+
select: { id: true }
142+
});
143+
144+
return existingUser?.id ?? null;
145+
}
146+
133147

134148
export async function getAuthUserId(){
135-
const session = await auth();
136-
const userId = session?.user?.id;
149+
const userId = await getAuthUserIdOrNull();
137150

138151
if (!userId) throw new Error("Unauthorized");
139152
return userId;

src/app/lists/page.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
fetchLikeMembers,
55
} from "../actions/likeActions";
66
import ListsTab from "./ListsTab";
7-
import { auth } from "@/auth";
7+
import { getAuthUserIdOrNull } from "../actions/authActions";
88
import { redirect } from "next/navigation";
99

1010
export const dynamic = "force-dynamic";
@@ -14,9 +14,9 @@ export default async function ListsPage({
1414
}: {
1515
searchParams: Promise<{ type?: string }>;
1616
}) {
17-
const session = await auth();
17+
const userId = await getAuthUserIdOrNull();
1818

19-
if (!session?.user?.id) {
19+
if (!userId) {
2020
redirect("/login");
2121
}
2222

src/app/members/page.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ import PaginationComponent from '@/components/PaginationComponent';
66
import Filters from '@/components/navbar/Filters';
77
import { GetMemberParams } from '@/types';
88
import EmptyState from '@/components/EmptyState';
9-
import { auth } from '@/auth';
9+
import { getAuthUserIdOrNull } from '../actions/authActions';
1010
import { redirect } from 'next/navigation';
1111

1212
export default async function MembersPage({searchParams,}:{
1313
searchParams: Promise<GetMemberParams>;
1414
}) {
15-
const session = await auth();
15+
const userId = await getAuthUserIdOrNull();
1616

17-
if (!session?.user?.id) {
17+
if (!userId) {
1818
redirect('/login');
1919
}
2020

0 commit comments

Comments
 (0)