Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@clerk/fastify

Package Overview
Dependencies
Maintainers
9
Versions
2235
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@clerk/fastify

Clerk.dev SDK for Fastify

  • 0.1.5-staging.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2.8K
decreased by-56.14%
Maintainers
9
Weekly downloads
 
Created
Source


@clerk/fastify

Chat on Discord Clerk documentation Follow on Twitter

Changelog · Report a Bug · Request a Feature · Ask a Question


Overview

Clerk is the easiest way to add authentication and user management to your Fastify application. To gain a better understanding of the Clerk Backend API and SDK, refer to the Node SDK and Backend API documentation.

Getting Started

To use this plugin you should first create a Clerk application and retrieve a Secret Key and a Publishable Key for you application (see here) to be used as environment variables CLERK_PUBLISHABLE_KEY & CLERK_SECRET_KEY.

Prerequisites

  • Node.js v14+
  • Fastify v4+

Installation

npm install @clerk/fastify

Build

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_PUBLISHABLE_KEY
import Fastify from 'fastify';
import { clerkPlugin, getAuth } from '@clerk/fastify';
import type { FastifyInstance } from 'fastify';

const server: FastifyInstance = Fastify({ logger: true });

server.register(clerkPlugin);

server.get('/private', async (req, reply) => {
  const auth = getAuth(req);
  if (!auth.userId) {
    return reply.code(403).send();
  }
  return { hello: 'world' };
});

const start = async () => {
  try {
    await server.listen({ port: 3000 });
  } catch (err) {
    server.log.error(err);
    process.exit(1);
  }
};
start();

Scoped routes

Support authenticated routes that the Clerk middleware will run as plugin in preHandler hook and unauthenticated routes that will not trigger the middleware using the Fastify docs.

import 'dotenv/config'; // To read CLERK_PUBLISHABLE_KEY
import Fastify from 'fastify';
import { clerkPlugin, getAuth } from '@clerk/fastify';
import type { FastifyReply, FastifyRequest, FastifyInstance } from 'fastify';

const server: FastifyInstance = Fastify({ logger: true });

const privateRoutes = async (fastify: FastifyInstance, _opts: any) => {
  fastify.register(clerkPlugin);

  fastify.get('/private', async (req: FastifyRequest, reply: FastifyReply) => {
    const auth = getAuth(req);

    if (!auth.userId) {
      return reply.code(403).send();
    }

    return { hello: 'world', auth };
  });
};

server.register(privateRoutes);

server.get('/public', async (_req, _reply) => {
  return { hello: 'world' };
});

const start = async () => {
  try {
    await server.listen({ port: 3000 });
  } catch (err) {
    server.log.error(err);
    process.exit(1);
  }
};
start();

Support

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

Security

@clerk/fastify follows good practices of security, but 100% security cannot be assured.

@clerk/fastify is provided "as is" without any warranty. Use at your own risk.

For more information and to report security issues, please refer to our security documentation.

License

This project is licensed under the MIT license.

See LICENSE for more information.

Keywords

FAQs

Package last updated on 18 Feb 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc