Skip to content

Connect, collaborate, and grow with a community of TypeScript developers

Fixed Session type, user prop is optional, updated code (#1230)

Junsu Park2ab6d85eecc6
diff --git a/apps/admin/src/app/dashboard/reports/[id]/_actions.ts b/apps/admin/src/app/dashboard/reports/[id]/_actions.ts
index a4871537..85ec6895 100644
--- a/apps/admin/src/app/dashboard/reports/[id]/_actions.ts
+++ b/apps/admin/src/app/dashboard/reports/[id]/_actions.ts
@@ -92,7 +92,7 @@ export async function banChallenge(challengeId: number, reportId: number) {
},
data: {
status: 'CLEARED',
- moderatorId: session?.user.id,
+ moderatorId: session?.user?.id,
updatedAt: new Date(),
},
}),
@@ -113,7 +113,7 @@ export async function dismissReport(reportId: number) {
},
data: {
status: 'DISMISSED',
- moderatorId: session?.user.id,
+ moderatorId: session?.user?.id,
updatedAt: new Date(),
},
});
@@ -184,7 +184,7 @@ export async function banUser(userId: string, reportId: number, banReason?: stri
},
data: {
status: 'CLEARED',
- moderatorId: session?.user.id,
+ moderatorId: session?.user?.id,
updatedAt: new Date(),
},
}),
diff --git a/apps/admin/src/app/dashboard/users/_actions.ts b/apps/admin/src/app/dashboard/users/_actions.ts
index e75ee2d0..4bb971b1 100644
--- a/apps/admin/src/app/dashboard/users/_actions.ts
+++ b/apps/admin/src/app/dashboard/users/_actions.ts
@@ -51,7 +51,7 @@ export async function banUser(userId: string, reportId: number, banReason?: stri
},
data: {
status: 'CLEARED',
- moderatorId: session?.user.id,
+ moderatorId: session?.user?.id,
updatedAt: new Date(),
},
}),
diff --git a/apps/admin/src/app/page.tsx b/apps/admin/src/app/page.tsx
index 7b45c861..dda12f43 100644
--- a/apps/admin/src/app/page.tsx
+++ b/apps/admin/src/app/page.tsx
@@ -5,7 +5,7 @@ import { redirect } from 'next/navigation';
async function Admin() {
const session = await auth();
- const roles = session?.user.role ?? [];
+ const roles = session?.user?.role ?? [];
const isMod = roles.includes(RoleTypes.MODERATOR);
const isAdmin = roles.includes(RoleTypes.ADMIN);
diff --git a/apps/admin/src/utils/auth-guards.ts b/apps/admin/src/utils/auth-guards.ts
index 2b433843..192dc526 100644
--- a/apps/admin/src/utils/auth-guards.ts
+++ b/apps/admin/src/utils/auth-guards.ts
@@ -2,7 +2,7 @@ import type { Session } from '@repo/auth/server';
import { RoleTypes } from '@repo/db/types';
export const assertAdmin = (session: Session | null) => {
- const isAdmin = session?.user.role.includes(RoleTypes.ADMIN);
+ const isAdmin = session?.user?.role.includes(RoleTypes.ADMIN);
if (!isAdmin) {
throw new Error('You are not authorized to perform this action.');
diff --git a/apps/web/src/app/[locale]/(profile)/[username]/_components/profile.tsx b/apps/web/src/app/[locale]/(profile)/[username]/_components/profile.tsx
index 531cc8d7..ae23a5dc 100644
--- a/apps/web/src/app/[locale]/(profile)/[username]/_components/profile.tsx
+++ b/apps/web/src/app/[locale]/(profile)/[username]/_components/profile.tsx
@@ -38,7 +38,7 @@ export async function Profile({ username: usernameFromQuery, children }: Props)
if (!user) return notFound();
const session = await auth();
- const isOwnProfile = session?.user.id === user.id;
+ const isOwnProfile = session?.user?.id === user.id;
const badges = await getBadges(user.id);
diff --git a/apps/web/src/app/[locale]/(profile)/[username]/bookmarks/page.tsx b/apps/web/src/app/[locale]/(profile)/[username]/bookmarks/page.tsx
index 7dae74a1..370bf241 100644
--- a/apps/web/src/app/[locale]/(profile)/[username]/bookmarks/page.tsx
+++ b/apps/web/src/app/[locale]/(profile)/[username]/bookmarks/page.tsx
@@ -23,7 +23,7 @@ export default async function BookmarksPage({ params: { username: usernameFromQu
const session = await auth();
const [, username] = decodeURIComponent(usernameFromQuery).split('@');
- if (!username || session?.user.name !== username) return notFound();
+ if (!username || session?.user?.name !== username) return notFound();
const user = await prisma.user.findFirst({
where: {
diff --git a/apps/web/src/app/[locale]/(profile)/[username]/completed/page.tsx b/apps/web/src/app/[locale]/(profile)/[username]/completed/page.tsx
index 8be6d1a3..a93232fe 100644
--- a/apps/web/src/app/[locale]/(profile)/[username]/completed/page.tsx
+++ b/apps/web/src/app/[locale]/(profile)/[username]/completed/page.tsx
@@ -23,7 +23,7 @@ export default async function CompletedPage({ params: { username: usernameFromQu
const session = await auth();
const [, username] = decodeURIComponent(usernameFromQuery).split('@');
- if (!username || session?.user.name !== username) return notFound();
+ if (!username || session?.user?.name !== username) return notFound();
const user = await prisma.user.findFirst({
where: {
diff --git a/apps/web/src/app/[locale]/(profile)/[username]/in-progress/page.tsx b/apps/web/src/app/[locale]/(profile)/[username]/in-progress/page.tsx
index 0167f164..cff06ff3 100644
--- a/apps/web/src/app/[locale]/(profile)/[username]/in-progress/page.tsx
+++ b/apps/web/src/app/[locale]/(profile)/[username]/in-progress/page.tsx
@@ -23,7 +23,7 @@ export default async function InProgressPage({ params: { username: usernameFromQ
const session = await auth();
const [, username] = decodeURIComponent(usernameFromQuery).split('@');
- if (!username || session?.user.name !== username) return notFound();
+ if (!username || session?.user?.name !== username) return notFound();
const user = await prisma.user.findFirst({
where: {
diff --git a/apps/web/src/app/[locale]/(profile)/[username]/shared-solutions/page.tsx b/apps/web/src/app/[locale]/(profile)/[username]/shared-solutions/page.tsx
index fd31d676..96b21e2d 100644
--- a/apps/web/src/app/[locale]/(profile)/[username]/shared-solutions/page.tsx
+++ b/apps/web/src/app/[locale]/(profile)/[username]/shared-solutions/page.tsx
@@ -48,7 +48,7 @@ export default async function SharedSolutionsPage({
});
const session = await auth();
- const isOwnProfile = session?.user.id === user.id;
+ const isOwnProfile = session?.user?.id === user.id;
return (
<Card className="col-span-4 md:min-h-[calc(100vh_-_56px_-_6rem)]">
diff --git a/apps/web/src/app/[locale]/aot-2023/_components/card-grid.tsx b/apps/web/src/app/[locale]/aot-2023/_components/card-grid.tsx
index 6f94ca29..52f8f5b6 100644
--- a/apps/web/src/app/[locale]/aot-2023/_components/card-grid.tsx
+++ b/apps/web/src/app/[locale]/aot-2023/_components/card-grid.tsx
@@ -32,7 +32,7 @@ async function getTrackChallenges(session: Session | null) {
select: {
submission: {
where: {
- userId: session?.user.id || '',
+ userId: session?.user?.id || '',
isSuccessful: true,
},
select: {
diff --git a/apps/web/src/app/[locale]/challenge/[slug]/getChallengeRouteData.ts b/apps/web/src/app/[locale]/challenge/[slug]/getChallengeRouteData.ts
index c487119e..4bf736d8 100644
--- a/apps/web/src/app/[locale]/challenge/[slug]/getChallengeRouteData.ts
+++ b/apps/web/src/app/[locale]/challenge/[slug]/getChallengeRouteData.ts
@@ -38,17 +38,17 @@ export const getChallengeRouteData = cache(async (slug: string, session: Session
},
vote: {
where: {
- userId: session?.user.id || '',
+ userId: session?.user?.id || '',
},
},
bookmark: {
where: {
- userId: session?.user.id || '',
+ userId: session?.user?.id || '',
},
},
submission: {
where: {
- userId: session?.user.id || '',
+ userId: session?.user?.id || '',
isSuccessful: true,
},
take: 1,
@@ -69,7 +69,7 @@ export const getChallengeRouteData = cache(async (slug: string, session: Session
track: {
enrolledUsers: {
some: {
- id: session.user.id,
+ id: session.user?.id,
},
},
},
diff --git a/apps/web/src/app/[locale]/challenge/[slug]/solutions/[solutionId]/getSolutionIdRouteData.ts b/apps/web/src/app/[locale]/challenge/[slug]/solutions/[solutionId]/getSolutionIdRouteData.ts
index 18bd41df..1c0b7625 100644
--- a/apps/web/src/app/[locale]/challenge/[slug]/solutions/[solutionId]/getSolutionIdRouteData.ts
+++ b/apps/web/src/app/[locale]/challenge/[slug]/solutions/[solutionId]/getSolutionIdRouteData.ts
@@ -26,7 +26,7 @@ export const getSolutionIdRouteData = cache(
},
vote: {
where: {
- userId: session?.user.id || '',
+ userId: session?.user?.id || '',
},
select: {
userId: true,
diff --git a/apps/web/src/app/[locale]/challenge/[slug]/solutions/_components/edit-solution.tsx b/apps/web/src/app/[locale]/challenge/[slug]/solutions/_components/edit-solution.tsx
index 181d8bd5..696d9ee1 100644
--- a/apps/web/src/app/[locale]/challenge/[slug]/solutions/_components/edit-solution.tsx
+++ b/apps/web/src/app/[locale]/challenge/[slug]/solutions/_components/edit-solution.tsx
@@ -48,7 +48,7 @@ export function EditSolution({ solution, setIsEditing }: Props) {
description: data.content ?? '',
slug: slug as string,
title: data.title ?? '',
- userId: session.data?.user.id!,
+ userId: session.data?.user?.id!,
});
toast({
diff --git a/apps/web/src/app/[locale]/challenge/[slug]/solutions/_components/solution-editor.tsx b/apps/web/src/app/[locale]/challenge/[slug]/solutions/_components/solution-editor.tsx
index 47f5c2af..e855a65c 100644
--- a/apps/web/src/app/[locale]/challenge/[slug]/solutions/_components/solution-editor.tsx
+++ b/apps/web/src/app/[locale]/challenge/[slug]/solutions/_components/solution-editor.tsx
@@ -51,7 +51,7 @@ export function SolutionEditor({ dismiss, challengeId, code }: Props) {
const form = useForm<FormSchema>({
resolver: zodResolver(formSchema),
defaultValues: {
- title: `${session.data?.user.name}'s Solution`,
+ title: `${session.data?.user?.name}'s Solution`,
content: getDefaultMarkdown(code ?? ''),
},
});
@@ -63,8 +63,8 @@ export function SolutionEditor({ dismiss, challengeId, code }: Props) {
challengeId,
description: data.content ?? '',
slug: slug as string,
- title: data.title ?? `${session.data?.user.name}'s Solution`,
- userId: session.data?.user.id!,
+ title: data.title ?? `${session.data?.user?.name}'s Solution`,
+ userId: session.data?.user?.id!,
});
toast({
diff --git a/apps/web/src/app/[locale]/challenge/[slug]/solutions/getSolutionRouteData.ts b/apps/web/src/app/[locale]/challenge/[slug]/solutions/getSolutionRouteData.ts
index f286fc44..1207d1ee 100644
--- a/apps/web/src/app/[locale]/challenge/[slug]/solutions/getSolutionRouteData.ts
+++ b/apps/web/src/app/[locale]/challenge/[slug]/solutions/getSolutionRouteData.ts
@@ -30,7 +30,7 @@ export async function getPaginatedSolutions({
id: true,
submission: {
where: {
- userId: session?.user.id || '',
+ userId: session?.user?.id || '',
isSuccessful: true,
},
orderBy: [
diff --git a/apps/web/src/app/[locale]/challenge/[slug]/submissions/[[...catchAll]]/page.tsx b/apps/web/src/app/[locale]/challenge/[slug]/submissions/[[...catchAll]]/page.tsx
index 796e1b7d..cb269627 100644
--- a/apps/web/src/app/[locale]/challenge/[slug]/submissions/[[...catchAll]]/page.tsx
+++ b/apps/web/src/app/[locale]/challenge/[slug]/submissions/[[...catchAll]]/page.tsx
@@ -28,7 +28,7 @@ export default async function SubmissionPage({ params: { slug } }: Props) {
const submissions = await withUnstableCache({
fn: getChallengeSubmissions,
- args: [session?.user.id ?? '', slug],
+ args: [session?.user?.id ?? '', slug],
keys: ['all-challenge-submissions'],
tags: [createChallengeSubmissionCacheKey(slug)],
});
diff --git a/apps/web/src/app/[locale]/challenge/_components/comments/comment.action.ts b/apps/web/src/app/[locale]/challenge/_components/comments/comment.action.ts
index 1507cadc..5e8ea66e 100644
--- a/apps/web/src/app/[locale]/challenge/_components/comments/comment.action.ts
+++ b/apps/web/src/app/[locale]/challenge/_components/comments/comment.action.ts
@@ -18,7 +18,7 @@ interface CommentToCreate {
export async function addComment(comment: CommentToCreate) {
const session = await auth();
- if (!session?.user.id) return 'unauthorized';
+ if (!session?.user?.id) return 'unauthorized';
if (comment.text.length === 0) return 'text_is_empty';
if (!session.user.id) return 'unauthorized';
if (comment.text.length === 0) return 'text_is_empty';
@@ -41,7 +41,7 @@ export async function addComment(comment: CommentToCreate) {
export async function replyComment(comment: CommentToCreate, parentId: number) {
const session = await auth();
- if (!session?.user.id) return 'unauthorized';
+ if (!session?.user?.id) return 'unauthorized';
if (comment.text.length === 0) return 'text_is_empty';
if (!session.user.id) return 'unauthorized';
if (comment.text.length === 0) return 'text_is_empty';
@@ -65,7 +65,7 @@ export async function replyComment(comment: CommentToCreate, parentId: number) {
export async function updateComment(text: string, id: number) {
const session = await auth();
- if (!session) return 'unauthorized';
+ if (!session || !session.user) return 'unauthorized';
if (text.length === 0) return 'text_is_empty';
const comment = await prisma.comment.findFirstOrThrow({
diff --git a/apps/web/src/app/[locale]/challenge/_components/comments/comment.tsx b/apps/web/src/app/[locale]/challenge/_components/comments/comment.tsx
index 2147beb5..b27d0954 100644
--- a/apps/web/src/app/[locale]/challenge/_components/comments/comment.tsx
+++ b/apps/web/src/app/[locale]/challenge/_components/comments/comment.tsx
@@ -239,7 +239,7 @@ function SingleComment({
const loggedinUser = useSession();
- const isAuthor = loggedinUser.data?.user.id === comment.user.id;
+ const isAuthor = loggedinUser.data?.user?.id === comment.user.id;
const isAdminAndModerator = isAdminOrModerator(loggedinUser.data);
const hasBeenEdited = comment.updatedAt.getTime() > comment.createdAt.getTime();
diff --git a/apps/web/src/app/[locale]/challenge/_components/comments/getCommentRouteData.ts b/apps/web/src/app/[locale]/challenge/_components/comments/getCommentRouteData.ts
index c718ef03..e011eda2 100644
--- a/apps/web/src/app/[locale]/challenge/_components/comments/getCommentRouteData.ts
+++ b/apps/web/src/app/[locale]/challenge/_components/comments/getCommentRouteData.ts
@@ -162,7 +162,7 @@ export async function getPaginatedComments({
userId: true,
},
where: {
- userId: session?.user.id || '',
+ userId: session?.user?.id || '',
},
},
rootChallenge: {
@@ -230,7 +230,7 @@ export async function getAllComments({
userId: true,
},
where: {
- userId: session?.user.id || '',
+ userId: session?.user?.id || '',
},
},
rootChallenge: {
diff --git a/apps/web/src/app/[locale]/challenge/_components/description/index.tsx b/apps/web/src/app/[locale]/challenge/_components/description/index.tsx
index 90555b95..0178c20b 100644
--- a/apps/web/src/app/[locale]/challenge/_components/description/index.tsx
+++ b/apps/web/src/app/[locale]/challenge/_components/description/index.tsx
@@ -146,7 +146,7 @@ export function Description({ challenge }: Props) {
'border-blue-500 text-blue-500': hasBookmarked,
},
)}
- disabled={!session.data?.user.id}
+ disabled={!session.data?.user?.id}
onClick={() => {
let shouldBookmark = false;
if (hasBookmarked) {
@@ -156,7 +156,7 @@ export function Description({ challenge }: Props) {
shouldBookmark = true;
setHasBookmarked(true);
}
- debouncedBookmark(challenge.id, session.data?.user.id!, shouldBookmark)?.catch(
+ debouncedBookmark(challenge.id, session.data?.user?.id!, shouldBookmark)?.catch(
(e) => {
console.error(e);
},
@@ -167,7 +167,7 @@ export function Description({ challenge }: Props) {
</Button>
</TooltipTrigger>
<TooltipContent>
- <p>{session.data?.user.id ? 'Bookmark' : 'Login to Bookmark'}</p>
+ <p>{session.data?.user?.id ? 'Bookmark' : 'Login to Bookmark'}</p>
</TooltipContent>
</Tooltip>
</div>
diff --git a/apps/web/src/app/[locale]/challenge/_components/vote.tsx b/apps/web/src/app/[locale]/challenge/_components/vote.tsx
index 5b254f05..70ad3b4c 100644
--- a/apps/web/src/app/[locale]/challenge/_components/vote.tsx
+++ b/apps/web/src/app/[locale]/challenge/_components/vote.tsx
@@ -79,7 +79,7 @@ export function Vote({
</Button>
</TooltipTrigger>
<TooltipContent>
- <p>{session.data?.user.id ? 'Upvote' : 'Login to Upvote'}</p>
+ <p>{session.data?.user?.id ? 'Upvote' : 'Login to Upvote'}</p>
</TooltipContent>
</Tooltip>
);
diff --git a/apps/web/src/app/[locale]/challenge/increment.action.ts b/apps/web/src/app/[locale]/challenge/increment.action.ts
index 05aca260..f67ce291 100644
--- a/apps/web/src/app/[locale]/challenge/increment.action.ts
+++ b/apps/web/src/app/[locale]/challenge/increment.action.ts
@@ -21,7 +21,7 @@ export async function incrementOrDecrementUpvote(
commentId: rootType === 'COMMENT' ? id : undefined,
sharedSolutionId: rootType === 'SHAREDSOLUTION' ? id : undefined,
submissionId: rootType === 'SUBMISSION' ? id : undefined,
- userId: session?.user.id ?? '',
+ userId: session?.user?.id ?? '',
},
});
diff --git a/apps/web/src/app/[locale]/claim/_actions.ts b/apps/web/src/app/[locale]/claim/_actions.ts
index ec7889b0..3b87ed0b 100644
--- a/apps/web/src/app/[locale]/claim/_actions.ts
+++ b/apps/web/src/app/[locale]/claim/_actions.ts
@@ -22,7 +22,7 @@ export async function validateToken(claimFormData: FormSchema) {
},
},
data: {
- user: { connect: { id: session?.user.id } },
+ user: { connect: { id: session?.user?.id } },
},
});
@@ -41,7 +41,7 @@ export async function isValidToken(session: Session, claimFormData: FormSchema)
},
},
data: {
- user: { connect: { id: session?.user.id } },
+ user: { connect: { id: session?.user?.id } },
},
});
return true;
diff --git a/apps/web/src/app/[locale]/explore/_components/explore.action.ts b/apps/web/src/app/[locale]/explore/_components/explore.action.ts
index 386a305b..31f03cae 100644
--- a/apps/web/src/app/[locale]/explore/_components/explore.action.ts
+++ b/apps/web/src/app/[locale]/explore/_components/explore.action.ts
@@ -46,7 +46,7 @@ export async function getChallengesByTagOrDifficulty(str: string, take?: number)
},
submission: {
where: {
- userId: session?.user.id || '',
+ userId: session?.user?.id || '',
isSuccessful: true,
},
take: 1,
diff --git a/apps/web/src/app/[locale]/settings/_components/settings.action.ts b/apps/web/src/app/[locale]/settings/_components/settings.action.ts
index 3253c86d..6d274cf4 100644
--- a/apps/web/src/app/[locale]/settings/_components/settings.action.ts
+++ b/apps/web/src/app/[locale]/settings/_components/settings.action.ts
@@ -14,7 +14,7 @@ export async function updateProfile(profileData: ProfileSchema) {
const session = await auth();
// 1. Checks that the user is logged in
- if (!session?.user.id) return { error: 'unauthorized' };
+ if (!session?.user?.id) return { error: 'unauthorized' };
// 2. test schema validation with zod
profileSchema.parse(profileData);
@@ -29,7 +29,7 @@ export async function updateProfile(profileData: ProfileSchema) {
const userLinksToCreate = profileData.userLinks.map((link) => ({
url: link.url,
user: {
- connect: { id: session.user.id },
+ connect: { id: session.user?.id },
},
}));
diff --git a/apps/web/src/app/[locale]/tracks/_components/track-enrolled-section.tsx b/apps/web/src/app/[locale]/tracks/_components/track-enrolled-section.tsx
index 1ebd4138..8e95ed75 100644
--- a/apps/web/src/app/[locale]/tracks/_components/track-enrolled-section.tsx
+++ b/apps/web/src/app/[locale]/tracks/_components/track-enrolled-section.tsx
@@ -67,7 +67,7 @@ async function getUserEnrolledTracks(session: Session) {
where: {
enrolledUsers: {
some: {
- id: session.user.id,
+ id: session.user?.id,
},
},
},
@@ -78,7 +78,7 @@ async function getUserEnrolledTracks(session: Session) {
include: {
submission: {
where: {
- userId: session.user.id,
+ userId: session.user?.id,
},
},
},
diff --git a/apps/web/src/app/[locale]/tracks/_components/track-grid.tsx b/apps/web/src/app/[locale]/tracks/_components/track-grid.tsx
index 08573a11..59e4d0f4 100644
--- a/apps/web/src/app/[locale]/tracks/_components/track-grid.tsx
+++ b/apps/web/src/app/[locale]/tracks/_components/track-grid.tsx
@@ -35,7 +35,7 @@ async function getTracks(session: Session | null) {
},
enrolledUsers: {
where: {
- id: session?.user.id ?? '',
+ id: session?.user?.id ?? '',
},
},
trackChallenges: {
@@ -44,7 +44,7 @@ async function getTracks(session: Session | null) {
include: {
submission: {
where: {
- userId: session?.user.id,
+ userId: session?.user?.id,
},
},
},
diff --git a/apps/web/src/app/[locale]/tracks/_components/track.action.ts b/apps/web/src/app/[locale]/tracks/_components/track.action.ts
index 98a21a29..f55cd382 100644
--- a/apps/web/src/app/[locale]/tracks/_components/track.action.ts
+++ b/apps/web/src/app/[locale]/tracks/_components/track.action.ts
@@ -23,7 +23,7 @@ export async function enrollUserInTrack(id: number, slug: string) {
data: {
enrolledUsers: {
connect: {
- id: session?.user.id,
+ id: session?.user?.id,
},
},
},
@@ -50,7 +50,7 @@ export async function unenrollUserFromTrack(id: number, slug: string) {
data: {
enrolledUsers: {
disconnect: {
- id: session?.user.id,
+ id: session?.user?.id,
},
},
},
@@ -80,7 +80,7 @@ export const getTrackDetails = cache(async (slug: string) => {
include: {
submission: {
where: {
- userId: session?.user.id ?? '',
+ userId: session?.user?.id ?? '',
},
},
user: {
@@ -94,7 +94,7 @@ export const getTrackDetails = cache(async (slug: string) => {
},
enrolledUsers: {
where: {
- id: session?.user.id ?? '',
+ id: session?.user?.id ?? '',
},
select: {
id: true,
@@ -114,7 +114,7 @@ export async function getUserEnrolledTracks(session: Session) {
where: {
enrolledUsers: {
some: {
- id: session.user.id,
+ id: session.user?.id,
},
},
},
@@ -125,7 +125,7 @@ export async function getUserEnrolledTracks(session: Session) {
include: {
submission: {
where: {
- userId: session.user.id,
+ userId: session.user?.id,
},
},
},
diff --git a/apps/web/src/app/[locale]/wizard/_components/create.action.ts b/apps/web/src/app/[locale]/wizard/_components/create.action.ts
index de46e13f..8b1b362d 100644
--- a/apps/web/src/app/[locale]/wizard/_components/create.action.ts
+++ b/apps/web/src/app/[locale]/wizard/_components/create.action.ts
@@ -7,7 +7,7 @@ import { type CreateChallengeSchema } from '.';
export async function uploadChallenge(data: CreateChallengeSchema, isUserACreator: boolean) {
const session = await auth();
- if (!session) {
+ if (!session || !session.user) {
throw new Error('Unauthorized');
}
diff --git a/apps/web/src/app/api/uploadthing/core.ts b/apps/web/src/app/api/uploadthing/core.ts
index ae59e9f9..e31cbbea 100644
--- a/apps/web/src/app/api/uploadthing/core.ts
+++ b/apps/web/src/app/api/uploadthing/core.ts
@@ -35,7 +35,7 @@ export const ourFileRouter = {
const session = await auth();
// If you throw, the user will not be able to upload
- if (!session?.user.id) throw new Error('Unauthorized');
+ if (!session?.user?.id) throw new Error('Unauthorized');
// Whatever is returned here is accessible in onUploadComplete as `metadata`
return { userId: session.user.id };
diff --git a/apps/web/src/components/Navigation/index.tsx b/apps/web/src/components/Navigation/index.tsx
index fc154d9a..75a25215 100644
--- a/apps/web/src/components/Navigation/index.tsx
+++ b/apps/web/src/components/Navigation/index.tsx
@@ -104,7 +104,7 @@ async function LoginButton() {
const isAdminOrMod = isAdminOrModerator(session);
- return session ? (
+ return session && session.user ? (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<button
diff --git a/apps/web/src/components/ReportDialog/report-dialog.action.ts b/apps/web/src/components/ReportDialog/report-dialog.action.ts
index 315638a1..0d876964 100644
--- a/apps/web/src/components/ReportDialog/report-dialog.action.ts
+++ b/apps/web/src/components/ReportDialog/report-dialog.action.ts
@@ -71,7 +71,7 @@ export async function addReport(
report: ChallengeReport | CommentReport | SolutionReport | UserReport,
) {
const reporter = await auth();
- if (!reporter?.user.id) return 'not_logged_in';
+ if (!reporter?.user?.id) return 'not_logged_in';
let filterData;
switch (report.type) {
diff --git a/apps/web/src/utils/auth-guards.ts b/apps/web/src/utils/auth-guards.ts
index 0b408093..833007f9 100644
--- a/apps/web/src/utils/auth-guards.ts
+++ b/apps/web/src/utils/auth-guards.ts
@@ -2,7 +2,7 @@ import type { Session } from '@repo/auth/server';
import { RoleTypes } from '@repo/db/types';
export function isAdminOrModerator(session: Session | null) {
- return [RoleTypes.ADMIN, RoleTypes.MODERATOR].some((i) => session?.user.role.includes(i));
+ return [RoleTypes.ADMIN, RoleTypes.MODERATOR].some((i) => session?.user?.role.includes(i));
}
export function isAuthor(session: Session | null, userId?: string | null) {
diff --git a/apps/web/src/utils/server/get-similar-challenges.ts b/apps/web/src/utils/server/get-similar-challenges.ts
index b49849b6..5ec090e1 100644
--- a/apps/web/src/utils/server/get-similar-challenges.ts
+++ b/apps/web/src/utils/server/get-similar-challenges.ts
@@ -25,7 +25,7 @@ export async function getSimilarChallenges(
const solvedSolutions = await prisma.submission.findMany({
where: {
- userId: session?.user.id,
+ userId: session?.user?.id,
isSuccessful: true,
},
select: {
diff --git a/apps/web/src/utils/server/is-beta-user.ts b/apps/web/src/utils/server/is-beta-user.ts
index 8d550721..b65d9ecd 100644
--- a/apps/web/src/utils/server/is-beta-user.ts
+++ b/apps/web/src/utils/server/is-beta-user.ts
@@ -7,7 +7,7 @@ export async function isBetaUser(session: Session | null) {
const isBetaUser = await prisma.betaTokens.findFirst({
where: {
- userId: session?.user.id ?? '',
+ userId: session?.user?.id ?? '',
},
});
diff --git a/packages/auth/src/react.ts b/packages/auth/src/react.ts
index e7aa4cfe..1cc7004a 100644
--- a/packages/auth/src/react.ts
+++ b/packages/auth/src/react.ts
@@ -6,7 +6,7 @@ import { useSession as useAuthSession } from 'next-auth/react';
export { SessionProvider, signIn, signOut } from 'next-auth/react';
interface Session extends DefaultSession {
- user: DefaultSession['user'] & {
+ user?: DefaultSession['user'] & {
id: string;
role: RoleTypes[];
};
diff --git a/packages/auth/src/server.ts b/packages/auth/src/server.ts
index 1d85214c..151a351b 100644
--- a/packages/auth/src/server.ts
+++ b/packages/auth/src/server.ts
@@ -15,7 +15,7 @@ export type { Session, DefaultSession as DefaultAuthSession } from 'next-auth';
*/
declare module 'next-auth' {
interface Session extends DefaultSession {
- user: DefaultSession['user'] & {
+ user?: DefaultSession['user'] & {
id: string;
role: RoleTypes[];
};