0.9.6 • Published 4 months ago

elysia-clerk v0.9.6

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

elysia-clerk

Elysia plugin to integrate with Clerk.

!NOTE This unofficial package isn't connected to Clerk.com. It's a project designed to smoothly incorporate Clerk features to your Elysia applications.

Install

bun add elysia-clerk

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_******

Configure clerkPlugin in your Elysia application

import { Elysia } from 'elysia'
import { clerkPlugin } from 'elysia-clerk'

new Elysia()
  .use(clerkPlugin())
  .get('/private', async ({ clerk, auth, error }) => {
    /**
     * Access the auth state in the context.
     * See the AuthObject here https://clerk.com/docs/references/nextjs/auth-object#auth-object
     */
    if (!auth?.userId) {
      return error(401)
    }

    /**
     * For other resource operations, you can use the clerk client from the context.
     * See what other utilities Clerk exposes here https://clerk.com/docs/references/backend/overview
     */
    const user = await clerk.users.getUser(auth.userId)

    return { user }
  })
  .listen(3000)

Instead of using Clerk globally, you can use Clerk for a subset of routes via Elysia plugins:

import { Elysia } from 'elysia'
import { clerkPlugin } from 'elysia-clerk'

const privateRoutes = new Elysia({ prefix: '/api' })
  .use(clerkPlugin())
  .get('/user', async ({ clerk, auth, error }) => {

    if (!auth?.userId) {
      return error(401)
    }

    const user = await clerk.users.getUser(auth.userId)

    return { user }
  })

new Elysia()
  .use(privateRoutes)
  .get('/', () => 'Hello world!')
  .listen(3000)

To see the available options you can pass to the clerkPlugin function, see AuthenticateRequestOptions.

License

MIT

0.9.6

4 months ago

0.9.5

5 months ago

0.9.4

5 months ago

0.9.3

5 months ago

0.9.2

6 months ago

0.9.1

6 months ago

0.10.1

7 months ago

0.10.0

7 months ago

0.9.0

7 months ago

0.8.8

7 months ago

0.8.7

7 months ago

0.8.6

8 months ago

0.8.5

8 months ago

0.8.4

8 months ago

0.8.3

9 months ago

0.8.2

9 months ago

0.8.1

9 months ago

0.8.0

9 months ago

0.7.0

10 months ago

0.6.1

10 months ago

0.6.0

10 months ago

0.5.5

11 months ago

0.5.4

11 months ago

0.5.3

11 months ago

0.5.2

11 months ago

0.5.1

1 year ago

0.5.0

1 year ago

0.4.0

1 year ago

0.3.1

1 year ago

0.3.0

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.2.2

2 years ago

0.1.0

2 years ago

0.1.1

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago