Files
accounted/app/api/team/accept/route.ts
T
Jakob Wennberg 4253afc343 fix(company): validate user_preferences write when switching company (#708)
Fixes #701. setActiveCompany upserted active_company_id without checking
the result, then set the gnubok-company-id cookie unconditionally. A failed
write — including an RLS-filtered UPDATE, which affects zero rows without
raising an error — looked like a successful switch: switchCompany returned
{}, the UI hard-reloaded, and middleware (which reads user_preferences, not
the cookie) resolved the old company.

- setActiveCompany now verifies the upsert with .select().single() and
  throws a typed CompanyContextError ('not_member' | 'persist_failed');
  the cookie is only set after the write is confirmed, so it can no longer
  diverge from the database.
- switchCompany logs the failure and returns distinct error codes instead
  of reporting every failure as a permissions problem.
- CompanySwitcher now shows a destructive toast on failure (it previously
  failed with no feedback); BankIdCompanyPicker translates the codes.
  Messages added to sv/en under company_switcher and select_company.
- The remaining fire-and-forget user_preferences writers (middleware
  fallback write-back, team invite accept, auth callback invite accept)
  now check and log errors; non-fatal by design since each has a working
  fallback path.
- New tests cover every failure mode, including cookie-not-set on a failed
  write and the silent zero-row write caught by the read-back.

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-06-11 09:54:04 +02:00

139 lines
4.4 KiB
TypeScript

import { createClient, createServiceClient } from '@/lib/supabase/server'
import { NextResponse, type NextRequest } from 'next/server'
import { hashInviteToken } from '@/lib/auth/invite-tokens'
/**
* GET /api/team/accept?token=xxx
* Validates an invite token and returns invite info (for the invite page).
* Only company invitations are supported — team invitations are disabled.
* No auth required — this is a public endpoint.
*/
export async function GET(request: NextRequest) {
const token = request.nextUrl.searchParams.get('token')
if (!token) {
return NextResponse.json({ error: 'Token saknas.' }, { status: 400 })
}
const tokenHash = hashInviteToken(token)
const serviceClient = createServiceClient()
const { data: companyInvite } = await serviceClient
.from('company_invitations')
.select('id, email, status, expires_at, company_id, companies:company_id(name)')
.eq('token_hash', tokenHash)
.single()
if (!companyInvite) {
return NextResponse.json({ error: 'Inbjudan hittades inte eller är ogiltig.' }, { status: 404 })
}
if (companyInvite.status !== 'pending') {
return NextResponse.json({ error: 'Inbjudan har redan använts.' }, { status: 410 })
}
const expired = new Date(companyInvite.expires_at) < new Date()
const { data: alreadyHasAccount } = await serviceClient.rpc('check_email_exists', {
email_to_check: companyInvite.email,
})
return NextResponse.json({
data: {
type: 'company',
companyName: (companyInvite.companies as unknown as { name: string })?.name || 'Företag',
email: companyInvite.email,
expired,
alreadyHasAccount,
},
})
}
/**
* POST /api/team/accept
* Accepts a company invite after the user has signed up.
* Team invitations are disabled — teams are single-user.
*/
export async function POST(request: NextRequest) {
const supabase = await createClient()
const { data: { user } } = await supabase.auth.getUser()
if (!user) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
}
const body = await request.json()
const token = body.token as string
if (!token) {
return NextResponse.json({ error: 'Token saknas.' }, { status: 400 })
}
const tokenHash = hashInviteToken(token)
const serviceClient = createServiceClient()
const { data: companyInvite, error: companyLookupError } = await serviceClient
.from('company_invitations')
.select('id, company_id, email, role, status, expires_at')
.eq('token_hash', tokenHash)
.single()
if (companyLookupError) {
console.error('[team/accept] company lookup error:', companyLookupError.message)
}
if (!companyInvite || companyInvite.status !== 'pending') {
return NextResponse.json({ error: 'Inbjudan är ogiltig.' }, { status: 400 })
}
if (new Date(companyInvite.expires_at) < new Date()) {
await serviceClient
.from('company_invitations')
.update({ status: 'expired' })
.eq('id', companyInvite.id)
return NextResponse.json({ error: 'Inbjudan har gått ut.' }, { status: 410 })
}
if (user.email?.toLowerCase() !== companyInvite.email.toLowerCase()) {
return NextResponse.json({ error: 'E-postadressen matchar inte inbjudan.' }, { status: 403 })
}
// Add user to company
const { error: memberError } = await serviceClient
.from('company_members')
.insert({
company_id: companyInvite.company_id,
user_id: user.id,
role: companyInvite.role,
source: 'direct',
})
if (memberError) {
if (memberError.code === '23505') {
return NextResponse.json({ error: 'Du är redan medlem.' }, { status: 409 })
}
return NextResponse.json({ error: 'Kunde inte lägga till medlem.' }, { status: 500 })
}
// Set active company. Non-fatal on failure — the membership insert already
// succeeded and middleware falls back to it — but log so silent
// persistence failures (#701) are observable.
const { error: prefError } = await serviceClient
.from('user_preferences')
.upsert({
user_id: user.id,
active_company_id: companyInvite.company_id,
}, { onConflict: 'user_id' })
if (prefError) {
console.error('[team/accept] failed to set active company', prefError)
}
// Mark invite as accepted
await serviceClient
.from('company_invitations')
.update({ status: 'accepted' })
.eq('id', companyInvite.id)
return NextResponse.json({
data: { type: 'company', companyId: companyInvite.company_id },
})
}