Skip to content

Commit f51e1d9

Browse files
committed
improvement(auth): remove /api/user/super-user route, use session role
Include user.role in customSession so it's available client-side. Replace all useSuperUserStatus() calls with session.user.role === 'admin'. Delete the now-redundant /api/user/super-user endpoint.
1 parent dbde6e6 commit f51e1d9

File tree

6 files changed

+7
-97
lines changed

6 files changed

+7
-97
lines changed

apps/sim/app/_shell/providers/session-provider.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export type AppSession = {
1313
emailVerified?: boolean
1414
name?: string | null
1515
image?: string | null
16+
role?: string
1617
createdAt?: Date
1718
updatedAt?: Date
1819
} | null

apps/sim/app/api/user/super-user/route.ts

Lines changed: 0 additions & 42 deletions
This file was deleted.

apps/sim/app/templates/[id]/template.tsx

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ export default function TemplateDetails({ isWorkspaceContext = false }: Template
148148
const [currentUserOrgRoles, setCurrentUserOrgRoles] = useState<
149149
Array<{ organizationId: string; role: string }>
150150
>([])
151-
const [isSuperUser, setIsSuperUser] = useState(false)
151+
const isSuperUser = session?.user?.role === 'admin'
152152
const [isUsing, setIsUsing] = useState(false)
153153
const [isEditing, setIsEditing] = useState(false)
154154
const [isApproving, setIsApproving] = useState(false)
@@ -186,21 +186,6 @@ export default function TemplateDetails({ isWorkspaceContext = false }: Template
186186
}
187187
}
188188

189-
const fetchSuperUserStatus = async () => {
190-
if (!currentUserId) return
191-
192-
try {
193-
const response = await fetch('/api/user/super-user')
194-
if (response.ok) {
195-
const data = await response.json()
196-
setIsSuperUser(data.isSuperUser || false)
197-
}
198-
} catch (error) {
199-
logger.error('Error fetching super user status:', error)
200-
}
201-
}
202-
203-
fetchSuperUserStatus()
204189
fetchUserOrganizations()
205190
}, [currentUserId])
206191

apps/sim/app/workspace/[workspaceId]/w/components/sidebar/components/settings-sidebar/settings-sidebar.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import { prefetchWorkspaceCredentials } from '@/hooks/queries/credentials'
2020
import { prefetchGeneralSettings, useGeneralSettings } from '@/hooks/queries/general-settings'
2121
import { useOrganizations } from '@/hooks/queries/organization'
2222
import { prefetchSubscriptionData, useSubscriptionData } from '@/hooks/queries/subscription'
23-
import { useSuperUserStatus } from '@/hooks/queries/user-profile'
2423
import { usePermissionConfig } from '@/hooks/use-permission-config'
2524
import { useSettingsNavigation } from '@/hooks/use-settings-navigation'
2625

@@ -49,7 +48,6 @@ export function SettingsSidebar({
4948
staleTime: 5 * 60 * 1000,
5049
})
5150
const { data: ssoProvidersData, isLoading: isLoadingSSO } = useSSOProviders()
52-
const { data: superUserData } = useSuperUserStatus(session?.user?.id)
5351

5452
const activeOrganization = organizationsData?.activeOrganization
5553
const { config: permissionConfig } = usePermissionConfig()
@@ -65,7 +63,7 @@ export function SettingsSidebar({
6563
const hasTeamPlan = subscriptionStatus.isTeam || subscriptionStatus.isEnterprise
6664
const hasEnterprisePlan = subscriptionStatus.isEnterprise
6765

68-
const isSuperUser = superUserData?.isSuperUser ?? false
66+
const isSuperUser = session?.user?.role === 'admin'
6967

7068
const isSSOProviderOwner = useMemo(() => {
7169
if (isHosted) return null

apps/sim/hooks/queries/user-profile.ts

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const logger = createLogger('UserProfileQuery')
99
export const userProfileKeys = {
1010
all: ['userProfile'] as const,
1111
profile: () => [...userProfileKeys.all, 'profile'] as const,
12-
superUser: (userId?: string) => [...userProfileKeys.all, 'superUser', userId ?? ''] as const,
1312
}
1413

1514
/**
@@ -117,40 +116,6 @@ export function useUpdateUserProfile() {
117116
})
118117
}
119118

120-
/**
121-
* Superuser status response type
122-
*/
123-
interface SuperUserStatus {
124-
isSuperUser: boolean
125-
}
126-
127-
/**
128-
* Fetch superuser status from API
129-
*/
130-
async function fetchSuperUserStatus(signal?: AbortSignal): Promise<SuperUserStatus> {
131-
const response = await fetch('/api/user/super-user', { signal })
132-
133-
if (!response.ok) {
134-
return { isSuperUser: false }
135-
}
136-
137-
const data = await response.json()
138-
return { isSuperUser: data.isSuperUser ?? false }
139-
}
140-
141-
/**
142-
* Hook to fetch superuser status
143-
* @param userId - User ID for cache isolation (required for proper per-user caching)
144-
*/
145-
export function useSuperUserStatus(userId?: string) {
146-
return useQuery({
147-
queryKey: userProfileKeys.superUser(userId),
148-
queryFn: ({ signal }) => fetchSuperUserStatus(signal),
149-
enabled: Boolean(userId),
150-
staleTime: 5 * 60 * 1000, // 5 minutes - superuser status rarely changes
151-
})
152-
}
153-
154119
/**
155120
* Reset password mutation
156121
*/

apps/sim/lib/auth/auth.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,10 @@ export const auth = betterAuth({
649649
expiresIn: 24 * 60 * 60, // 24 hours - Socket.IO handles connection persistence with heartbeats
650650
}),
651651
customSession(async ({ user, session }) => ({
652-
user,
652+
user: {
653+
...user,
654+
role: (user as unknown as { role?: string }).role ?? 'user',
655+
},
653656
session,
654657
})),
655658
emailOTP({

0 commit comments

Comments
 (0)