mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-22 03:50:30 +00:00
658536c217
Removing a member deleted their membership row only, and the invite link checked nothing but its own token, so whoever kept the link walked straight back in (GHSA-wr4c-964p-39jg). trip_member_removals now notes who another member took off a trip. The invite link refuses them on preview and on accept with a 403 that says so, old link or regenerated, and the join page shows that instead of the join button. Adding them back by name, or through a trip-bound invite or a plugin acting for a member manager, lifts the note. Leaving a trip yourself leaves none, so the link keeps working for whoever left and for everybody else.
65 lines
2.5 KiB
TypeScript
65 lines
2.5 KiB
TypeScript
import { useEffect, useState } from 'react'
|
|
import { useNavigate, useParams } from 'react-router'
|
|
import { tripInviteApi } from '../../api/client'
|
|
import { useTranslation } from '../../i18n'
|
|
import { getApiErrorMessage } from '../../types'
|
|
import { useToast } from '../../components/shared/Toast'
|
|
|
|
export type JoinTripState = 'loading' | 'ready' | 'joining' | 'invalid' | 'removed'
|
|
|
|
/** A 403 that says somebody took this user off the trip: the link cannot bring them back. */
|
|
function isRemovedRefusal(err: unknown): boolean {
|
|
const response = (err as { response?: { status?: number; data?: { removed?: unknown } } }).response
|
|
return response?.status === 403 && response.data?.removed === true
|
|
}
|
|
|
|
/**
|
|
* State + effects behind JoinTripPage (#1143): resolve the invite token to a
|
|
* trip name, then accept it (add the current user as a member) and open the trip.
|
|
* The page itself is a thin presentational shell over this hook.
|
|
*/
|
|
export function useJoinTrip() {
|
|
const { token } = useParams<{ token: string }>()
|
|
const navigate = useNavigate()
|
|
const { t } = useTranslation()
|
|
const toast = useToast()
|
|
|
|
const [state, setState] = useState<JoinTripState>('loading')
|
|
const [title, setTitle] = useState('')
|
|
|
|
useEffect(() => {
|
|
let cancelled = false
|
|
if (!token) { setState('invalid'); return }
|
|
tripInviteApi.preview(token)
|
|
.then((data: { title: string }) => { if (!cancelled) { setTitle(data.title); setState('ready') } })
|
|
.catch((err: unknown) => { if (!cancelled) setState(isRemovedRefusal(err) ? 'removed' : 'invalid') })
|
|
return () => { cancelled = true }
|
|
}, [token])
|
|
|
|
const accept = () => {
|
|
if (!token) return
|
|
setState('joining')
|
|
tripInviteApi.accept(token)
|
|
.then((data: { trip_id: number }) => navigate(`/trips/${data.trip_id}`, { replace: true }))
|
|
.catch((err: unknown) => {
|
|
// Only a 404 means the invite itself is gone. A rate limit or a server
|
|
// having a bad minute used to be reported as an expired invite too,
|
|
// which sent the user back to the dashboard with no way to try again.
|
|
if ((err as { response?: { status?: number } }).response?.status === 404) {
|
|
setState('invalid')
|
|
return
|
|
}
|
|
if (isRemovedRefusal(err)) {
|
|
setState('removed')
|
|
return
|
|
}
|
|
setState('ready')
|
|
toast.error(getApiErrorMessage(err, t('common.error')))
|
|
})
|
|
}
|
|
|
|
const goToDashboard = () => navigate('/dashboard', { replace: true })
|
|
|
|
return { state, title, accept, goToDashboard }
|
|
}
|