Welcome to the Kinde community

Updated 10 months ago

Great to hear @GonzaP!

Great to hear @GonzaP!
The latest version of the ExpressJS SDK is actually v1.2.3. Please use this latest version and let me know if you have any issues.
G
O
v
11 comments
Hi @Oli - Kinde I upgraded the SDK version to the 1.2.3, but typescript still can't find the types.
Plain Text
Could not find a declaration file for module '@kinde-oss/kinde-node-express'. 'd:/codeagain/isell-cargas-backend/node_modules/@kinde-oss/kinde-node-express/dist-cjs/index.js' implicitly has an 'any' type.
. That's the same as when I used the 1.0.12 version, but now my app even crashes, specifically when using the JWT verifier. This is how I implemented it
Plain Text
import { jwtVerify } from "@kinde-oss/kinde-node-express";

export const kindeConfig = {
  clientId: process.env.KINDE_CLIENT_ID,
  issuerBaseUrl: process.env.KINDE_DOMAIN,
  secret: process.env.KINDE_CLIENT_SECRET,
  siteUrl: "http://localhost:3000",
  redirectUrl: process.env.KINDE_REDIRECT_URL,
};
export const verificadorKindeJWT = jwtVerify(process.env.KINDE_DOMAIN);
and this is how I setup Kinde, as taken from the docs,
Plain Text
import { setupKinde } from "@kinde-oss/kinde-node-express"
import { kindeConfig } from "./configs/autenticacion/kinde";

const app = express();
setupKinde(kindeConfig, app);
And the error I'm getting in the terminal is
Plain Text
D:\codeagain\isell-cargas-backend\node_modules\@kinde-oss\kinde-node-express\dist-cjs\helpers\kindeMiddlewareHelpers.js:72
[1]     const { audience } = options;
[1]             ^
[1]
[1] TypeError: Cannot destructure property 'audience' of 'options' as it is undefined.
[1]     at jwtVerify (D:\codeagain\isell-cargas-backend\node_modules\@kinde-oss\kinde-node-express\dist-cjs\helpers\kindeMiddlewareHelpers.js:72:13)
[1]     at Object.<anonymous> (D:\codeagain\isell-cargas-backend\build\configs\autenticacion\kinde.js:21:66)
[1]     at Module._compile (node:internal/modules/cjs/loader:1376:14)
[1]     at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
[1]     at Module.load (node:internal/modules/cjs/loader:1207:32)
[1]     at Module._load (node:internal/modules/cjs/loader:1023:12)
[1]     at Module.require (node:internal/modules/cjs/loader:1235:19)
[1]     at require (node:internal/modules/helpers:176:18)
[1]     at Object.<anonymous> (D:\codeagain\isell-cargas-backend\build\rutas\rutaRazonSocial.js:33:17)
[1]     at Module._compile (node:internal/modules/cjs/loader:1376:14)
Maybe it's a small thing I'm doing wrong but I can't seem to find it. I'll go back to 1.0.12 meanwhile and try again later
Hey @GonzaP,
Sorry you are still experiencing issues.
I will have to speak to my ExpressJS expert teammates on Monday to figure out your issue.
hey @GonzaP , sorry to hear you're having trouble with the Express SDK - have you got a file in @kinde-oss/kinde-node-express/dist-cjs/setup/kindeClient.js with
Plain Text
const grantTypeConfigParams = {
    [kinde_typescript_sdk_1.GrantType.AUTHORIZATION_CODE]: ['secret', 'redirectUrl', 'unAuthorisedUrl'],
    [kinde_typescript_sdk_1.GrantType.PKCE]: ['redirectUrl', 'unAuthorisedUrl'],
    [kinde_typescript_sdk_1.GrantType.CLIENT_CREDENTIALS]: ['secret'],
};
- I believe the types should be coming from there.
For your other implementation, would you be able to give this a go:
  • import setupKinde directly from the @kinde-oss/kinde-node-express module
  • added grantType + a few other properties (if required) to config
  • setting up audience + providing it to const verificadorKindeJWT = jwtVerify(process.env.KINDE_DOMAIN, options);
Something like:

Plain Text
const { jwtVerify } = require("@kinde-oss/kinde-node-express");
require("dotenv").config();

const express = require("express");
const {
  setupKinde,
  protectRoute,
  getUser,
} = require("@kinde-oss/kinde-node-express");

const app = express();
const port = 3000;
app.use(express.static("public"));
const config = {
  clientId: process.env.KINDE_CLIENT_ID,
  issuerBaseUrl: process.env.KINDE_ISSUER_URL,
  siteUrl: process.env.KINDE_SITE_URL,
  secret: process.env.KINDE_CLIENT_SECRET,
  postLogoutRedirectUrl: process.env.KINDE_POST_LOGOUT_REDIRECT_URL,
  redirectUrl:  process.env.KINDE_POST_LOGOUT_REDIRECT_URL,
  unAuthorisedUrl: process.env.KINDE_ISSUER_URL || '/unauthorized',
  grantType: 'AUTHORIZATION_CODE',
};

const options = {
  audience:  process.env.KINDE_ISSUER_URL || '/api'
}

const verificadorKindeJWT = jwtVerify(process.env.KINDE_DOMAIN, options);
module.exports = { verificadorKindeJWT };

app.set("view engine", "pug");
setupKinde(config, app);

app.get("/", (req, res) => {
  if (req.session && req.session.kindeAccessToken) {
    res.redirect("/admin");
  } else {
    res.render("index", {
      title: "Hey",
      message: "Hello there! what would you like to do?",
    });
  }
});

app.get("/admin", protectRoute, getUser, (req, res) => {
  res.render("admin", {
    title: "Admin",
    user: req.user,
  });
});

app.listen(port, function () {
  console.log(`Kinde Express Starter Kit listening on port ${port}!`);
});


thanks!
Hi @viv (kinde) I do have that file inside @kinde-oss/kinde-node-express/dist-cjs/setup/kindeClient.js with the specified content. It still wouldn't work. I found that the types are only recognized when setting my tsconfig.json "moduleResolution" to "Node" or "Node10", while I was using "NodeNext", but changing that basically breaks my entire app saying it can't find any import I make.
Another thing I did was leaving my tsconfig as it was before, and going to the package.json at @kinde-oss/kinde-node-express. I see that under the "exports" key you have
Plain Text
 "exports": {
    ".": {
      "require": {
        "types": "./dist-cjs/types/index.d.ts",
        "default": "./dist-cjs/index.js"
      },
      "import": {
        "types": "./dist/types/index.d.ts",
        "default": "./dist/index.js"
      }
    }
  },
And although I don't really know how npm packages and publishing work, I changed the "types" key inside "require" to "./dist/types/index.d.ts" instead of "./dist-cjs/types/index.d.ts" as the types folder I see is inside dist instead of dist-cjs , and then the types where recognized in my app, typing everything as expected. As I said I don't know how packages typings work but that seems to solve my problem for now at least, I'll just tell my coworkers to do the same change for now
And well I tried with the setup you specified but it redirects me to the unAuthorisedUrl after logging in or registering, instead of my own redirectUrl, and sometimes redirected me to siteUrl as well. I'm surely setting something wrong but I'll try again later or when the express sdk docs get an update for the last version
Hey @GonzaP,
I will get Viv to look into this tomorrow.
Hey @GonzaP , sorry to see you're still experiencing issues with the sdk + thanks for letting us know that's what resolved your initial err. Would you be able to try the below in your app.js / index.js and seeing if this resolves the errors you're currently experiencing?

Plain Text
require("dotenv").config();
const { jwtVerify } = require("@kinde-oss/kinde-node-express");
const express = require("express");

const {
  setupKinde,
  protectRoute,
  getUser,
  GrantType,
} = require("@kinde-oss/kinde-node-express");
const app = express();
const port = 3000;
app.use(express.static("public"));
const config = {
  grantType: GrantType.AUTHORIZATION_CODE,
  clientId: process.env.KINDE_CLIENT_ID,
  issuerBaseUrl: process.env.KINDE_ISSUER_URL,
  siteUrl: process.env.KINDE_SITE_URL,
  secret: process.env.KINDE_CLIENT_SECRET,
  redirectUrl: process.env.KINDE_REDIRECT_URL,
  unAuthorisedUrl: process.env.KINDE_SITE_URL,
  postLogoutRedirectUrl: process.env.KINDE_POST_LOGOUT_REDIRECT_URL,
};

const options = {
  audience:  process.env.KINDE_ISSUER_URL || '/api'
}

const verificadorKindeJWT = jwtVerify(process.env.KINDE_DOMAIN, options);
module.exports = { verificadorKindeJWT };

app.set("view engine", "pug");
const client = setupKinde(config, app);

app.get("/", async (req, res) => {
  if (await client.isAuthenticated(req)) {
    res.redirect("/admin");
  } else {
    res.render("index", {
      title: "Hey",
      message: "Hello there! what would you like to do?",
    });
  }
});

app.get("/admin", protectRoute, getUser, (req, res) => {
  res.render("admin", {
    title: "Admin",
    user: req.user,
  });
});

app.listen(port, function () {
  console.log(`Listening on port ${port}!`);
});
I am on leave from tomorrow, but tag @Andre @ Kinde if you need additional help from tomorrow onwards.
Add a reply
Sign up and join the conversation on Discord