K
Kinde11mo ago
MaDsEn

Typescript SDK middleware route handling.

Building a project using fresh.deno and Kindes Typescript SDK. Have it setup, with pretty basic implementation. Having some issues about how to best setup the _middleware.ts file so we can create private/protected routes. Have anyone made an example for how to best implement it??. Thanks again..
2 Replies
onderay
onderay11mo ago
Hey @MaDsEn as of now, we don't have a built-in feature or specific guide for setting up middleware with the Kinde TypeScript SDK in a Deno environment. The examples we have are mainly for Node.js and Next.js environments. However, you can create your own middleware function to check if a user is authenticated before accessing certain routes. Here's a basic example of how you might do this:
import { createKindeServerClient, GrantType } from "@kinde-oss/kinde-typescript-sdk";

// Initialize Kinde client
const kindeClient = createKindeServerClient(GrantType.AUTHORIZATION_CODE, {
authDomain: "https://<your_kinde_subdomain>.kinde.com",
clientId: "<your_kinde_client_id>",
clientSecret: "<your_kinde_client_secret>",
redirectURL: "http://localhost:3000/callback",
logoutRedirectURL: "http://localhost:3000"
});

// Middleware to protect routes
async function isAuthenticated(req, res, next) {
const token = req.headers.authorization;

try {
const user = await kindeClient.getUser(token);
if (user) {
req.user = user;
next();
} else {
res.status(401).send('Unauthorized');
}
} catch (error) {
res.status(500).send('Error authenticating user');
}
}

// Use middleware in routes
app.get('/protected-route', isAuthenticated, (req, res) => {
res.send('You are authenticated');
});
import { createKindeServerClient, GrantType } from "@kinde-oss/kinde-typescript-sdk";

// Initialize Kinde client
const kindeClient = createKindeServerClient(GrantType.AUTHORIZATION_CODE, {
authDomain: "https://<your_kinde_subdomain>.kinde.com",
clientId: "<your_kinde_client_id>",
clientSecret: "<your_kinde_client_secret>",
redirectURL: "http://localhost:3000/callback",
logoutRedirectURL: "http://localhost:3000"
});

// Middleware to protect routes
async function isAuthenticated(req, res, next) {
const token = req.headers.authorization;

try {
const user = await kindeClient.getUser(token);
if (user) {
req.user = user;
next();
} else {
res.status(401).send('Unauthorized');
}
} catch (error) {
res.status(500).send('Error authenticating user');
}
}

// Use middleware in routes
app.get('/protected-route', isAuthenticated, (req, res) => {
res.send('You are authenticated');
});
Please note that this is a very basic example and might not cover all your needs. You might need to adjust it according to your application's requirements. Also, remember to replace the placeholders with your actual Kinde credentials.
MaDsEn
MaDsEnOP11mo ago
Thank you for the respons. Will check it out👍. It was just easier if someone had made an example,😊 but have a few ideas that might work for a more structured project.
Want results from more Discord servers?
Add your server