Clerk is the easiest way to add authentication and user management to your Node.js application. To gain a better understanding of the Clerk Backend API, refer to the Backend API documentation.
- Node.js
>=18.17.0
or later - Koa installed (follow their Getting started guide)
npm install @dimkl/clerk-koa
To build the package locally with the TypeScript compiler, run:
npm run build
Retrieve your Backend API key from the API Keys screen in your Clerk dashboard and set it as an environment variable in a .env
file:
CLERK_PUBLISHABLE_KEY=pk_*******
CLERK_SECRET_KEY=sk_******
You will then be able to access all the available methods.
import 'dotenv/config'; // To read CLERK_SECRET_KEY
import { clerkClient } from '@dimkl/clerk-koa';
const { data: userList } = await clerkClient.users.getUserList();
You will also be able to secure an Koa app.
import Koa from "koa";
import { clerkMiddleware, getAuth } from "@dimkl/clerk-koa";
const app = new Koa();
app.use(clerkMiddleware());
app.use(async (ctx: KoaContext) => {
const auth = getAuth(ctx);
ctx.body = `<body>Hello ${auth?.userId}</body>`;
ctx.status = 200;
});
app.listen(3000);
You will also be able to protect a specific endpoint in a Koa app.
import Koa from "koa";
import Router from "@koa/router";
import { clerkMiddleware, getAuth, requireAuth } from "@dimkl/clerk-koa";
const app = new Koa();
app.use(clerkMiddleware());
router.get("/", (ctx) => {
ctx.body = `<body> Public Homepage ${auth.userId}</body>`;
ctx.status = 200;
});
router.get("/protected", requireAuth, async (ctx: KoaContext) => {
const auth = getAuth(ctx);
ctx.body = `<body>Signed-in user "${auth.userId}"</body>`;
ctx.status = 200;
});
app.use(router.routes()).use(router.allowedMethods());
app.listen(3000);
You can get in touch with me in any of the following ways:
- Create a GitHub Discussion and mention
@dimkl
We're open to all community contributions!
@dimkl/clerk-koa
follows good practices of security, but 100% security cannot be assured.
@dimkl/clerk-koa
is provided "as is" without any warranty. Use at your own risk.
For more information and to report security issues, please refer to the security documentation.
This project is licensed under the MIT license.
See LICENSE for more information.