Skip to content

Bastion Auth ​

A collection of authentication components and utilities built around Bastion SSO. Includes session management via TanStack Query, a profile dropdown, sign-out dialog, and pre-configured Axios instances.

Installation ​

bash
npx shadcn@latest add https://befame-registry.example.com/r/auth-provider.json

AuthProvider ​

Wraps your app (or a route subtree) to validate the current session on mount. On a 401 response from /auth/me it automatically redirects to the Bastion SSO page. While loading, a centered spinner is shown.

Usage ​

tsx
import { AuthProvider } from '@/components/bastion/auth-provider'

export default function App() {
  return (
    <AuthProvider config={{ allowGuest: true, redirectOnUnauthenticated: false }}>
      <RouterOutlet />
    </AuthProvider>
  )
}

Guest mode ​

Set allowGuest to let unauthenticated users view the app without being redirected.

Disabling redirects (development) ​

Set redirectOnUnauthenticated to false to skip the SSO redirect while developing locally.

Props ​

PropTypeDefaultDescription
childrenReactNode—Content to render once the session is validated.
config.allowGuestbooleanfalseWhen true, unauthenticated users are not redirected.

useUserInfo hook ​

Access the current user from any component inside AuthProvider.

tsx
import { useUserInfo } from '@/components/bastion/auth-provider'

function MyComponent() {
  const { value: user } = useUserInfo()
  return <p>Hello, {user?.givenName}</p>
}

Returns AuthContextValue:

FieldTypeDescription
valueAuthUser | nullThe currently authenticated user, or null.
set(data: AuthUser) => voidManually update the in-memory user (rarely needed).

AuthUser shape ​

ts
interface AuthUser {
  id: string
  login: string
  name: string
  givenName: string
  identityType: number
  roles: string[]
  email: string | null
  phone: string | null
}

ProfileDropdown ​

An avatar button that opens a dropdown with the user's name/email, a link to the Bastion profile page, and a sign-out action. When no user is authenticated, a Zaloguj się button is shown instead.

Usage ​

tsx
import { ProfileDropdown } from '@/components/bastion/profile-dropdown'

<ProfileDropdown />

Place it in your header or sidebar — it reads the current user from useAuthStore automatically.


SignOutDialog ​

A confirmation dialog that calls the logout endpoint, resets the auth store, and closes itself.

Usage ​

tsx
import { SignOutDialog } from '@/components/bastion/sign-out-dialog'
import { useState } from 'react'

function MyHeader() {
  const [open, setOpen] = useState(false)

  return (
    <>
      <button onClick={() => setOpen(true)}>Sign out</button>
      <SignOutDialog open={open} onOpenChange={setOpen} />
    </>
  )
}

Props ​

PropTypeDescription
openbooleanWhether the dialog is visible.
onOpenChange(open: boolean) => voidCalled when the dialog should open or close.

Utilities (utils.ts) ​

buildBastionRedirectUrl(redirectUri?) ​

Returns the Bastion SSO URL, optionally including a post-login redirect and the app ID.

ts
import { buildBastionRedirectUrl } from '@/components/bastion/utils'

// Redirect to SSO, then back to the current page
window.location.href = buildBastionRedirectUrl(window.location.href)
ParameterTypeDescription
redirectUristring (optional)URL the user is sent to after login.

buildBastionProfileUrl() ​

Returns the Bastion management URL (no parameters).

ts
import { buildBastionProfileUrl } from '@/components/bastion/utils'

window.open(buildBastionProfileUrl(), '_blank')

getApiUrl(endpoint) ​

Prepends API_BASE_URL to a relative endpoint string.

ts
import { getApiUrl } from '@/components/bastion/utils'

const url = getApiUrl('/users') // → https://example.com/api/v1/users

api / grotApi ​

Pre-configured Axios instances with withCredentials: true and JSON content-type headers. Both include a response interceptor that redirects to the Bastion SSO page on 401 (after confirming the session is truly expired).

ts
import { api, grotApi } from '@/components/bastion/utils'

// Use like a normal Axios instance
const { data } = await api.get('/users')
const { data: me } = await grotApi.get('/auth/me')
InstanceBase URL env varTypical use
apiVITE_API_URL / VITE_APP_BASE_URLApp-specific backend
grotApiVITE_APP_GROT_API_URLBastion / Grot auth API

Environment Variables ​

VariableRequiredDescription
VITE_BASTION_URLYesBase URL of the Bastion service.
VITE_APP_IDNoApp identifier sent to Bastion SSO.
VITE_API_URLDev onlyFull base URL for api in development.
VITE_APP_BASE_URLProdBase URL prefix; api/v1 is appended.
VITE_APP_GROT_API_URLYesBase URL for grotApi (Grot/Bastion API).
VITE_DISABLE_AUTH_REDIRECTNoSet to "true" to disable SSO redirects locally.