Pre-requisites: You need to set up your Kobble account.
Install the SDK
1
Install @kobbleio/react
Install the @kobbleio/react sdk with the provider of your choice.
2
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:3
Setup the environment variables
Create a
.env
file at the root of your project and add the following environment variables.KOBBLE_DOMAIN and KOBBLE_CLIENT_ID can be found in your Kobble dashboard.
4
Create a callback handler
Install and configure React Router.Then create a new page view to handle our callback
If you’re not familiar with React Router, you can check our router configuration here.
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()
TheuseAuth
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()
TheuseAuthStateChanged
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.
The callback may be called multiple times. For instance when you refresh the page it will be initially called with a
null
user and then the User
object if authenticated.useAccessControl()
TheuseAccessControl
hook allows you to check if the user has the required permissions or quotas to access a specific resource.
You must attribute quotas and/or permissions to your Products from your Kobble dashboard for them to work.
Check permissions
Check remaining quotas
useKobble()
TheuseKobble
hook allows you to get the kobble
client instance that exposes various utility methods.
Get user state
Even if you can do it using the Kobble instance we suggest you do it using the
useAuth
hook explained above.Get access and ID tokens
Listen for state changes
TheonAuthStateChanged
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.
The callback may be called multiple times. For instance when you refresh the page it will be initially called with a
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:
The onClick event will automatically get handled by the SDK, don’t pass any handler.
<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:
The onClick event will automatically get handled by the SDK, don’t pass any handler.
<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.