Skip to content

Commit 81c610f

Browse files
authored
Merge pull request #336 from codeunia-dev/feat/companydashboard
fix(company-dashboard): Use effective company slug fallback and enhance role protection redirect
2 parents 46433c5 + af7b5f9 commit 81c610f

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

app/dashboard/company/layout.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,16 @@ function CompanyDashboardContent({
185185
}
186186

187187
// Generate sidebar items with dynamic company slug
188+
// Use currentCompany.slug as fallback when companySlug from params is undefined
189+
const effectiveSlug = companySlug || currentCompany.slug
190+
188191
const sidebarItems: SidebarGroupType[] = [
189192
{
190193
title: 'Dashboard',
191194
items: [
192195
{
193196
title: 'Overview',
194-
url: `/dashboard/company/${companySlug}`,
197+
url: `/dashboard/company/${effectiveSlug}`,
195198
icon: LayoutDashboard,
196199
},
197200
],
@@ -201,17 +204,17 @@ function CompanyDashboardContent({
201204
items: [
202205
{
203206
title: 'Events',
204-
url: `/dashboard/company/${companySlug}/events`,
207+
url: `/dashboard/company/${effectiveSlug}/events`,
205208
icon: Calendar,
206209
},
207210
{
208211
title: 'Hackathons',
209-
url: `/dashboard/company/${companySlug}/hackathons`,
212+
url: `/dashboard/company/${effectiveSlug}/hackathons`,
210213
icon: Trophy,
211214
},
212215
{
213216
title: 'Team',
214-
url: `/dashboard/company/${companySlug}/team`,
217+
url: `/dashboard/company/${effectiveSlug}/team`,
215218
icon: Users,
216219
},
217220
],
@@ -221,7 +224,7 @@ function CompanyDashboardContent({
221224
items: [
222225
{
223226
title: 'Analytics',
224-
url: `/dashboard/company/${companySlug}/analytics`,
227+
url: `/dashboard/company/${effectiveSlug}/analytics`,
225228
icon: BarChart3,
226229
},
227230
],
@@ -231,12 +234,12 @@ function CompanyDashboardContent({
231234
items: [
232235
{
233236
title: 'Company Settings',
234-
url: `/dashboard/company/${companySlug}/settings`,
237+
url: `/dashboard/company/${effectiveSlug}/settings`,
235238
icon: Settings,
236239
},
237240
{
238241
title: 'Subscription',
239-
url: `/dashboard/company/${companySlug}/subscription`,
242+
url: `/dashboard/company/${effectiveSlug}/subscription`,
240243
icon: CreditCard,
241244
},
242245
],

lib/hooks/useRoleProtection.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@ export function useRoleProtection(requiredRole: 'student' | 'company_member') {
2020
return
2121
}
2222

23-
// Check if user is a company member (FIXED: changed from 'company_users' to 'company_members')
23+
// Check if user is a company member and get their company slug
2424
const { data: companyMembership } = await supabase
2525
.from('company_members')
26-
.select('id')
26+
.select(`
27+
id,
28+
company:companies(slug)
29+
`)
2730
.eq('user_id', user.id)
31+
.eq('status', 'active')
2832
.maybeSingle()
2933

3034
const isCompanyMember = !!companyMembership
@@ -39,7 +43,15 @@ export function useRoleProtection(requiredRole: 'student' | 'company_member') {
3943
} else if (requiredRole === 'student') {
4044
if (isCompanyMember) {
4145
// User is a company member trying to access student routes
42-
router.push('/dashboard/company')
46+
// Redirect to their company dashboard with the correct slug
47+
const company = companyMembership.company as unknown as { slug: string } | null
48+
const companySlug = company?.slug
49+
if (companySlug) {
50+
router.push(`/dashboard/company/${companySlug}`)
51+
} else {
52+
// Fallback if slug is not available
53+
router.push('/dashboard/company')
54+
}
4355
return
4456
}
4557
}

0 commit comments

Comments
 (0)