JavaScript SDK
If you haven’t already got a Kinde account, register for free here (no credit card required). This will give you a Kinde domain, which you need to get started, e.g. yourapp.kinde.com
You can also view the JavaScript starter kit in GitHub.
Set up Kinde
Link to this sectionSet your callback and logout 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. These need to match the ones listed in your application details in Kinde.
The http://localhost:3000
is an example of a commonly used local development URL. It should be replaced with the URL where your app is running.
- In Kinde, go to Settings > Applications > [your app] > View details.
- Set the Allowed callback URLs (also known as redirect URIs) to the URL of your app. This is where the Kinde client app is served. For local development this could be
http://localhost:3000
. This is required for your users to sign in to your app successfully. - Set the URLs they’ll be redirected to after signing out, by adding Allowed logout redirect URLs to your JavaScript applications logout page. For local development this could be
http://localhost:3000
. - Select Save.
Environments
Link to this sectionAs part of your development process, we highly recommend you create a development environment within your Kinde account. In this case, you’d use the Environment subdomain in the code block above.
Set up your app
Link to this sectionInstallation
Link to this sectionIntegrate with your app
Link to this sectionYou’ll need to create a new instance of the Kinde Auth client object.
We recommend using the async/await method. It must be the first thing that happens before you initialize your app.
- In Kinde, go to Settings > Applications > [your app] > View details.
- Replace the client_id and domain placeholders in the code block above with the the values from the App keys section.
Note: The redirect_uri
value you enter here needs to be the same as the redirect URI you entered in the Kinde application (see above).
Log in / register
Link to this sectionKinde provides login / register methods that are easy to implement. Here’s an example of adding buttons to your HTML:
You can bind events to buttons.
Clicking either of these buttons redirects your user to Kinde, where they authenticate before being redirected back to your site.
Test sign up
Link to this sectionRegister your first user by signing up yourself. You’ll see your newly registered user on the Users page of the relevant organization in Kinde.
Handle redirect
Link to this sectionOnce your user is redirected back to your site from Kinde, you can set a callback to take place. The callback automatically passes in the user object and any application state you set prior to the redirect.
Log out
Link to this sectionThis is implemented in much the same way as signing in or registering. The Kinde single page application client already comes with a sign out method.
Call your API
Link to this sectionThe getToken
method lets you to securely call your API and pass the bearer token to validate that your user is authenticated.
We recommend using our middleware on your back end to verify users and protect endpoints. Our current implementation is Node/Express, but we’re working on more.
Organizations
Link to this sectionFor general information about using organizations, see Kinde organizations for developers.
Create an organization
To create a new organization within your application, you will need to run a similar function below.
Sign up / sign in users to organizations
Kinde has a unique code for every organization. You’ll have to pass this code through when you register a new user. Example function below:
If you want a user to sign in to a particular organization, pass this code along with the sign in method.
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:
Get user information
Link to this sectionUse the getUser()
helper function to request the user information from Kinde.
Use the getUserProfile()
function to request the latest user information from the server.
User permissions
Link to this sectionWhen a user signs in to an organization the Access token your product/application receives contains a custom claim with an array of permissions for that user.
You can set permissions in your Kinde account. Here’s an example.
We provide helper functions to more easily access permissions:
A practical example in code might look something like:
Feature flags
Link to this sectionWhen a user signs in the Access token your product/application receives contains a custom claim called feature_flags
which is an object detailing the feature flags for that user.
You can set feature flags in your Kinde account. Here’s an example.
In order to minimize the payload in the token we have used single letter keys / values where possible. The single letters represent the following:
t
= type
v
= value
s
= string
b
= boolean
i
= integer
We provide helper functions to more easily access feature flags:
We also require wrapper functions by type which should leverage getFlag
above.
Booleans:
Strings and integers work in the same way as booleans above:
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.
The audience of a token is the intended recipient of the token.
To request multiple audiences, pass them separated by white space. See example.
For details on how to connect, see Register an API.
Overriding scope
Link to this sectionBy default the JavaScript SDK requests the following scopes:
profile
email
offline
openid
You can override this by passing scope
into the createKindeClient
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:
Persisting authentication state on page refresh or new tab
Link to this sectionYou will find that when you refresh the browser using a front-end based SDK that the authentication state is lost. This is because there is no secure way to persist this in the front-end.
There are two ways to work around this.
- (Recommended) use our Custom Domains feature which then allows us to set a secure, httpOnly first party cookie on your domain.
- (Non-production solution only) If you’re not yet ready to add your custom domain, or for local development, we offer an escape hatch you can provide to the Kinde Client
is_dangerously_use_local_storage
. This will use local storage to store the refresh token. DO NOT use this in production.
Once you implement one of the above, you don’t need to do anything else.
Persisting application state
Link to this sectionThe options argument passed into the login
and register
methods accepts an app_state
key where you can pass in the current application state prior to redirecting to Kinde. This is then returned to you in the second argument of the on_redirect_callback
as seen above.
A common use case is to allow redirects to the page the user was trying to access prior to authentication. This could be achieved as follows:
Login handler:
Redirect handler:
Token storage in the authentication state
Link to this sectionBy default the JWTs provided by Kinde are stored in memory. This protects you from both CSRF attacks (possible if stored as a client side cookie) and XSS attacks (possible if persisted in local storage).
The trade off with this approach however is that if a page is refreshed or a new tab is opened then the token is wiped from memory, and the sign in button would need to be clicked to re-authenticate. There are two ways to prevent this behaviour:
- Use the Kinde custom domain feature. We can then set a secure, httpOnly cookie against your domain containing only the refresh token which is not vulnerable to CSRF attacks.
- There is an escape hatch which can be used for local development:
is_dangerously_use_local_storage
. This absolutely should not be used in production and we highly recommend you use a custom domain. This will store only the refresh token in local storage and is used to silently re-authenticate.
SDK API Reference - createKindeClient
Link to this sectionaudience
Link to this sectionThe audience claim for the JWT.
Type: string
Required: No
client_id
Link to this sectionThe unique ID of your application in Kinde.
Type: string
Required: Yes
domain
Link to this sectionEither your Kinde instance URL, e.g https://yourapp.kinde.com
or your custom domain.
Type: string
Required: Yes
logout_uri
Link to this sectionWhere your user will be redirected when they sign out.
Type: string
Required: No
is_dangerously_use_local_storage
Link to this sectionAn escape hatch for storing the refresh token in local storage. Recommended for local development only, and not production.
Type: boolean
Required: No
Default: false
redirect_uri
Link to this sectionThe URL that the user will be returned to after authentication.
Type: string
Required: Yes
scope
Link to this sectionThe scopes to be requested from Kinde.
Type: string
Required: No
SDK API Reference - kindeClient methods
Link to this sectioncreateOrg
Link to this sectionConstructs redirect url and sends user to Kinde to sign up and create a new org for your business.
usage:
Sample output:
getClaim
Link to this sectionGets a claim from an access or ID token.
Arguments:
Usage:
Sample output:
getPermission
Link to this sectionReturns the state of a given permission.
Arguments:
Usage:
Sample output:
getPermissions
Link to this sectionReturns all permissions for the current user for the organization they are signed in to.
Usage:
Sample output:
getOrganization
Link to this sectionGet details for the organization your user is signed in to.
Usage:
Sample output:
getToken
Link to this sectionReturns the raw Access token from memory.
Usage:
Sample output:
getUser
Link to this sectionReturns the profile for the current user.
Usage:
Sample output:
getUserOrganizations
Link to this sectionGets an array of all organizations the user has access to.
Usage:
Sample output:
login
Link to this sectionConstructs redirect URL and sends user to Kinde sign in.
Arguments
Usage:
Example output:
logout
Link to this sectionLogs the user out of Kinde.
Argument:
Usage:
Example output:
register
Link to this sectionConstructs redirect url and sends user to Kinde to sign up.
Usage:
Sample output:
Reach out to support@kinde.com if you need help getting Kinde connected.