React
Learn how to add authentication to your React application using Kobble SDK.
Install the SDK
Install @kobbleio/react
Install the @kobbleio/react sdk with the provider of your choice.
Use the KobbleProvider
To use the SDK, you must wrap your App component with the KobbleProvider
component.
Here’s an example that also uses React Router:
Setup the environment variables
Create a .env
file at the root of your project and add the following environment variables.
Create a callback handler
Install and configure React Router.
Then create a new page view to handle our callback src/pages/Callback.tsx
.
Use the <HandleCallback />
component to handle the callback and redirect the user to the home page automatically.
Hooks
The SDK exposes many hooks that you can use to interact with the authentication state of your application.
useAuth()
The useAuth
hook allows you to get the authentication state of the user. Instead of using the kobble
client instance, you can directly get the user object from the react context.
useAuthStateChanged()
The useAuthStateChanged
hook allows you to listen for changes in the authentication state of the user.
The callback function will be called with the user object when the user is signed in, and null
when the user is signed out.
null
user and then the User
object if authenticated.useAccessControl()
The useAccessControl
hook allows you to check if the user has the required permissions or quotas to access a specific resource.
Check permissions
Check remaining quotas
useKobble()
The useKobble
hook allows you to get the kobble
client instance that exposes various utility methods.
Get user state
useAuth
hook explained above.Get access and ID tokens
Listen for state changes
The onAuthStateChanged
method allows you to listen for changes in the authentication state of the user.
The callback function will be called with the user object when the user is signed in, and null
when the user is signed out.
null
user and then the User
object if authenticated.Components
The SDK exposes a few components that you can use to interact with the authentication state of your application seemlessly.
<SignedIn>
The <SignedIn>
component allows you to show content only when the user is signed in.
<SignedOut>
The <SignedOut>
component allows you to show content only when the user is signed out.
<LoginButton>
The <LoginButton>
component allows you to show a button that triggers the login flow and redirects the user to your authentication portal.
You can use the component as it or pass it a child to customize it.
Default usage:
Custom usage:
<LogoutButton>
The <LogoutButton>
component allows you to show a button that triggers the logout flow.
You can use the component as it or pass it a child to customize it.
Default usage:
Custom usage:
<IsAllowed>
The <IsAllowed>
component allows you to show content only when the user has the required permissions or quotas to access a specific resource.
It takes a permission
or quota
prop that you can use to check if the user has the required permissions or quotas.
Both permission
and quota
props can be either a string or an array of strings.
<IsForbidden>
The <IsForbidden>
component allows you to show content only when the user doesn’t have the required permissions or quotas to access a specific resource.
It takes a permission
or quota
prop that you can use to check if the user doesn’t have the required permissions or quotas.
Both permission
and quota
props can be either a string or an array of strings.
<ProfileLink>
The <ProfileLink>
component allows you to show a link to the user’s profile of your Customer Portal.
It must take a child that will be used as the link text.
<PricingLink>
The <PricingLink>
component allows you to show a link to the pricing page of your Customer Portal.
It must take a child that will be used as the link text.
<HandleCallback>
The <HandleCallback>
component allows you to handle the callback from the authentication portal and redirect the user to the desired page.
It takes a navigate
prop that you can use to redirect the user to the desired page after the callback is handled.