Skip to content

Commit e618063

Browse files
committed
fix the createOrgForm
1 parent 70bb9f2 commit e618063

3 files changed

Lines changed: 31 additions & 61 deletions

File tree

src/lib/auth-client.ts

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ export type AuthSession = typeof authClient.$Infer.Session;
4444
* Check if a role can update workspace (synchronous, client-side only)
4545
*/
4646
export function canRoleUpdateWorkspace(
47-
role: "owner" | "admin" | "member"
47+
role: "owner" | "admin" | "member" | undefined,
4848
): boolean {
49+
if (!role) return false;
4950
return authClient.organization.checkRolePermission({
5051
permissions: { organization: ["create"] },
5152
role,
@@ -56,8 +57,9 @@ export function canRoleUpdateWorkspace(
5657
* Check if a role can delete an organization (synchronous, client-side only)
5758
*/
5859
export function canRoleDeleteOrganization(
59-
role: "owner" | "admin" | "member"
60+
role: "owner" | "admin" | "member" | undefined,
6061
): boolean {
62+
if (!role) return false;
6163
return authClient.organization.checkRolePermission({
6264
permissions: { organization: ["delete"] },
6365
role,
@@ -68,8 +70,9 @@ export function canRoleDeleteOrganization(
6870
* Check if a role can view an organization (synchronous, client-side only)
6971
*/
7072
export function canRoleViewOrganization(
71-
role: "owner" | "admin" | "member"
73+
role: "owner" | "admin" | "member" | undefined,
7274
): boolean {
75+
if (!role) return false;
7376
return authClient.organization.checkRolePermission({
7477
permissions: { organization: ["view"] },
7578
role,
@@ -80,8 +83,9 @@ export function canRoleViewOrganization(
8083
* Check if a role can update an organization (synchronous, client-side only)
8184
*/
8285
export function canRoleUpdateOrganization(
83-
role: "owner" | "admin" | "member"
86+
role: "owner" | "admin" | "member" | undefined,
8487
): boolean {
88+
if (!role) return false;
8589
return authClient.organization.checkRolePermission({
8690
permissions: { organization: ["update"] },
8791
role,
@@ -93,36 +97,17 @@ export function canRoleUpdateOrganization(
9397
* @deprecated
9498
*/
9599
export function canRoleDeleteWorkspace(
96-
role: "owner" | "admin" | "member"
100+
role: "owner" | "admin" | "member",
97101
): boolean {
98102
return canRoleDeleteOrganization(role);
99103
}
100104

101-
/**
102-
* Deprecated: Use canRoleCreateOrganization instead
103-
* @deprecated
104-
*/
105-
export function canRoleCreateWorkspace(
106-
role: "owner" | "admin" | "member"
107-
): boolean {
108-
return canRoleCreateOrganization(role);
109-
}
110-
111-
export function canRoleCreateOrganization(
112-
role: "owner" | "admin" | "member"
113-
): boolean {
114-
return authClient.organization.checkRolePermission({
115-
permissions: { organization: ["create"] },
116-
role,
117-
});
118-
}
119-
120105
/**
121106
* Deprecated: Use canRoleViewOrganization instead
122107
* @deprecated
123108
*/
124109
export function canRoleViewWorkspace(
125-
role: "owner" | "admin" | "member"
110+
role: "owner" | "admin" | "member",
126111
): boolean {
127112
return canRoleViewOrganization(role);
128113
}
@@ -131,8 +116,9 @@ export function canRoleViewWorkspace(
131116
* Check if a role can manage members (synchronous, client-side only)
132117
*/
133118
export function canRoleManageMembers(
134-
role: "owner" | "admin" | "member"
119+
role: "owner" | "admin" | "member" | undefined,
135120
): boolean {
121+
if (!role) return false;
136122
return authClient.organization.checkRolePermission({
137123
permissions: { member: ["create", "update", "delete"] },
138124
role,
@@ -143,8 +129,9 @@ export function canRoleManageMembers(
143129
* Check if a role can invite members (synchronous, client-side only)
144130
*/
145131
export function canRoleInviteMembers(
146-
role: "owner" | "admin" | "member"
132+
role: "owner" | "admin" | "member" | undefined,
147133
): boolean {
134+
if (!role) return false;
148135
return authClient.organization.checkRolePermission({
149136
permissions: { invitation: ["create"] },
150137
role,
@@ -158,7 +145,7 @@ class Permission {
158145
*/
159146
static checkPermissionSync(
160147
role: "owner" | "admin" | "member",
161-
permissions: Record<string, string[]>
148+
permissions: Record<string, string[]>,
162149
): boolean {
163150
return authClient.organization.checkRolePermission({
164151
permissions,
@@ -167,40 +154,46 @@ class Permission {
167154
}
168155

169156
private static canRoleCreateOrganization(
170-
role: "owner" | "admin" | "member"
157+
role: "owner" | "admin" | "member" | undefined,
171158
): boolean {
159+
if (!role) return false;
172160
return Permission.checkPermissionSync(role, { organization: ["create"] });
173161
}
174162

175163
private static canRoleDeleteOrganization(
176-
role: "owner" | "admin" | "member"
164+
role: "owner" | "admin" | "member" | undefined,
177165
): boolean {
166+
if (!role) return false;
178167
return Permission.checkPermissionSync(role, { organization: ["delete"] });
179168
}
180169

181170
private static canRoleViewOrganization(
182-
role: "owner" | "admin" | "member"
171+
role: "owner" | "admin" | "member" | undefined,
183172
): boolean {
173+
if (!role) return false;
184174
return Permission.checkPermissionSync(role, { organization: ["view"] });
185175
}
186176

187177
private static canRoleUpdateOrganization(
188-
role: "owner" | "admin" | "member"
178+
role: "owner" | "admin" | "member" | undefined,
189179
): boolean {
180+
if (!role) return false;
190181
return Permission.checkPermissionSync(role, { organization: ["update"] });
191182
}
192183

193184
private static canRoleManageMembers(
194-
role: "owner" | "admin" | "member"
185+
role: "owner" | "admin" | "member" | undefined,
195186
): boolean {
187+
if (!role) return false;
196188
return Permission.checkPermissionSync(role, {
197189
member: ["create", "update", "delete"],
198190
});
199191
}
200192

201193
private static canRoleInviteMembers(
202-
role: "owner" | "admin" | "member"
194+
role: "owner" | "admin" | "member" | undefined,
203195
): boolean {
196+
if (!role) return false;
204197
return Permission.checkPermissionSync(role, { invitation: ["create"] });
205198
}
206199

@@ -221,7 +214,7 @@ class Permission {
221214
* Use this when you need to validate permissions before an action
222215
*/
223216
export async function checkPermission(
224-
permissions: Record<string, string[]>
217+
permissions: Record<string, string[]>,
225218
): Promise<boolean> {
226219
try {
227220
const result = await authClient.organization.hasPermission({

src/lib/form/new-workspace-form.tsx

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ import { useForm } from "@tanstack/react-form";
55
import { useRouter } from "next/navigation";
66
import { toast } from "sonner";
77
import { createWorkspace } from "@/action";
8-
import {
9-
canRoleCreateOrganization,
10-
useListOrganizations,
11-
useSession,
12-
} from "@/lib/auth-client";
8+
import { useListOrganizations, useSession } from "@/lib/auth-client";
139
import {
1410
Field,
1511
FieldDescription,
@@ -24,18 +20,13 @@ import { NewWorkspaceFormSchema, TUserRole } from "@/types";
2420

2521
const NewWorkspaceForm = () => {
2622
const router = useRouter();
27-
const { data: session } = useSession();
2823
const { data: organizations } = useListOrganizations();
2924
const orgMember = organizations?.find(
30-
(org) => org.id === organizations[0]?.id
25+
(org) => org.id === organizations[0]?.id,
3126
);
3227

3328
const [isPending, setPending] = useState<boolean>(false);
3429

35-
const userRole = orgMember?.role as TUserRole;
36-
37-
const canCreateWorkspace = canRoleCreateOrganization(userRole);
38-
3930
const form = useForm({
4031
defaultValues: {
4132
name: "",
@@ -61,7 +52,7 @@ const NewWorkspaceForm = () => {
6152
// Handle permission denied errors
6253
if (error instanceof Error && error.message.includes("permission")) {
6354
toast.error(
64-
"You don't have permission to create a workspace. Contact your admin."
55+
"You don't have permission to create a workspace. Contact your admin.",
6556
);
6657
} else {
6758
toast.error("An unexpected error occurred. Please try again.");
@@ -72,18 +63,6 @@ const NewWorkspaceForm = () => {
7263
}
7364
},
7465
});
75-
if (!canCreateWorkspace) {
76-
return (
77-
<div className="space-y-4 min-w-lg mx-auto text-center">
78-
<p className="text-red-500 font-medium">
79-
You don't have permission to create a workspace.
80-
</p>
81-
<p className="text-muted-foreground text-sm">
82-
Contact your organization admin to request workspace creation access.
83-
</p>
84-
</div>
85-
);
86-
}
8766

8867
return (
8968
<form

src/styles/globals.css

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
@custom-variant dark (&:is(.dark *));
88

9-
@custom-variant dark (&:is(.dark *));
10-
119
@theme {
1210
--color-primary: oklch(0.527 0.154 150.069);
1311
--color-secondary: #ff742e;

0 commit comments

Comments
 (0)