Note: useSession()
uses suspense by default, so you need a <Suspense>
component above it in the tree. Or you can set
useSession({suspense: false})
to disable suspense.
import { useSession } from "@blitzjs/auth"
function SomeComponent() {
const session = useSession()
session.userId
session.role
return /*... */
}
options:
initialPublicData: PublicData
- Use this with SSR to set public data
from the server sessionsuspense: boolean
- Defaults to true
session: Partial<PublicData> & {isLoading: boolean}
useAuthenticatedSession()
useAuthenticatedSession(options?) => PublicData & {isLoading: boolean}
This will throw AuthenticationError
if the user is not logged in
import { useAuthenticatedSession } from "@blitzjs/auth"
const session = useAuthenticatedSession()
options:
initialPublicData: PublicData
- Use this with SSR to set public data
from the server sessionsuspense: boolean
- Defaults to true
session: PublicData & {isLoading: boolean}
useAuthorize()
useAuthorize() => void
This will throw AuthenticationError
if the user is not logged in
import { useAuthorize } from "@blitzjs/auth"
useAuthorize()
useRedirectAuthenticated()
useRedirectAuthenticated(to: string) => void
This will redirect a logged in user to the given url path. It does nothing for logged out users.
import { useRedirectAuthenticated } from "@blitzjs/auth"
useRedirectAuthenticated("/dashboard")
to: string