Skip to content

Commit 0d2cd0e

Browse files
🛠️ fix: add user authentication check and redirect to login page
- In `layout.tsx` of project settings, check if the user is authenticated and has access to the project. If not, redirect to the login page with a redirect URL to the current page. - In `layout.tsx` of project join, check if the user is authenticated. If not, redirect to the login page with a redirect URL to the current page. - In `page.tsx` of project join, remove the user authentication check and redirect as it is already handled in the layout.tsx file. - In `utils.ts`, handle the error in the `getSession` function and return a session object with user as null if the error occurs.
1 parent f571830 commit 0d2cd0e

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
lines changed

apps/web/app/(dashboard)/app/project/[projectId]/layout.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,20 @@ export default async function SettingsLayout({
3131
];
3232

3333
const session = await getSession();
34+
const userId = session?.user?.id;
35+
if (!userId) redirect(`/signin?redirect=/app/project/${projectId}`);
36+
3437
const userInProject = await prisma.projectUsers.findUnique({
3538
where: {
3639
userId_projectId: {
37-
userId: session.user.id,
40+
userId: userId,
3841
projectId: projectId,
3942
},
4043
},
4144
});
4245

4346
if (!userInProject) {
44-
redirect('/app')
47+
redirect("/app");
4548
}
4649

4750
return (

apps/web/app/(dashboard)/app/project/join/[invitationCode]/layout.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { prisma } from "@/app/utils";
1+
import { getSession, prisma } from "@/app/utils";
22
import { redirect } from "next/navigation";
33

44
interface SettingsLayoutProps {
@@ -12,6 +12,11 @@ export default async function SettingsLayout({
1212
children,
1313
params,
1414
}: SettingsLayoutProps) {
15+
const session = await getSession();
16+
if (!session?.user) {
17+
redirect(`/signin?redirect=/app/project/join/${params.invitationCode}`);
18+
}
19+
1520
const invitationCode = params.invitationCode;
1621
const project = await prisma.project.findUnique({
1722
where: {

apps/web/app/(dashboard)/app/project/join/[invitationCode]/page.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,14 @@ export default async function JoinProjectPage({
1212
params: { invitationCode: string };
1313
}) {
1414
const session = await getSession();
15-
if (!session.user) {
16-
redirect(`/signin?redirect=/app/project/join/${params.invitationCode}`);
17-
}
1815
const invitationCode = params.invitationCode;
1916

2017
const project = await prisma.project.findUnique({
2118
where: {
2219
invitationCode: invitationCode,
2320
users: {
2421
none: {
25-
userId: session.user.id,
22+
userId: session?.user?.id,
2623
},
2724
},
2825
},

apps/web/app/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,13 @@ interface WithSessionHandler {
9797
}
9898

9999
export const getSession = async () => {
100+
try {
100101
const session = await getServerSession(authOptions) as Session;
101102
if (!session?.user) return { user: null } as unknown as Session;
102103
return session;
104+
} catch(error) {
105+
return { user: null } as unknown as Session;
106+
}
103107
};
104108

105109
export const withSession =

0 commit comments

Comments
 (0)