@parcely/auth-redirect
v1.0.0
Published
Browser-side login-redirect interceptor for parcely. Redirects on 401/403 with optional return_to preservation and cooldown debounce.
Maintainers
Readme
@parcely/auth-redirect
Browser-side login-redirect interceptor for parcely. On 401 / 403, redirects to a login URL — with optional return_to preservation, a shouldRedirect predicate, and cooldown debounce to prevent redirect storms.
npm install @parcely/core @parcely/auth-redirectimport { createClient } from '@parcely/core'
import { createAuthRedirect } from '@parcely/auth-redirect'
const http = createClient({ baseURL: 'https://api.example.com' })
const redirect = createAuthRedirect({
loginUrl: '/login', // string | (err) => string
on: [401, 403], // default
preserveReturnTo: true, // appends ?return_to=<encoded current href>
returnToParam: 'return_to',
shouldRedirect: (err) => !err.config.url?.includes('/public/'),
cooldownMs: 2_000, // debounce repeated redirects
})
redirect.install(http)Behaviour
- Triggers on an
HttpErrorwithcode === 'ERR_HTTP_STATUS'andstatusinon. - Appends
?<returnToParam>=<encodedCurrentHref>whenpreserveReturnTo. shouldRedirect(err)returningfalsesuppresses the redirect.- Cooldown prevents a second redirect within
cooldownMsof the first. - Non-browser runtime: no-op with a one-shot
console.warn— safe to install in isomorphic code. - Always rethrows the original error after (or instead of) triggering a redirect.
Ordering with @parcely/auth-token
Install auth-token first and auth-redirect second. Refresh-on-401 gets first crack at the error; if refresh succeeds, the retried request typically resolves and the redirect never fires. If refresh fails or isn't configured, the redirect handles it.
License
MIT
