Skip to content

Latest commit

 

History

History
135 lines (89 loc) · 3.77 KB

README.md

File metadata and controls

135 lines (89 loc) · 3.77 KB


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