Skip to content

Commit 62e3f95

Browse files
RyukemeisterThyMinimalDevRajiv SahalPeerRich
authored
chore: redirect platform users to dashboard (#15361)
* chore: redirect platform users to dashboard * update AppProps typing to include isPlatformUser * fixup * get isPlatformUser boolean from user and then redirect user accordingly * fix type checks * minor refactor * AppProps type does not change * helper function for redirecting on server side * revert: AppProps type does not change * replace helper function to redirect platform users with a custom hook * add comments stating why we use this hook * fixup! add comments stating why we use this hook --------- Co-authored-by: Morgan Vernay <morgan@cal.com> Co-authored-by: Rajiv Sahal <rajivsahal@Rajivs-MacBook-Pro.local> Co-authored-by: Morgan <33722304+ThyMinimalDev@users.noreply.github.com> Co-authored-by: Peer Richelsen <peeroke@gmail.com>
1 parent 9a473d5 commit 62e3f95

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

apps/web/components/PageWrapper.tsx

+21
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import { useSession } from "next-auth/react";
12
import { DefaultSeo } from "next-seo";
23
import { Inter } from "next/font/google";
34
import localFont from "next/font/local";
45
import Head from "next/head";
6+
import { useRouter, usePathname } from "next/navigation";
57
import Script from "next/script";
8+
import { useEffect } from "react";
69

710
import "@calcom/embed-core/src/embed-iframe";
811
import LicenseRequired from "@calcom/features/ee/common/components/LicenseRequired";
@@ -27,6 +30,23 @@ const calFont = localFont({
2730
weight: "600",
2831
});
2932

33+
const Redirects = () => {
34+
const session = useSession();
35+
const router = useRouter();
36+
const pathName = usePathname();
37+
// is the connected user in a platform organization or not
38+
const isPlatformUser = session?.data?.isPlatformUser ?? false;
39+
40+
useEffect(() => {
41+
// is platform user trying to access a non-platform page, redirect to platform dashboard
42+
if (isPlatformUser && pathName && !pathName.startsWith("/settings")) {
43+
return router.replace("/settings/platform");
44+
}
45+
}, [isPlatformUser, pathName, router]);
46+
47+
return <></>;
48+
};
49+
3050
function PageWrapper(props: AppProps) {
3151
const { Component, pageProps, err, router } = props;
3252
let pageStatus = "200";
@@ -56,6 +76,7 @@ function PageWrapper(props: AppProps) {
5676

5777
return (
5878
<AppProviders {...providerProps}>
79+
<Redirects />
5980
<Head>
6081
<meta
6182
name="viewport"

apps/web/pages/_app.tsx

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { IncomingMessage } from "http";
2+
import type { NextApiRequest } from "next";
23
import type { AppContextType } from "next/dist/shared/lib/utils";
34
import React from "react";
45

@@ -22,14 +23,23 @@ declare global {
2223
}
2324

2425
MyApp.getInitialProps = async (ctx: AppContextType) => {
25-
const { req } = ctx.ctx;
26+
const { req, res } = ctx.ctx;
2627

2728
let newLocale = "en";
2829

2930
if (req) {
3031
const { getLocale } = await import("@calcom/features/auth/lib/getLocale");
32+
const { getServerSession } = await import("@calcom/features/auth/lib/getServerSession");
33+
const session = await getServerSession({ req: req as NextApiRequest, res });
34+
3135
// eslint-disable-next-line @typescript-eslint/no-explicit-any
3236
newLocale = await getLocale(req as IncomingMessage & { cookies: Record<string, any> });
37+
return {
38+
pageProps: {
39+
newLocale,
40+
session,
41+
},
42+
};
3343
} else if (typeof window !== "undefined" && window.calNewLocale) {
3444
newLocale = window.calNewLocale;
3545
}

packages/features/auth/lib/getServerSession.ts

+8
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ export async function getServerSession(options: {
5858
where: {
5959
email: token.email.toLowerCase(),
6060
},
61+
include: {
62+
organization: {
63+
select: {
64+
isPlatform: true,
65+
},
66+
},
67+
},
6168
});
6269

6370
if (!userFromDb) {
@@ -103,6 +110,7 @@ export async function getServerSession(options: {
103110
profile: user.profile,
104111
},
105112
profileId: token.profileId,
113+
isPlatformUser: user.profile.organization?.isPlatform ?? false,
106114
upId,
107115
};
108116

packages/types/next-auth.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ declare module "next-auth" {
1414
profileId?: number | null;
1515
upId: string;
1616
user: User;
17+
isPlatformUser?: boolean;
1718
}
1819

1920
interface User extends Omit<DefaultUser, "id"> {

0 commit comments

Comments
 (0)