Get a Kinde Management Token using Ruby on Rails

At the time of writing the Kinde Ruby SDK doesn't quite work (it is auto generated) when trying to get a management token for use with the Kinde Management API.

A simple way to do this is to use an HTTP library of your choosing - I like the HTTP gem

Assuming you have setup the secrets using the credentials files:

EDITOR="code --wait" bin/rails credentials:edit --environment=development

With the secrets:

kinde: client_id: "CLIENT_ID" client_secret: "CLIENT_SECRET" domain: "https://YOUR_DOMAIN"

Create a new service and use the code below:

module Kinde module Services class GetManagementToken def perform raw_token = get_token return { success: false, errors: ["Unable to get token"] } if raw_token.blank? parsed_token = parse_token(raw_token) return { success: false, errors: ["Unable to parse token"] } if parsed_token.blank? return { success: true, data: parsed_token } end private def get_token HTTP.post("#{domain_url}/oauth2/token", form: { audience: "https://api.kinde-auth.app", client_id: Rails.application.credentials.dig(:kinde, :client_id), client_secret: Rails.application.credentials.dig(:kinde, :client_secret), grant_type: "client_credentials", }, ).to_s end def parse_token(raw_token) begin JSON.parse(raw_token, symbolize_names: true) rescue => exception nil end end def domain_url Rails.application.credentials.dig(:kinde, :domain) end end end end

Then just call the service:

Kinde::Services::GetManagementToken.new.perform
View More Posts

Portfolio built using the open source project by

Initium Studio

Create your own Next.js Portfolio with Contentful CMS by forking this repo onGitHub

Blog

|

Projects

|

Health

|

Recipes

|

About

|

Contact

2024 Greeff Consulting Pty Ltd