Using Kinde without an SDK
SDKs and APIs
The Kinde Java SDK allows developers to connect their Java app to Kinde.
You can also find our Java docs and Java starter kit in GitHub.
The SDK is officially only supported for version 8 or later of Java.
If you haven’t already created a Kinde account, register for free here (no credit card required). Registration gives you a Kinde domain, which you will need to get started. e.g. yourapp.kinde.com
.
Create a JAR file of your SDK project using the below command:
To deploy it to a remote Maven repository instead, configure the settings of the repository and execute:
Refer to the OSSRH Guide for more information.
Kinde’s SDK is available through Maven. To install it, simply add the following line to your pom.xml
Add this dependency to your project’s build file:
Generate the JAR by executing the following code:
Then manually install the following JARs:
Create KindeClientSDK object to use the SDK methods.
Add @ComponentScan
annotation in your main application. It should include the packages where your controllers from both the main application and the dependency project are located.
For your app to work with Kinde, you need to set callback and logout redirect URLs.
http://localhost:8080/api/auth/kinde_callback
http://localhost:8080
Tip: Make sure there are no hidden spaces in URLs and remove the ‘/’ backslash at the end.
If you would like to use different Environments as part of your development process, you will need to add them within your Kinde business first. You will also need to add the Environment subdomain to the code block above.
Add following properties in the application.properties
file:
The following variables need to be replaced in the code snippets below.
https://your_kinde_domain.kinde.com
http://localhost:8080/api/auth/kinde_callback
http://localhost:8080
KindeClientSDK
struct implements three OAuth flows: client_credentials
, authorization_code
, authorization_code_flow_pkce
Kinde supports an easy to implement login / register flow.
To redirect users to the /login
and /register
pages on the client side when a link is clicked, you can use HTML anchor tags. Here’s an example:
After the user is redirected to the /login
or /register
page, your server will need to handle the authentication process. The SDK provides login
and register
methods that you can use to handle these requests on the server side. Use these methods in your server-side code:
When the user is redirected back to your site from Kinde, this will call your callback URL defined in the kinde.redirect.url
variable. You will need to route /api/auth/kinde_callback
to call a function to handle this.
The Kinde SPA client comes with a logout method.
Register your first user by signing up yourself. You’ll see your newly registered user on the Users page in Kinde.
To access the user information, use the getUserDetails
helper function:
After a user signs in and they are verified, the token return includes permissions for that user. User permissions are set in Kinde, but you must also configure your application to unlock these functions.
Example of a permissions array.
Helper functions for permission access.
Example in your code.
Once the user has successfully authenticated, you’ll have a JWT and a refresh token and that has been stored securely. Use the getToken
method to get the token:
Use the getAccessToken
method of the Storage class to get an access token.
The token will be stored in the cookie. To specify the expiration time, use the setTokenTimeToLive
method.
Get the current authentication status with isAuthenticated
.
An 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.
To create a new organization within your application, run a similar function to below:
You can also pass org_name as your organization
When a user signs up or in to an organization, the org_code
needs to be passed with the request. The org_code
refers to the one created automatically in Kinde when the organization was created, for example org_0e9f496742ae
.
Here’s an helper function for registering in below:
If you want a user to sign in to a particular organization, pass this code along with the sign in method.
Because a user can belong to multiple organizations, and because they may have different permissions for the different organizations, with pass both the org_code
and permissions
in the token. Here’s an example:
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:
By default, Kinde requests the following scopes:
You can also pass override scopes to the Kinde SDK as per this example:
Kinde provides a helper to grab any claim from your ID or access tokens. The helper defaults to access tokens:
When a user signs in, the access token your product or application receives contains a custom claim called feature_flags
. Feature flags define what features a user can access after they sign in.
You can set feature flags in Kinde through code (see below), or through the Kinde application.
To minimize the token payload we use single letter keys / values to represent the following:
t = type
v = value
s = string
b = boolean
i = integer
We also provide helper functions to more easily access feature flags:
A practical example in code would look something like:
We also require wrapper functions to be separated by type as part of the getFlag
function.
Boolean wrapper:
String wrapper:
Integer wrapper:
Example of wrapper function:
This applies to frontend SDKs only.
By 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 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 behavior:
isDangerouslyUseLocalStorage
. This SHOULD NOT be used in production. This will store only the refresh token in local storage to silently re-authenticate.Storing Tokens
The setToken
method is used to store the token in a secure manner. It encodes the token (which can be a string or a map) and sets it as a cookie in the HTTP response. The token is stored with specific attributes such as MaxAge
, Path
, Domain
, Secure
, and HttpOnly
to enhance security.
The setItem
method is a utility function for setting a cookie with the provided attributes.
Retrieving Tokens
The getToken
method is used to retrieve the token from the HTTP request. It decodes and reads the stored token from the cookie.
The getItem
method is a utility function for retrieving a specific cookie from the HTTP request.
Either your Kinde instance URL or your custom domain. e.g. https://yourapp.kinde.com/
Type: string
Required: yes
redirectUri
The URL that the user will be returned to after authentication.
Type: string
Required: yes
clientId
The unique ID of your application. Get this from the Application details section in Kinde.
Type: string
Required: yes
clientSecret
The unique client secret of your Kinde application. Get this from the Application details section in Kinde.
Type: string
Required: Not required if you use OAuth PKCE
grantType
The grantType for Kinde Authorization varies for each OAuth 2 flow. You can use:
code_challenge
and code_challenge_method
parameters are also required for this grant type.Type: string
Required: yes
logoutRedirectUri
Where your user will be redirected when they sign out.
Type: string
Required: yes
scope
The scopes to be requested from Kinde.
Type: string
Required: No
Default: openid profile email offline
additionalParameters
Additional parameters that will be passed in the authorization request.
Type: Map<String, Object>
Required: No
Default: new Hashmap<>()
additionalParameters - audience
The audience claim for the JWT.
Type: string
Required: No
login
Constructs redirect URL and sends user to Kinde to sign in.
Arguments:
Usage:
register
Constructs redirect url and sends user to Kinde to sign up.
Arguments:
Usage:
logout
Logs the user out of Kinde.
Arguments:
Usage:
getToken
Returns the raw access token from URL after logged from Kinde.
Arguments:
Usage:
Sample output:
createOrg
Constructs redirect url and sends user to Kinde to sign up and create a new org for your business.
Arguments:
Usage:
Sample output:
getClaim
Gets a claim from an access or ID token.
Arguments:
Usage:
Sample output:
getPermission
Returns the state of a given permission.
Arguments:
Usage:
Sample output:
getPermissions
Returns all permissions for the current user for the organization they are logged into.
Arguments:
Usage:
Sample output:
getOrganization
Get details for the organization your user is logged into.
Arguments:
Usage:
Sample output:
getUserDetails
Returns the profile for the current user.
Arguments:
Usage:
Sample output:
getUserOrganizations
Gets an array of all organizations the user has access to.
Arguments:
Usage:
Sample output:
getFlag
Gets a feature flag from an access token.
Arguments:
Usage:
Sample output:
getBooleanFlag
Gets a boolean feature flag from an access token.
Arguments:
Usage:
Sample output:
getStringFlag
Gets a string feature flag from an access token.
Arguments:
Usage:
Sample output:
getIntegerFlag
Gets a integer feature flag from an access token.
Arguments:
Usage:
Sample output:
isAuthenticated
To check user authenticated or not.
Arguments:
Usage:
Sample output:
If you need help connecting to Kinde, please contact us at support@kinde.com.