Elixir SDK
The Kinde Elixir SDK allows developers to connect their Elixir app to Kinde. This document is relevant for up to Elixir v1.2.0.
You can also view the Elixir docs and Elixir starter kit in GitHub.
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 sectionInstall erlang and elixir. Update the deps, update path to the SDK in mix.exs
.
Add to your extra applications in mix.exs
.
Set callback URLs
Link to this section- In Kinde, go to Settings > Applications > [Your app] > View details.
- Add your callback URLs in the relevant fields. For example:
- Allowed callback URLs (also known as redirect URIs) - for example,
http://localhost:4000/callback
- Allowed logout redirect URLs - for example,
http://localhost:4000
- Allowed callback URLs (also known as redirect URIs) - for example,
- Select Save.
Note: The http://localhost:4000
is an example of a commonly used local development URL. It should be replaced with the URL where your app is running.
Add environments
Link to this sectionKinde comes with a production environment, but you can set up other environments if you want to. Note that each environment needs to be set up independently, so you need to use the Environment subdomain in the code block above for those new environments.
Configure your app
Link to this sectionAPI Keys
Follow these steps to set up the project:
- Create a .env file at the root directory.
- Add the following, but change the
“values”
to match your own information.
- If required, set the scopes. You can include scopes such as
openid
profile
offline
, in addition toemail
.
- Open the console and write
source .env
before any mix command.
Environment variables
You can also set these variables in .env file within your project directory. The following variables need to be replaced in the code snippets.
KINDE_HOST
- your Kinde domain - e.g.https://yourkindedomain.kinde.com
KINDE_REDIRECT_URL
- your callback url, make sure this URL is under your allowed callback redirect URLs. - e.g.http://localhost:4000/callback
KINDE_POST_LOGOUT_REDIRECT_URL
- where you want users to be redirected to after logging out, make sure this URL is under your allowed logout redirect URLs. - e.g.http://localhost:4000
KINDE_CLIENT_ID
- you can find this on the Application details pageKINDE_CLIENT_SECRET
- you can find this on the Application details page
Usage
Link to this sectionExecute following in your terminal to run:
OAuth Flows (Grant Types)
Link to this sectionThe KindeClientSDK struct implements three OAuth flows:
- Client Credentials flow
- Authorization Code flow
- Authorization Code with PKCE flow
Each flow can be used with their corresponding grant type when initializing a client.
OAuth Flow | Grant Type | Type |
---|---|---|
Client Credentials | :client_credentials | atom |
Authorization Code | :authorization_code | atom |
Authorization Code with PKCE | :authorization_code_flow_pkce | atom |
Integrate with your app
Link to this sectionCreate a new instance of the Kinde Auth client object before you initialize your app:
Log in and registration
Link to this sectionThe Kinde client provides methods for easy log in and registration.
You can add buttons in your HTML as follows:
You will also need to route /login
and /register
to the SDK methods:
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 (or default organization) in Kinde.
Manage redirects
Link to this sectionWhen 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 /callback
to call a function to handle this.
Tokens
Link to this sectionWe use the Kinde helper function to get the tokens generated by login
and get_token
.
Or first calling the get_token
function:
Example of a returned token:
User details
Link to this sectionThis function returns the user object including Kinde ID. This function reads the information from the id_token
that is returned after successful authentication. Include the required scopes if not added already (openid profile email offline
).
Note: You need to have already authenticated before you call the API, otherwise an error will occur.
View users in Kinde
Link to this sectionGo to the Users page in Kinde to see who has registered.
Organizations
Link to this sectionCreate an organization
There is an additional create_org
method which allows an organization to be created. This method calls the current sign-up logic by setting the is_create_org
parameter to true.
Use this helper function to create an organization.
Sign up and sign in to organizations
Kinde has a unique code for every organization. You’ll have to pass this code when creating a client or registering a new user: additional_parameters_org_code
.
If you want a user to sign in to a particular organization, pass this code along with the sign in method.
For more information about how organizations work in Kinde, see Kinde organizations for developers.
Logout
Link to this sectionThe Kinde SDK client comes with a logout method.
Authenticated
Link to this sectionReturns whether if a user is signed in by verifying that the access token is still valid.
Claims
Link to this sectionWe have provided a helper to grab any claim from your ID or access tokens, which accepts a key for a token and returns the claim value. There is also an optional argument to define which token to check. The helper defaults to access tokens.
This function will returns claims as follows.
For example, when a key is accepted for a token as KindeClientSDK.get_claim(conn, "jti", :id_token)
the return claim value would be as follows.
Permissions
Link to this sectionWhen a user signs in to an organization the access token your product/application contains a custom claim with an array of permissions for that user.
You can set permissions in your Kinde account. Here’s an example.
For more details See Define user permissions.
We provide helper functions to more easily access permissions.
get_permission
checks the permission value and returns if it is granted or not (i.e. checks if permission key exists in the permissions
claim array) and checks the relevant org code by checking against claim org_code
.
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.
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
You can override this by passing scope into the KindeSDK.
Persisting authentication state on page refresh or new tab
Link to this sectionWhen a user refreshes the page or opens a new tab, the authentication state can be lost. To work around this issue, there are two possible solutions:
- Use cookies to store the authentication token. This can be done by setting an
httpOnly
cookie with the authentication token, which will be sent to the server with every request, allowing the server to maintain the authentication state. - Use a session store to store the authentication token. Elixir has several session store options available, including using a database, in-memory cache, or distributed cache.
Once one of these solutions is implemented, there is no need for additional action to persist the authentication state.
Token storage
Link to this sectionOnce the user has successfully authenticated, you’ll have a JWT and possibly a refresh token that should be stored securely.
API reference - Create Kinde Client
Link to this sectiondomain
Link to this sectionEither your Kinde URL or your custom domain. e.g https://yourapp.kinde.com
Type: string
Required: Yes
redirect_url
Link to this sectionThe URL that the user will be returned to after authentication.
Type: string
Required: Yes
backend_client_id
Link to this sectionThe unique ID of your backend application in Kinde.
Type: string
Required: Yes
frontend_client_id
Link to this sectionThe unique ID of your frontend application in Kinde.
Type: string
Required: Yes
client_secret
Link to this sectionThe unique client secret associated with your application in Kinde.
Type: string
Required: No
logout_redirect_url
Link to this sectionWhere your user will be redirected upon logout.
Type: string
Required: No, except for PKCE flow
scope
Link to this sectionThe scopes to be requested from Kinde.
Type: string
Required: No
Default: openid profile email offline
additional_parameters
Link to this sectionAdditional parameters that will be passed in the authorization request.
Type: map
Required: No
Default: %{}
additional_parameters_audience
Link to this sectionThe audience claim for the JWT.
Type: string
Required: No
additional_parameters_org_name
Link to this sectionThe org claim for the JWT.
Type: string
Required: No
additional_parameters_org_code
Link to this sectionThe org claim for the JWT.
Type: string
Required: No
API reference - Kinde Client Functions
Link to this sectionlogin
Link to this sectionConstructs a redirect URL and sends the user to Kinde to sign in.
Arguments: conn, client
Usage:
Sample output: redirect
register
Link to this sectionConstructs a redirect URL and sends the user to Kinde to sign up.
Arguments: conn, client
Usage:
Sample: redirect
logout
Link to this sectionLogs the user out of Kinde.
Arguments: conn
Usage:
Sample: redirect
get_token
Link to this sectionReturns the raw access token from URL after logged in from Kinde.
Arguments: conn
Usage:
Sample:
create_org
Link to this sectionConstructs the redirect URL to sign up and create a new organization in your business.
Arguments: conn
, atom
Usage:
Sample: redirect
get_claim
Link to this sectionGets a claim from an access or ID token.
Arguments: conn, string, atom
Usage:
Sample:
get_claims
Link to this sectionGets all claims from an access or ID token.
Arguments: conn, atom
Usage:
Sample:
get_permission
Link to this sectionReturns the state of a given permission.
Arguments: conn, string
Usage:
Sample:
get_permissions
Link to this sectionReturns all permissions for the current user for the organization they are signed into.
Arguments: conn, atom
Usage:
Sample:
get_organization
Link to this sectionGet details for the organization your user is signed into.
Arguments: conn
Usage:
Sample:
get_organizations
Link to this sectionGets an array of all organizations the user has access to.
Arguments: conn
Usage:
Sample:
get_user_details
Link to this sectionReturns the profile for the current user.
Arguments: conn
Usage:
Sample:
get_cache_pid
Link to this sectionReturns the Kinde cache PID from the conn
.
Arguments: conn
Usage:
Sample:
save_kinde_client
Link to this sectionSaves the Kinde client created into the conn
.
Arguments: conn
Usage:
Sample:
get_kinde_client
Link to this sectionReturns the Kinde client created from the conn
.
Arguments: conn
Usage:
Sample:
get_all_data
Link to this sectionReturns all the Kinde data (tokens) returned.
Arguments: conn
Usage:
Sample:
Feature flag helper functions
Link to this sectionget_flag/2
Link to this sectionDetails of a feature-flag.
Arguments: conn, code
Usage:
Sample output:
get_flag/3
Link to this sectionThe default value of a feature flag.
Arguments: conn, code
, default value
Usage:
Sample output:
get_flag/4
Link to this sectionThe type and default value of a feature flag.
Arguments: conn, code, default value, flag_type
Usage:
Sample output:
get_boolean_flag/2
Link to this sectionReturns the boolean flag.
Arguments: conn, code
Usage:
Sample output:
get_boolean_flag/3
Link to this sectionReturns the boolean flag value.
Arguments: conn, code, default value
Usage:
Sample output:
get_string_flag/2
Link to this sectionReturns the string flag value.
Arguments: conn, code
Usage:
Sample output: corresponding values from object or error-messages
get_string_flag/3
Link to this sectionReturns the string flag value.
Arguments: conn, code, default value
Usage:
Sample output:
get_integer_flag/2
Link to this sectionReturns the integer flag value.
Arguments: conn, code
Usage:
Sample output: corresponding values from object or error-messages
get_integer_flag/3
Link to this sectionReturns the integer flag value.
Arguments: conn, code, default value
Usage:
Sample output:
If you need help connecting to Kinde, please contact us at support@kinde.com.