Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/app/dashboard/[teamIdOrSlug]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import { AUTH_URLS } from '@/configs/urls'
import { DashboardContextProvider } from '@/features/dashboard/context'
import DashboardLayoutView from '@/features/dashboard/layout/layout'
import Sidebar from '@/features/dashboard/sidebar/sidebar'
import { l } from '@/lib/clients/logger/logger'
import { getSessionInsecure } from '@/server/auth/get-session'
import getUserByToken from '@/server/auth/get-user-by-token'
import { getTeam } from '@/server/team/get-team'
import { SidebarInset, SidebarProvider } from '@/ui/primitives/sidebar'
import { cookies } from 'next/headers'
import { redirect, unauthorized } from 'next/navigation'
import { Metadata } from 'next/types'
import { serializeError } from 'serialize-error'

export const metadata: Metadata = {
title: 'Dashboard - E2B',
Expand Down Expand Up @@ -48,6 +50,17 @@ export default async function DashboardLayout({
const team = teamRes?.data

if (!team) {
l.warn(
{
key: 'dashboard_layout:team_not_resolved',
user_id: data.user.id,
error: serializeError(teamRes?.serverError),
context: {
teamIdOrSlug,
},
},
`dashboard_layout:team_not_resolved - team not resolved for user (${data.user.id}) when accessing team (${teamIdOrSlug}) in dashboard layout`
)
throw unauthorized()
}

Expand Down
20 changes: 20 additions & 0 deletions src/lib/clients/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,32 @@ export const withTeamIdResolution = createMiddleware<{
const teamId = await getTeamIdFromSegment(clientInput.teamIdOrSlug as string)

if (!teamId) {
l.warn(
{
key: 'with_team_id_resolution:invalid_team_id_or_slug',
context: {
teamIdOrSlug: clientInput.teamIdOrSlug,
},
},
`with_team_id_resolution:invalid_team_id_or_slug - invalid team id or slug provided through withTeamIdResolution middleware: ${clientInput.teamIdOrSlug}`
)

throw unauthorized()
}

const isAuthorized = await checkUserTeamAuthorization(ctx.user.id, teamId)

if (!isAuthorized) {
l.warn(
{
key: 'with_team_id_resolution:user_not_authorized',
context: {
teamIdOrSlug: clientInput.teamIdOrSlug,
},
},
`with_team_id_resolution:user_not_authorized - user not authorized to access team: ${clientInput.teamIdOrSlug}`
)

throw unauthorized()
}

Expand Down
12 changes: 6 additions & 6 deletions src/lib/schemas/team.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { z } from 'zod'

export const TeamIdOrSlugSchema = z.union([
z.uuid(),
z
.string()
.regex(
/^[a-z0-9]+(-[a-z0-9]+)*$/i,
'Must be a valid slug (words separated by hyphens)'
),
z.string(),
// FIXME: Add correct team regex as in db slug generation
// .regex(
// /^[a-z0-9]+(-[a-z0-9]+)*$/i,
// 'Must be a valid slug (words separated by hyphens)'
// ),
])
13 changes: 11 additions & 2 deletions src/server/team/get-team-id-from-segment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'server-cli-only'
import 'server-only'

import { CACHE_TAGS } from '@/configs/cache'
import { l } from '@/lib/clients/logger/logger'
Expand Down Expand Up @@ -29,7 +29,16 @@ export const getTeamIdFromSegment = async (segment: string) => {
}

if (z.uuid().safeParse(segment).success) {
return segment
// make sure this uuid is a valid teamId and is not it's slug
const { data } = await supabaseAdmin
.from('teams')
.select('id')
.not('slug', 'eq', segment)
.eq('id', segment)

if (data?.length) {
return data[0]!.id
}
}

const { data, error } = await supabaseAdmin
Expand Down
Loading