Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Org settings route protection (#699)

Ameer Al Ashhabcf762b463586
diff --git a/apps/web/app/(org)/dashboard/_components/Navbar/Items.tsx b/apps/web/app/(org)/dashboard/_components/Navbar/Items.tsx
index 53e7e9336..f436c2e53 100644
--- a/apps/web/app/(org)/dashboard/_components/Navbar/Items.tsx
+++ b/apps/web/app/(org)/dashboard/_components/Navbar/Items.tsx
@@ -62,6 +62,7 @@ const AdminNavItems = ({ toggleMobileNav }: Props) => {
{
name: "Organization Settings",
href: `/dashboard/settings/organization`,
+ ownerOnly: true,
icon: <CogIcon />,
subNav: [],
},
@@ -75,7 +76,7 @@ const AdminNavItems = ({ toggleMobileNav }: Props) => {
},
]
: []),
- ];
+ ]
const [dialogOpen, setDialogOpen] = useState(false);
const {
@@ -86,6 +87,7 @@ const AdminNavItems = ({ toggleMobileNav }: Props) => {
const formRef = useRef<HTMLFormElement | null>(null);
const [createLoading, setCreateLoading] = useState(false);
const [organizationName, setOrganizationName] = useState("");
+ const isOwner = activeOrg?.organization.ownerId === user.id;
const isPathActive = (path: string) => pathname.includes(path);
@@ -261,7 +263,7 @@ const AdminNavItems = ({ toggleMobileNav }: Props) => {
sidebarCollapsed ? "flex flex-col justify-center items-center" : ""
)}
>
- {manageNavigation.map((item) => (
+ {manageNavigation.filter((item) => !item.ownerOnly || isOwner).map((item) => (
<div
key={item.name}
className="flex relative justify-center items-center mb-1.5 w-full"
diff --git a/apps/web/app/(org)/dashboard/settings/organization/Organization.tsx b/apps/web/app/(org)/dashboard/settings/organization/Organization.tsx
index 8e0852ce3..fa864cb4e 100644
--- a/apps/web/app/(org)/dashboard/settings/organization/Organization.tsx
+++ b/apps/web/app/(org)/dashboard/settings/organization/Organization.tsx
@@ -2,11 +2,11 @@
import { manageBilling } from "@/actions/organization/manage-billing";
import { updateOrganizationDetails } from "@/actions/organization/update-details";
+import { useDashboardContext } from "@/app/(org)/dashboard/Contexts";
import { Card, CardDescription, CardTitle } from "@cap/ui";
import { useRouter } from "next/navigation";
import { Dispatch, SetStateAction, useCallback, useRef, useState } from "react";
import { toast } from "sonner";
-import { useDashboardContext } from "@/app/(org)/dashboard/Contexts";
import { BillingCard } from "./components/BillingCard";
import { CustomDomainIconCard } from "./components/CustomDomainIconCard";
@@ -15,125 +15,126 @@ import { MembersCard } from "./components/MembersCard";
import { OrganizationDetailsCard } from "./components/OrganizationDetailsCard";
import { SeatsInfoCards } from "./components/SeatsInfoCards";
-export const Organization = () => {
- const { activeOrganization, user } = useDashboardContext();
- const organizationName = activeOrganization?.organization.name;
- const router = useRouter();
- const [loading, setLoading] = useState(false);
- const [billingLoading, setBillingLoading] = useState(false);
- const isOwner = user?.id === activeOrganization?.organization.ownerId;
- const [isInviteDialogOpen, setIsInviteDialogOpen] = useState(false);
- const ownerToastShown = useRef(false);
- const [saveLoading, setSaveLoading] = useState(false);
+export const
+ Organization = () => {
+ const { activeOrganization, user } = useDashboardContext();
+ const organizationName = activeOrganization?.organization.name;
+ const router = useRouter();
+ const [loading, setLoading] = useState(false);
+ const [billingLoading, setBillingLoading] = useState(false);
+ const isOwner = user?.id === activeOrganization?.organization.ownerId;
+ const [isInviteDialogOpen, setIsInviteDialogOpen] = useState(false);
+ const ownerToastShown = useRef(false);
+ const [saveLoading, setSaveLoading] = useState(false);
- const showOwnerToast = useCallback(() => {
- if (!ownerToastShown.current) {
- toast.error("Only the owner can make changes");
- ownerToastShown.current = true;
- setTimeout(() => {
- ownerToastShown.current = false;
- }, 3000);
- }
- }, []);
+ const showOwnerToast = useCallback(() => {
+ if (!ownerToastShown.current) {
+ toast.error("Only the owner can make changes");
+ ownerToastShown.current = true;
+ setTimeout(() => {
+ ownerToastShown.current = false;
+ }, 3000);
+ }
+ }, []);
- const handleSubmit = useCallback(
- async (e: React.FormEvent<HTMLFormElement>) => {
- e.preventDefault();
+ const handleSubmit = useCallback(
+ async (e: React.FormEvent<HTMLFormElement>) => {
+ e.preventDefault();
- if (!isOwner) {
- showOwnerToast();
- return;
- }
+ if (!isOwner) {
+ showOwnerToast();
+ return;
+ }
- const formData = new FormData(e.currentTarget);
- const organizationName = formData.get("organizationName") as string;
- const allowedEmailDomain = formData.get("allowedEmailDomain") as string;
+ const formData = new FormData(e.currentTarget);
+ const organizationName = formData.get("organizationName") as string;
+ const allowedEmailDomain = formData.get("allowedEmailDomain") as string;
- try {
- setSaveLoading(true);
- await updateOrganizationDetails(
- organizationName,
- allowedEmailDomain,
- activeOrganization?.organization.id as string
- );
- toast.success("Settings updated successfully");
- router.refresh();
- } catch (error) {
- console.error("Error updating settings:", error);
- toast.error("An error occurred while updating settings");
- } finally {
- setSaveLoading(false);
- }
- },
- [isOwner, showOwnerToast, activeOrganization?.organization.id, router]
- );
+ try {
+ setSaveLoading(true);
+ await updateOrganizationDetails(
+ organizationName,
+ allowedEmailDomain,
+ activeOrganization?.organization.id as string
+ );
+ toast.success("Settings updated successfully");
+ router.refresh();
+ } catch (error) {
+ console.error("Error updating settings:", error);
+ toast.error("An error occurred while updating settings");
+ } finally {
+ setSaveLoading(false);
+ }
+ },
+ [isOwner, showOwnerToast, activeOrganization?.organization.id, router]
+ );
- const handleManageBilling = useCallback(
- async (loadingDispatch: Dispatch<SetStateAction<boolean>>) => {
- if (!isOwner) {
- showOwnerToast();
- return;
- }
- loadingDispatch(true);
- try {
- const url = await manageBilling();
- router.push(url);
- } catch (error) {
- console.error("Error managing billing:", error);
- toast.error("An error occurred while managing billing");
- loadingDispatch(false);
- }
- },
- [isOwner, showOwnerToast, router]
- );
+ const handleManageBilling = useCallback(
+ async (loadingDispatch: Dispatch<SetStateAction<boolean>>) => {
+ if (!isOwner) {
+ showOwnerToast();
+ return;
+ }
+ loadingDispatch(true);
+ try {
+ const url = await manageBilling();
+ router.push(url);
+ } catch (error) {
+ console.error("Error managing billing:", error);
+ toast.error("An error occurred while managing billing");
+ loadingDispatch(false);
+ }
+ },
+ [isOwner, showOwnerToast, router]
+ );
+
+ return (
+ <form className="flex flex-col gap-6" onSubmit={handleSubmit}>
+ {isOwner === false && (
+ <Card>
+ <CardTitle>*Only the owner can make changes</CardTitle>
+ <CardDescription>
+ Only the owner can make changes to this organization.
+ </CardDescription>
+ </Card>
+ )}
- return (
- <form className="flex flex-col gap-6" onSubmit={handleSubmit}>
- {isOwner === false && (
- <Card>
- <CardTitle>*Only the owner can make changes</CardTitle>
- <CardDescription>
- Only the owner can make changes to this organization.
- </CardDescription>
- </Card>
- )}
+ <SeatsInfoCards />
- <SeatsInfoCards />
+ <div className="flex flex-col gap-6 justify-center items-stretch xl:flex-row">
+ <OrganizationDetailsCard
+ isOwner={isOwner}
+ saveLoading={saveLoading}
+ showOwnerToast={showOwnerToast}
+ organizationName={organizationName}
+ />
+ <CustomDomainIconCard
+ isOwner={isOwner}
+ showOwnerToast={showOwnerToast}
+ />
+ </div>
- <div className="flex flex-col gap-6 justify-center items-stretch xl:flex-row">
- <OrganizationDetailsCard
+ <MembersCard
isOwner={isOwner}
- saveLoading={saveLoading}
+ loading={loading}
+ handleManageBilling={() => handleManageBilling(setLoading)}
showOwnerToast={showOwnerToast}
- organizationName={organizationName}
+ setIsInviteDialogOpen={setIsInviteDialogOpen}
/>
- <CustomDomainIconCard
+
+ <BillingCard
isOwner={isOwner}
- showOwnerToast={showOwnerToast}
+ loading={billingLoading}
+ handleManageBilling={() => handleManageBilling(setBillingLoading)}
/>
- </div>
-
- <MembersCard
- isOwner={isOwner}
- loading={loading}
- handleManageBilling={() => handleManageBilling(setLoading)}
- showOwnerToast={showOwnerToast}
- setIsInviteDialogOpen={setIsInviteDialogOpen}
- />
-
- <BillingCard
- isOwner={isOwner}
- loading={billingLoading}
- handleManageBilling={() => handleManageBilling(setBillingLoading)}
- />
- <InviteDialog
- isOpen={isInviteDialogOpen}
- setIsOpen={setIsInviteDialogOpen}
- isOwner={isOwner}
- showOwnerToast={showOwnerToast}
- handleManageBilling={() => handleManageBilling(setLoading)}
- />
- </form>
- );
-};
+ <InviteDialog
+ isOpen={isInviteDialogOpen}
+ setIsOpen={setIsInviteDialogOpen}
+ isOwner={isOwner}
+ showOwnerToast={showOwnerToast}
+ handleManageBilling={() => handleManageBilling(setLoading)}
+ />
+ </form>
+ );
+ };
diff --git a/apps/web/app/(org)/dashboard/settings/organization/components/CustomDomain.tsx b/apps/web/app/(org)/dashboard/settings/organization/components/CustomDomain.tsx
index 505784c96..5989d807d 100644
--- a/apps/web/app/(org)/dashboard/settings/organization/components/CustomDomain.tsx
+++ b/apps/web/app/(org)/dashboard/settings/organization/components/CustomDomain.tsx
@@ -33,7 +33,12 @@ type DomainConfig = {
requiredAValue?: string;
};
-export function CustomDomain() {
+interface CustomDomainProps {
+ isOwner: boolean;
+ showOwnerToast: () => void;
+}
+
+export function CustomDomain({ isOwner, showOwnerToast }: CustomDomainProps) {
const router = useRouter();
const { activeOrganization, isSubscribed } = useDashboardContext();
const [domain, setDomain] = useState(
@@ -226,6 +231,7 @@ export function CustomDomain() {
}
};
+
return (
<div className="flex flex-wrap mt-4 space-y-6">
<div className="flex-1">
@@ -233,28 +239,28 @@ export function CustomDomain() {
<Input
type="text"
id="customDomain"
- placeholder="your-domain.com"
+ placeholder={isOwner ? "your-domain.com" : "Only the owner of the organization can change this"}
value={domain}
onChange={(e) => setDomain(e.target.value)}
- disabled={loading}
+ disabled={!isOwner}
className="flex-1 min-h-[44px]"
/>
<div className="flex gap-2 justify-between items-center mt-4">
{activeOrganization?.organization.customDomain &&
(isVerified ? (
<>
- <div className="flex items-center gap-1.5 text-green-800 bg-green-400 px-2.5 py-1.5 rounded-full text-sm">
+ <div className="flex items-center gap-1 text-white bg-green-600 px-2.5 py-1.5 rounded-full text-sm">
<CheckCircle className="size-3" />
- <span className="text-xs font-medium text-green-800">
+ <span className="text-xs font-medium text-white">
Domain verified
</span>
</div>
</>
) : (
<>
- <div className="flex items-center gap-1.5 text-red-800 bg-red-400 px-2.5 py-1.5 rounded-full text-sm">
- <XCircle className="w-4 h-4" />
- <span className="text-sm font-medium text-red-800">
+ <div className="flex items-center gap-1 text-white bg-red-500 px-2.5 py-1.5 rounded-full text-sm">
+ <XCircle className="size-3" />
+ <span className="text-xs font-medium text-white">
Domain not verified
</span>
</div>
@@ -265,7 +271,6 @@ export function CustomDomain() {
<Button
type="submit"
size="sm"
- className="px-[1.5rem]"
variant="dark"
onClick={handleSubmit}
spinner={loading}
@@ -285,11 +290,11 @@ export function CustomDomain() {
>
{verifying ? (
<FontAwesomeIcon
- className="animate-spin size-3.5"
+ className="animate-spin size-3 mr-0.5"
icon={faRefresh}
/>
) : (
- <FontAwesomeIcon className="size-3.5" icon={faRefresh} />
+ <FontAwesomeIcon className="size-3 mr-0.5" icon={faRefresh} />
)}
Refresh
</Button>
@@ -302,7 +307,7 @@ export function CustomDomain() {
onClick={handleRemoveDomain}
disabled={loading}
>
- <FontAwesomeIcon className="size-3.5" icon={faTrash} />
+ <FontAwesomeIcon className="size-3 mr-0.5" icon={faTrash} />
Remove
</Button>
)}
diff --git a/apps/web/app/(org)/dashboard/settings/organization/components/CustomDomainIconCard.tsx b/apps/web/app/(org)/dashboard/settings/organization/components/CustomDomainIconCard.tsx
index e4a6e5d5c..10fbcaec9 100644
--- a/apps/web/app/(org)/dashboard/settings/organization/components/CustomDomainIconCard.tsx
+++ b/apps/web/app/(org)/dashboard/settings/organization/components/CustomDomainIconCard.tsx
@@ -26,7 +26,7 @@ export const CustomDomainIconCard = ({
it unique.
</CardDescription>
</div>
- <CustomDomain />
+ <CustomDomain isOwner={isOwner} showOwnerToast={showOwnerToast} />
</div>
</Card>
);
diff --git a/apps/web/app/(org)/dashboard/settings/organization/page.tsx b/apps/web/app/(org)/dashboard/settings/organization/page.tsx
index 3a15963d1..6bca5b40d 100644
--- a/apps/web/app/(org)/dashboard/settings/organization/page.tsx
+++ b/apps/web/app/(org)/dashboard/settings/organization/page.tsx
@@ -1,4 +1,7 @@
+import { getCurrentUser } from "@cap/database/auth/session";
import { Metadata } from "next";
+import { redirect } from "next/navigation";
+import { getDashboardData } from "../../dashboard-data";
import { Organization } from "./Organization";
export const metadata: Metadata = {
@@ -7,6 +10,21 @@ export const metadata: Metadata = {
export const revalidate = 0;
-export default function OrganizationPage() {
+export default async function OrganizationPage() {
+ const user = await getCurrentUser();
+
+ if (!user) {
+ redirect("/auth/signin");
+ }
+
+ const dashboardData = await getDashboardData(user);
+ const isOwner = dashboardData.organizationSelect.find(
+ (organization) => organization.organization.ownerId === user.id
+ );
+
+ if (!isOwner) {
+ redirect("/dashboard/caps");
+ }
+
return <Organization />;
}
diff --git a/packages/ui/src/components/input/Input.tsx b/packages/ui/src/components/input/Input.tsx
index 6a068e7aa..3405996b9 100644
--- a/packages/ui/src/components/input/Input.tsx
+++ b/packages/ui/src/components/input/Input.tsx
@@ -3,7 +3,7 @@ import * as React from "react";
import { classNames } from "@cap/utils";
export interface InputProps
- extends React.InputHTMLAttributes<HTMLInputElement> {}
+ extends React.InputHTMLAttributes<HTMLInputElement> { }
const Input = React.forwardRef<HTMLInputElement, InputProps>(
({ className, type, ...props }, ref) => {
@@ -13,7 +13,7 @@ const Input = React.forwardRef<HTMLInputElement, InputProps>(
className={classNames(
"flex px-4 w-full font-thin transition-all duration-200 text-[13px] text-gray-12 bg-gray-1 border-gray-4 outline-0 focus:bg-gray-2",
"rounded-xl hover:bg-gray-2 hover:border-gray-5 h-[44px] placeholder:text-gray-8 border-[1px] focus:border-gray-5",
- "file:border-0 file:bg-transparent file:text-sm file:font-medium",
+ "file:border-0 file:bg-transparent file:text-sm file:font-medium disabled:pointer-events-none",
"disabled:cursor-not-allowed disabled:bg-gray-2 disabled:text-gray-9 placeholder:transition-all",
"ring-0 ring-gray-2 focus:ring-1 focus:ring-gray-12 focus:ring-offset-2 ring-offset-gray-1 hover:placeholder:text-gray-12 placeholder:duration-200",
className