Docs
/
/
In-App UI
React
Hooks
useKnockAuthState

useKnockAuthState

The useKnockAuthState hook subscribes to a Knock client's authentication state, re-rendering your component when the user signs in, signs out, or switches. It's backed by the subscribable authStore on the client, so it stays correct even when the client is re-authenticated in place — for example, when you toggle the enabled prop on KnockProvider.

Parameters

#

This hook accepts a single argument:

knockKnockRequired

An authenticated Knock client, such as the instance returned by the useKnockClient hook.

Returns

#

Returns a KnockAuthState object describing the client's current authentication.

status'authenticated' | 'unauthenticated'

Whether a user is authenticated to the client.

userIdstring | undefined | null

The ID of the authenticated user, or undefined when no user is authenticated.

userTokenstring | undefined

The user token in use for the authenticated user, when one was provided.

Example

#

The following example reads the authentication state to render different UI while a user signs in. It pairs well with the enabled prop on KnockProvider, which flips the client between signed-out and signed-in as the identity loads.

New chat