SvelteKit SDK
Kinde SvelteKit SDK allows developers to integrate Kinde Authentication into their SvelteKit projects. This SDK implements the following OAuth 2.0 flows.
- Authorization Code - Intended for confidential clients for e.g. web-servers
- Authorization Code with PKCE extension - For public clients for e.g. single page web application and or mobile applications, and confidential clients for e.g. web-servers.
- Client Credentials Flow - Intended for confidential clients, where machine to machine communication is required.
For new project, you can find our Svelte Starter Kit
Requirements
Link to this section- Node version 18.16.x 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. yourapp.kinde.com
.
Install
Link to this sectionConfigure Kinde
Link to this sectionSet callback URLs
Link to this section-
In Kinde, go to Settings > Applications and then navigate to the relevant frontend app or backend app.
-
Add your callback URLs in the relevant fields. For example:
-
Allowed callback URLs (also known as redirect URIs):
https://<your_app_domain>/api/auth/kinde_callback
e.g:
http://localhost:5173/api/auth/kinde_callback
-
Allowed logout redirect URLs:
https://<your_app_domain>
e.g:
http://localhost:5173
-
-
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.
Configure your app
Link to this sectionEnvironment variables
Put these variables in your .env
file. You can find these variables on your Settings > Applications > [Your app] > View details page.
KINDE_ISSUER_URL
- your Kinde domainKINDE_CLIENT_ID
- your Kinde Client IDKINDE_CLIENT_SECRET
- your Kinde Client secret. Leave this field empty if you are using Authentication Code Flow with PKCEKINDE_REDIRECT_URL
- this is the URL Kinde will redirect to after the authentication process. Note that the Kindeapi/auth/kinde_callback
path must be included in the URL (see example below). You also need to make sure this URL is included in your list of Allowed callback URLs.KINDE_POST_LOGOUT_REDIRECT_URL
- Specify the destination where you want users to be redirected to after logging out. Make sure this URL is listed under your Allowed logout redirect URLs.KINDE_POST_LOGIN_REDIRECT_URL
- the URL users will be redirected to after signing in.KINDE_AUTH_WITH_PKCE
- Settrue
if you want to use Authentication Code Flow with PKCE
Below is an example of a .env
file
Integrate with your app
Link to this sectionImplement sessionHooks
in your hooks.
Then implement the Kinde routes in your app by creating a server file.
This will handle Kinde Auth endpoints in your SvelteKit app.
/api/auth/login
- will redirect you to login at the KindeAuth server./api/auth/logout
- will log you out of the app./api/auth/register
- will redirect you to register at the KindeAuth server./api/auth/kinde_callback
- the endpoint to handle the redirect after logging in from Kinde/api/auth/health
- See Health Below
Sign in and sign up (login and register)
Link to this sectionThe SDK ships with predefined API routes to generate the auth urls for sign up and sign in.
Direct users post login / registration
Link to this sectionin addition to KINDE_POST_LOGIN_REDIRECT_URL
which will direct your users to a single place, you direct the users more granularly.
Both login and register support passing a url param post_login_redirect_url
which will direct to the user once the flow is completed.
Log out
Link to this sectionThis is implemented in much the same way as signing up or signing in. An API route is provided for you
Check if the user is authenticated
Link to this sectionWe’ve provided a helper to get a boolean value that checks if a user is signed in by verifying that the access token is valid.
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 getUser
helper function:
Health check
Link to this sectionTo check your configuration, the SDK exposes an endpoint with your settings.
/api/auth/health
Note: The client secret will indicate only if the secret is set or not set correctly.
To enable set the KINDE_DEBUG
environment variable to true
View users in Kinde
Link to this sectionIf you navigate to the “Users” page within Kinde you will see your newly registered user there.
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 set against KINDE_AUDIENCE
in your environment variables.
The audience of a token is the intended recipient of the token.
For details on how to connect, see Register an API
Overriding scope
Link to this sectionBy default the KindeSDK
requests the following scopes:
- profile
- offline
- openid
To override this, you can set the value against the KINDE_SCOPE
in your environment variables.
Organizations
Link to this sectionCreate an organization
Link to this sectionTo create a new organization within your application, set up the following route:
Sign up and sign in to organizations
Link to this sectionThe Kinde client provides methods for you easily sign up and sign in users into organizations. You can add links in your HTML as follows:
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 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:
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 grab any feature flag from 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 using the getToken
method.
Kinde Management API
Link to this sectionTo use our management API please see @kinde/management-api-js
SDK API reference
Link to this sectionlogin
Link to this sectionConstructs redirect url and sends user to Kinde to sign in.
Usage:
register
Link to this sectionConstructs redirect url and sends user to Kinde to sign up.
Usage:
logout
Link to this sectionLogs the user out of Kinde.
Usage:
isAuthenticated
Link to this sectionChecks if the user is authenticated.
Usage:
Sample output:
createOrg
Link to this sectionConstructs redirect url and sends the user to Kinde to sign up and creates a new org for your business.
Arguments:
Usage:
getClaim
Link to this sectionExtracts the provided claim from the provided token type in the current session, the returned object includes the provided claim.
Arguments:
Usage:
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 logged into.
Usage:
Sample output:
getOrganization
Link to this sectionGet details for the organization your user is logged into.
Usage:
Sample output:
getUserOrganizations
Link to this sectionGets an array of all organizations the user has access to.
Usage:
Sample output:
getUser
Link to this sectionExtracts the user details from the ID token obtained after authentication.
Usage:
Sample output:
getToken
Link to this sectionReturns the access token obtained after authentication.
Usage:
Sample output:
getUserProfile
Link to this sectionMakes use of the getToken
method above to fetch user details.
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:
getStringFlag
Link to this sectionGet a string flag from the feature_flags claim of the access_token
Arguments:
Usage:
Sample output:
getIntegerFlag
Link to this sectionGet an integer flag from the feature_flags claim of the access_token
Arguments:
Usage:
Sample output:
If you need help connecting to Kinde, contact us at support@kinde.com.