npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fastify-auth-prisma

v1.2.444

Published

[![Maintainability](https://api.codeclimate.com/v1/badges/6e747003545ffe76ceac/maintainability)](https://codeclimate.com/github/qlaffont/fastify-auth-prisma/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/6e747003545ffe76ceac/test

Downloads

1,733

Readme

Maintainability Test Coverage npm npm Snyk Vulnerabilities for npm package NPM

Fastify-Auth-Prisma

Fastify plugin with Prisma to make simple & secure authentification middleware. Old Owner: @flexper

Usage


pnpm install fastify-auth-prisma unify-fastify prisma @prisma/client

Initialize Prisma and create a similar schema.prisma

model Token {
  id           String @id @unique @default(uuid())
  refreshToken String
  accessToken  String

  owner   User   @relation(fields: [ownerId], references: [id])
  ownerId String

  createdAt DateTime @default(now())
}

model User {
  id            String  @id @unique @default(uuid())

  tokens          Token[]

  createdAt       DateTime         @default(now())
  updatedAt       DateTime         @updatedAt
}

Add your plugin in your fastify server

import fastify from 'fastify';
import { PrismaClient, User } from '@prisma/client';
import unifyFastifyPlugin from 'unify-fastify';
import {fastifyAuthPrismaPlugin} from 'fastify-auth-prisma';

const prisma = new PrismaClient();
const server = fastify();

declare module 'fastify' {
  interface FastifyRequest {
    connectedUser?: User;
  }
}

await server.register(unifyFastifyPlugin);

await server.register(fastifyAuthPrismaPlugin, {
  config: [{url: "/public/*", method: 'GET'}],
  prisma,
  secret: process.env.JWT_ACCESS_SECRET, // Recommanded to use an external variable but you can use any generated string
});

API

fastifyAuthPrismaPlugin

Options

| Field Name | Type | Description | | -------------- | ------------------------------------------------ | ---------------------------------------------------------------------------------- | | config | {url: string, method: HttpMethods}[] | Specify which urls are allowed without valid token | | cookieIsSigned | boolean [OPTIONAL] | If cookies is used, precise if value is signed | | secret | string | Secret use for accessToken generation | | prisma | Prisma Client | | | userValidation | (user: Prisma[User]) => Promise [OPTIONAL] | Function to run to add userValidation on request (ex: isBanned / isEmailValidated) |

Return

| Field Name | Type | Description | | ------------- | -------------- | ----------------------------- | | connectedUser | Prisma["User"] | Connected user | | isConnected | boolean | Return if a user is connected |

createUserToken(prisma)(userId, {secret, refreshSecret, accessTokenTime, refreshTokenTime})

Options | Field Name | Type | Description | | ---------------- | ------------- | ----------------------------------------------------------------------------------- | | prisma | Prisma Client | | | userId | string | | | secret | string | Secret use for accessToken generation | | refreshSecret | string? | Secret use for refreshToken generation | | accessTokenTime | string | Time validity for accessToken Help for time format | | refreshTokenTime | string | Time validity for refreshToken Help for time format |

Return

| Field Name | Type | Description | | ------------ | ------ | ----------- | | accessToken | string | | | refreshToken | string | |

removeUserToken(prisma)(accessToken)

Options | Field Name | Type | Description | | ----------- | ------------- | ----------- | | prisma | Prisma Client | | | accessToken | string | |

Return void

removeAllUserTokens(prisma)(userId)

Options | Field Name | Type | Description | | ---------- | ------------- | ----------- | | prisma | Prisma Client | | | userId | string | |

Return void

refreshUserToken(prisma)(refreshToken, { secret, refreshSecret, accessTokenTime })

Options | Field Name | Type | Description | | --------------- | ------------- | ---------------------------------------------------------------------------------- | | prisma | Prisma Client | | | refreshToken | string | Refresh token generated | | secret | string | Secret use for accessToken generation | | refreshSecret | string | Secret use for refreshToken generation | | accessTokenTime | string | Time validity for accessToken Help for time format |

Return

| Field Name | Type | Description | | ------------ | ------ | ----------- | | accessToken | string | | | refreshToken | string | |

getAccessTokenFromRequest(req)

Options | Field Name | Type | Description | | ---------- | --------------- | ----------- | | req | Fastify request | |

Return string

Config Array

To configure your public routes, you need to specify your url and your method. You can use some alias too :

  • Standard example : {url: '/test/toto', method: 'GET'}
  • Match url who start with test : {url: '/test/*', method: 'GET'}
  • Match all methods for this url : {url: '/test/toto', method: '*'}
  • Match url who contain dynamic variable in it : {url: '/test/:var1/test', method: 'GET'}

You can combine all this options of course ! {url: '/test/:testvar/toto/*', method: '*'}

Test

To test this package, you need to run a PostgresSQL server :


docker-compose up -d
chmod -R 777 docker
pnpm prisma migrate deploy
pnpm test

Maintain

This package use TSdx. Please check documentation to update this package.