Using Kinde without an SDK
SDKs and APIs
The Kinde PHP SDK allows developers to integrate with Composable Commerce APIs using PHP native interfaces, models and helpers instead of manually using the HTTP and JSON API.
You can also view the PHP docs and PHP starter kit in GitHub.
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
Install Composer and then execute the following command:
Or add the following to your composer.json file:
http://localhost:8000/callback
http://localhost:8000
Kinde 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.
Environment variables
The following variables need to be replaced in the code snippets below.
KINDE_HOST
- your Kinde domain - e.g. https://your_kinde_domain.kinde.com
KINDE_REDIRECT_URL
- your callback url, make sure this URL is under your allowed callback redirect URLs. - e.g. http://localhost:8000/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:8000
KINDE_CLIENT_ID
- you can find this on the Application details pageKINDE_CLIENT_SECRET
- you can find this on the Application details pageAdd the composer autoloader to your app.
Create a new instance of the Kinde Auth client object before you initialize your app.
The Kinde client provides methods for easy login and registration.
You can add buttons in your HTML as follows:
You will also need to route /login
and /register
to the SDK methods:
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 /callback
to call a function to handle this.
You can also get the current authentication status with isAuthenticated
For more information, please check out Kinde\KindeSDK\Sdk\Enums\AuthStatus
By default, your cookie will apply to the full domain and sub folder from which the code is called from. You can override both the path and domain using the following helper functions.
The Kinde SPA client comes with a logout method.
To access the user information, use the getUserDetails
helper function:
Go to the Users page in Kinde to see who has registered.
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.
We provide helper functions to more easily access permissions:
A practical example in code might look something like:
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.
The audience of a token is the intended recipient of the token.
For details on how to connect, see Register an API
By default the KindeSDK
requests the following scopes:
You can override this by passing scope into the KindeSDK
We have provided a helper to grab any claim from your id or access tokens. The helper defaults to access tokens:
To create a new organization within your application, run a similar function to below:
You can also pass org_name
as your organization
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:
For more information about how organizations work in Kinde, see Kinde organizations for developers.
We have provided a helper to grab any feature flag from access_token
:
We also provide wrapper functions which should leverage getFlag
above:
Once the user has successfully authenticated, you’ll have a JWT stored in a local cookie containing an access token, refresh token and ID token if requested. You can use the getAccessToken
method of the Storage
class to retrieve the access token from the cookie. There are also helper functions to get the decoded access token or id token.
To specify the expiration time, you can use the setTokenTimeToLive
method.
By default, your token cookie will be local to the subdomain that made the login request (e.g. login.yourdomain.com), so app.yourdomain.com will have no visibility of this.
You can confirm this in your browser, for example in Chrome go to Developer tools and under Application > Storage > Cookies, look at the Domain column.
To achieve cross sub-domain use the cookie needs to be set to the root domain rather than the subdomain. You can do this by adding the following code after initializing the KindeClient:
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: yes
grantType
The grantType
for Kinde Authorization varies for each OAuth 2 flow. You can use:
GrantType::authorizationCode:
Intended for confidential clients, e.g. web-servers.GrantType::PKCE:
Extension for public clients, e.g. single page web applications and mobile applications, and confidential clients, e.g. web-servers. Note that the code_challenge
and code_challenge_method
parameters are also required for this grant type.GrantType::clientCredentials:
Intended for confidential clients where machine-to-machine communication is required.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:
additionalParameters
Additional parameters that will be passed in the authorization request.
Type: array
Required: No
Default: [ ]
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.
Usage:
getToken
Returns the raw access token from URL after logged from Kinde.
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.
Usage:
Sample output:
getOrganization
Get details for the organization your user is logged into.
Usage:
Sample output:
getUserDetails
Returns the profile for the current user.
Usage:
Sample output:
getUserOrganizations
Gets an array of all organizations the user has access to.
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.
Usage:
Sample output:
If you need help connecting to Kinde, please contact us at support@kinde.com.