Implementation
To integrate JWT-based authentication with Privy in your React application, you’ll need to use theuseSubscribeToJwtAuthWithFlag
hook to subscribe the Privy SDK to your auth provider’s
state.Getting the state from your auth provider
To get the state from your auth provider, import the provider’s hook.Subscribing to the auth provider’s state
In a component that lives below bothPrivyProvider
, and your custom auth provider, call the
useSubscribeToJwtAuthWithFlag
hook to subscribe the Privy SDK to your auth provider’s state.MyStateSyncComponent
component) should be mounted throughout the
lifetime of your app to ensure state is kept in sync.Integrate the provider with your app
Make sure to nest your custom provider inside your auth provider in your app structure:App.tsx
Disabling the external auth provider
If you want to disable the external auth provider, you can set theenabled
flag to false
in
the hook configuration.enabled
flag to false
will disable the external auth provider and will stop
Privy from attempting to synchronize its state with the external auth provider regardless of the
value of the isAuthenticated
flag, until enabled
is set to true
again.Advanced Usage
This approach is not recommended for most use cases, as it increases the complexity of setup significantly and
can result in state synchronization issues if used incorrectly.Always prefer the flag-based approach when possible if your auth provider offers an
isAuthenticated
flag.isAuthenticated
flag, you can use the useSyncJwtBasedAuthState
hook to subscribe to
the auth provider’s state via state listeners.Let’s say the library for your auth provider exports an authStore
object that holds state.subscribe
method that takes a callback, and invokes it every time the
auth state changes, most importantly when the user either logs in or out.getState
method that returns the current state, which we can use to
get the current JWT token whenever necessary.useSyncJwtBasedAuthState
hook, we can link Privy to the auth provider’s state store
by using those two methods.Accessing User Authentication Status
Once configured, you can access the user’s authentication status through the Privy SDK:When using a custom authentication provider, you should not use the Privy
login
method (from useLogin
or usePrivy
). Instead, call the login method of your custom provider, and the Privy SDK will automatically synchronize its state.