@dimkl/clerk-koa
TypeScript icon, indicating that this package has built-in type declarations

0.1.2 • Public • Published


@dimkl/clerk-koa


Overview

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.

Getting started

Prerequisites

  • Node.js >=18.17.0 or later
  • Koa installed (follow their Getting started guide)

Installation

npm install @dimkl/clerk-koa

To build the package locally with the TypeScript compiler, run:

npm run build

Usage

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);

Support

You can get in touch with us in any of the following ways:

Contributing

We're open to all community contributions!

Security

@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.

License

This project is licensed under the MIT license.

See LICENSE for more information.

Readme

Keywords

Package Sidebar

Install

npm i @dimkl/clerk-koa

Homepage

clerk.com/

Weekly Downloads

2

Version

0.1.2

License

MIT

Unpacked Size

58 kB

Total Files

9

Last publish

Collaborators

  • dimkl