Express.js SDK
If 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
.
This topic assumes you are building with ExpressJS and have the relevant express
packages already installed.
You can view Kinde’s Express JS docs and Express JS starter kit in GitHub.
Set up your app
Link to this sectionAdd Kinde ExpressJS SDK as a dependency
Link to this sectionNode version 18.x.x or newer.
Integrate with your app
Link to this sectionYou’ll need to import our Kinde helpers into your project. We recommend you do this in the entry point of your application, typically index.js
or app.js
- In Kinde, go to Settings > Applications > [Your app] > View details.
- Replace the placeholders in the code block above with the the values from the App Keys section.
- Replace
http://localhost:3000
with the url of wherever your app is running.
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 and app key values in the code block above.
Set your callback and logout URLs
Link to this sectionKinde will redirect your user to authenticate. They’ll be redirected back to your Express 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 > [Your app] > View details.
- Set the Allowed callback URLs (redirect URIs) to the URL of your app. This is where your application is served. For local development this could be
http://localhost:3000
. This is required for your users to sign in to your app successfully. This should match theredirectUrl
you set in the config in the previous step. - 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
. This should match thesiteUrl
you set in the config in the previous step. - Select Save.
Note: 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.
Sign in / register
Link to this sectionKinde provides sign in / register methods that are easy to implement. Here’s an example of adding buttons to your HTML:
Clicking either of these buttons redirects your user to Kinde, where they authenticate before being redirected back to your site.
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.
Protecting routes
Link to this sectionThe protectRoute
middleware included in the SDK allows you to tell Express which routes are for authorized users only.
You can pass an unAuthorisedUrl
into the setupKinde
configuration object which will to tell Kinde where you would like the user to be redirected in the case of protectRoute
identifying the user is not yet authenticated.
Access the user object
Link to this sectionThe getUser
middleware included in the SDK allows you to add the user object to the request.
Sign out
Link to this sectionThis is implemented in much the same way as signing in or registering. The Kinde SDK already comes with a /logout
method.
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.
Verify JWT
Link to this sectionThis SDK also contains a library for verifying JWTs signed by Kinde. used as a layer of protect API endpoints. Initialize the library as follows:
Replace <your_kinde_subdomain>
above with the subdomain you registered on Kinde. The step above caches your JWKS for Kinde in your Express application.
Protect endpoints
Link to this sectionIf you add the verifier middleware to an endpoint it will validate the token. If valid the users Kinde id is added to the request, otherwise a 403 error is thrown.
Kinde Management API
Link to this sectionTo use the Kinde management API please see @kinde/management-api-js
SDK API Reference - setupKinde
Link to this sectionclientId
Link to this sectionThe unique ID of your application in Kinde.
Type: string
Required: Yes
issuerBaseUrl
Link to this sectionEither your Kinde URL or your custom domain. e.g https://yourapp.kinde.com
Type: string
Required: Yes
redirectUrl
Link to this sectionThe URL that the user will be returned to after authentication.
Type: string
Required: Yes
secret
Link to this sectionThe unique Client secret of your application in Kinde
Type: string
Required: Yes
siteUrl
Link to this sectionWhere your user will be redirected when they sign out.
Type: string
Required: Yes
unAuthorisedUrl
Link to this sectionThe URL the user will be redirected to if protectRoute
finds the user is not authenticated.
Type: string
Required: No
Reach out to support@kinde.com if you need help getting Kinde connected.