TypeScript SDK
Kinde’s TypeScript SDK allows developers to integrate Kinde Authentication into their JavaScript or TypeScript projects. This SDK implements the following OAuth2.0 flows. Learn more here
- Authorization Code - Intended for confidential clients, e.g. web-servers
- Authorization Code with PKCE extension - For public clients, e.g. single page web application and or mobile applications
- Client Credentials flow - Intended for confidential clients, where machine to machine communication is required.
Requirements
Link to this sectionNode version 16 or newer
Register for Kinde
Link to this sectionIf you haven’t already got a Kinde account, register for free here (no credit card required). Registering gives you a Kinde domain, which you need to get started, e.g. <your_subdomain>.kinde.com
.
Install
Link to this sectionConfigure Kinde
Link to this sectionSet callback URLs
Link to this sectionKinde will redirect your user to authenticate. They’ll be redirected back to your JavaScript app after signing in or signing up.
To authenticate your app, you need to specify which URL Kinde should redirect your user.
-
In Kinde, go to Settings > Applications and then navigate to Front-end app or Back-end app which ever applies - or add a new application.
-
Add your callback URLs in the relevant fields. For example:
-
Allowed callback URLs (also known as redirect URIs):
https://<your_app_domain>/callback
e.g:http://localhost:3000/callback
-
Allowed logout redirect URLs:
https://<your_app_domain>
e.g:http://localhost:3000
-
-
Select Save.
Add environments
Link to this sectionKinde comes with a production environment, but you can set up other environments if you want to. Each environment has a unique subdomain so be sure to use the correct one in the Configure your app section which follows.
Integrate with your app
Link to this sectionFirst step is to configure and create a client. The following settings are needed depending on which authentication flow you are using.
You can find these values on your Settings > Applications > [Your app] > View details page.
authDomain
- your Kinde domainclientId
- your Kinde client IDclientSecret
- your Kinde client secretredirectURL
- your callback url to redirect to after authentication. Make sure this URL is under your Allowed callback URLs.logoutRedirectURL
- where you want users to be redirected to after logging out. Make sure this URL is under your Allowed logout redirect URLs.
Log in and register
Link to this sectionTo incorporate the login and register features, you’ll need to redirect to Kinde for authentication. One way to do this is to create routes for /login
and /register
.
With that in place you can simply add links in your HTML as follows:
In the above example there is a sessionManager
which has not been defined. In order to track the authenticated session between requests a session store is required. Any key-value store can be used for this, you just need to implement the SessionManager
interface to provide it to the SDK.
An example session manager storing in memory could be implemented as:
This would work for a single user for local development purposes, but would need to be expanded for a production environment.
The appropriate session store for your application will depend on your application architecture, for example encrypted cookies in a stateless server environment or a shared cache/database for a load balanced cluster of servers.
Commonly, the session manager will be a wrapper around an existing session management library - often provided by a web framework, or a third party library.
Manage redirects
Link to this sectionYou will also need to route /callback
. When the user is redirected back to your site from Kinde, it will trigger a call to the callback URL defined in the redirectURL
client option.
Logout
Link to this sectionThe Kinde SDK comes with a logout method.
Check if user is authenticated
Link to this sectionWe’ve provided a helper to get a boolean value to check if a user is signed in by verifying that the access token is still valid. The isAuthenticated
function is only available for authentication code and PKCE flows.
View user profile
Link to this sectionYou need to have already authenticated before you call the API, otherwise an error will occur.
To access the user information, use the getUserProfile
helper function:
View users in Kinde
Link to this sectionGo to the Users page in Kinde to see your newly registered user.
Audience
Link to this sectionAn audience
is the intended recipient of an access token - for example the API for your application. The audience argument can be passed to the Kinde client to request an audience be added to the provided token.
Overriding scope
Link to this sectionBy default the Kinde SDK requests the following scopes:
profile
email
offline
openid
You can override this by passing scopes into the Kinde SDK
Organizations
Link to this sectionCreate an organization
Link to this sectionTo have a new organization created within your application during registration, you can create a route as follows:
You can also pass org_name
as part of the query string as per the following HTML:
Log in and register to organizations
Link to this sectionThe Kinde client provides methods for you to easily log in and register users into existing organizations.
Update the routes to accept an org_code
parameter and pass it to the SDK:
Following authentication, Kinde provides a JSON web token (JWT) to your application. Along with the standard information we also include the org_code
and the permissions for that organization (this is important as a user can belong to multiple organizations and have different permissions for each).
Example of a returned token:
The id_token
will also contain an array of organizations that a user belongs to - this is useful if you wanted to build out an organization switcher for example.
There are two helper functions you can use to extract information:
User permissions
Link to this sectionOnce a user has been verified, your product/application will return the JWT token with an array of permissions for that user. You will need to configure your product/application to read permissions and unlock the respective functions.
Set permissions in your Kinde account. Here’s an example set of permissions.
We provide helper functions to more easily access the permissions claim:
A practical example in code might look something like:
Getting claims
Link to this sectionWe have provided a helper to grab any claim from your id or access tokens. The helper defaults to access tokens:
Feature Flags
Link to this sectionWe have provided a helper to return any features flag from the access token:
We also require wrapper functions by type which should leverage getFlag
above.
Get boolean flags
Link to this sectionGet string flags
Link to this sectionGet integer flags
Link to this sectionToken storage
Link to this sectionAfter the user has successfully logged in, you will have a JSON Web Token (JWT) and a refresh token securely stored. You can retrieve an access token by utilizing the getToken
method.
Kinde Management API
Link to this sectionThe sections below relate to how to call the Kinde Management API. You need to add a machine to machine application and get an access token to connect:
SDK API reference
Link to this sectionauthDomain
Link to this sectionEither your Kinde instance url or your custom domain. e.g. https://yourapp.kinde.com
.
Type: string
Required: Yes
redirectUri
Link to this sectionThe url that the user will be returned to after authentication.
Type: string
Required: Yes
LogoutRedirectUri
Link to this sectionThe url that the user will be returned to after they sign out.
Type: string
Required: Yes
clientId
Link to this sectionThe ID of your application in Kinde.
Type: string
Required: Yes
clientSecret
Link to this sectionThe unique client secret associated with your application in Kinde.
Type: string
Required: Yes
scope
Link to this sectionThe scopes to be requested from Kinde.
Type: string
Required: No
Default: openid profile email offline
audience
Link to this sectionThe audience claim for the JWT.
Type: string
Required: No
Kinde SDK methods
Link to this sectionlogin
Link to this sectionConstructs a redirect URL and sends the user to Kinde to sign in.
Arguments:
Usage:
register
Link to this sectionConstructs a redirect URL and sends the user to Kinde to sign up.
Arguments:
Usage:
logout
Link to this sectionLogs the user out of Kinde.
Arguments:
Usage:
handleRedirectToApp
Link to this sectionCallback middleware function for Kinde OAuth 2.0 flow.
Arguments:
Usage:
isAuthenticated
Link to this sectionCheck if the user is authenticated.
Arguments:
Usage:
Output: true
or false
createOrg
Link to this sectionConstructs redirect url and sends user to Kinde to sign up and create a new org for your business.
Arguments:
Usage:
getClaim
Link to this sectionExtract the provided claim from the provided token type in the current session, the returned object includes the provided claim.
Arguments:
Usage:
getClaimValue
Link to this sectionExtract the provided claim from the provided token type in the current session.
Arguments:
Usage:
Output: 'David'
getPermission
Link to this sectionReturns the state of a given permission.
Arguments:
Usage:
Output sample:
getPermissions
Link to this sectionReturns all permissions for the current user for the organization they are logged into.
Arguments:
Usage:
Sample output:
getOrganization
Link to this sectionGet details for the organization your user is logged into.
Arguments:
Usage:
Sample output:
getUserOrganizations
Link to this sectionGets an array of all organizations the user has access to.
Arguments:
Usage:
Sample output:
getUser
Link to this sectionExtracts the user details from the ID token obtained post authentication.
Arguments:
Usage:
getToken
Link to this sectionReturns a valid access token if available.
Arguments:
Usage:
refreshTokens
Link to this sectionUses the refresh token to update and return new tokens.
Arguments:
Usage:
getUserProfile
Link to this sectionExtracts makes use of the getToken
method above to fetch user details.
Arguments:
Usage:
Sample output:
getFlag
Link to this sectionGet a flag from the feature_flags claim of the access_token
.
Arguments:
Usage:
Sample output:
getBooleanFlag
Link to this sectionGet a boolean flag from the feature_flags
claim of the access token.
Arguments:
Usage:
Sample output: true
getStringFlag
Link to this sectionGet a string flag from the feature_flags
claim of the access token.
Arguments:
Usage:
Sample output: pink
getIntegerFlag
Link to this sectionGet an integer flag from the feature_flags
claim of the access token.
Arguments:
Usage:
Sample output: 2
If you need help connecting to Kinde, please contact us at support@kinde.com.