https://github.com/qlaffont/fastify-auth-prisma
Fastify plugin with Prisma to make simple & secure authentification middleware.
https://github.com/qlaffont/fastify-auth-prisma
authentication fastify prisma simple typescript
Last synced: 9 months ago
JSON representation
Fastify plugin with Prisma to make simple & secure authentification middleware.
- Host: GitHub
- URL: https://github.com/qlaffont/fastify-auth-prisma
- Owner: qlaffont
- License: mit
- Created: 2022-08-18T07:07:04.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2024-07-16T07:09:24.000Z (over 1 year ago)
- Last Synced: 2025-04-21T14:53:42.632Z (9 months ago)
- Topics: authentication, fastify, prisma, simple, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/fastify-auth-prisma
- Size: 5.62 MB
- Stars: 4
- Watchers: 4
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://codeclimate.com/github/qlaffont/fastify-auth-prisma/maintainability) [](https://codeclimate.com/github/qlaffont/fastify-auth-prisma/test_coverage)    
# Fastify-Auth-Prisma
Fastify plugin with Prisma to make simple & secure authentification middleware. Old Owner: [@flexper](https://github.com/flexper)
## Usage
```bash
pnpm install fastify-auth-prisma unify-fastify prisma @prisma/client
```
[Initialize Prisma](https://www.prisma.io/docs/getting-started) and create a similar schema.prisma
```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
```typescript
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](https://github.com/vercel/ms) |
| refreshTokenTime | string | Time validity for refreshToken [Help for time format](https://github.com/vercel/ms) |
**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](https://github.com/vercel/ms) |
**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 :
```bash
docker-compose up -d
chmod -R 777 docker
pnpm prisma migrate deploy
pnpm test
```
## Maintain
This package use [TSdx](https://github.com/jaredpalmer/tsdx). Please check documentation to update this package.