Giter Site home page Giter Site logo

maybemaby / fastify-jumpstart Goto Github PK

View Code? Open in Web Editor NEW
6.0 0.0 0.0 782 KB

Template for a database agnostic fastify typescript web API including local JWT auth, OAS3, and testing.

License: MIT License

Shell 0.55% TypeScript 98.64% Dockerfile 0.81%
authentication boilerplate fastify oas3 starter-kit typescript

fastify-jumpstart's Introduction

Fastify Jumpstart

A starter kit template for Fastify Typescript REST APIs.

Features

  • Typescript
  • OpenAPI v3 with fastify-swagger
  • Testing with Tap
  • ESLint
  • Database Agnostic
  • Local Auth with JWT and refresh tokens

Available Optional Plugins

  • Prometheus for monitoring

Project Structure

src/app.ts

Contains the build function for bootstrapping a fastify instance. Also contains configuration for default plugins like fastify-sensible, apiRouter, fastify-swagger, and healthRoute.

src/server.ts

Actual server instance where you can apply any additional plugins and initialize the web serve.

src/plugins

Contains all plugins like prom and localAuth.

src/schema

Contains schema definitions. Using Typebox as the type provider for fastify.

src/index.d.ts

Type definitions for extending Fastify and its plugins.

src/routes

Contains router plugin definitions. By default, includes a /health, /api, and some placeholder endpoints to demonstrate defining more.

Example of adding routes.

src/routes/api/placeholder/placeholderRouter.ts

import { FastifyPluginCallback } from "fastify";
import { TypeBoxTypeProvider } from "@fastify/type-provider-typebox";

export const placeholderRouter: FastifyPluginCallback = (
  instance,
  opts,
  done
) => {
  // Important for type-safe schemas
  const fastify = instance.withTypeProvider<TypeBoxTypeProvider>();

  fastify.get("/", async (req, reply) => {
    reply.send("Success");
  });

  done();
};

src/routes/api/index.ts

import { FastifyPluginCallback } from "fastify";
import { placeholderRouter } from "./placeholder/placeholderRouter";

export const apiRouter: FastifyPluginCallback = (fastify, opts, done) => {
  // ...
  fastify.register(placeholderRouter, { prefix: "/placeholder" });
  done();
};

Local Authorization Plugin

A local authorization plugin is included in src/plugins/localAuth. It utilizes fastify-jwt to implement access and refresh tokens. Access tokens are validated in the Authorization header. Refresh tokens are stored in a HttpOnly cookie. Adds /login, /signup, /logout, and /refresh endpoints. To get started, you should define logic for the login, signup, logout, and refresh hooks.

app.register(localAuth, {
  // Manually configure jwt settings. See fastify-jwtdocumentation for options
  // accessJwt: {
  //  ...
  // },
  // refreshJwt: {
  //  ...
  // },
  // Select a different root path, defaults to /auth
  // path: '/auth',
  // Override default cookie serialize options
  // refreshCookie: {
  //  ...
  // },
  // Automatically refreshes access tokens in a x-access-token-header
  // when an expired access token is used and a refresh token is in cookies.
  // autoRefresh: false,
  signUp(user) {
    // Enter some logic to process signups and return a UserType
    return { id: "some-id", provider: "email" };
  },
  login(user) {
    // Enter some logic to process logins and return a UserType
    return { id: "user.id", provider: "email" };
  },
  logout(jti) {
    // Enter some logic if you want to blacklist the jti
  },
  refresh(_jti: string): boolean {
    // Enter some logic to verify a refresh token jti is valid, return true if valid
    // A UUID is generated during signing to use as a jti
  },
});

Defaults

  • Access Tokens expire in 24 hours and do not auto refresh.
  • Refresh Tokens expire in 30 days.
  • Refresh Tokens stored in a HttpOnly SameSite=Lax Cookie
  • Jti is a uuid.
  • Users have an email and password.

Protecting routes

import fastify from "fastify";

fastify.addHook("onRequest", fastify.authorize);  

Customizing the UserType

User properties and the jwt payload can be customized under the src/index.d.ts file.

declare module "@fastify/jwt" {
  interface FastifyJWT {
    payload: { id: string; provider: string }; // payload type is used for signing and verifying
    user: {
      id: string;
      provider: string;
    }; // user type is return type of `request.user` object
  }
}

Testing

Test runner is node-tap.

# Run all tests
npm run test
# Run all tests in watch mode
npm run test:w
# Test routes only
npm run test:routes
# Run tests marked "only"
npm run test:only

Docker

docker build --tag fastify-jumpstart .
docker run -p 5000:5000 -d  --name jumpstart-app fastify-jumpstart  

Todo:

  • Auth
  • Refresh access token automatically if it fails and refresh token is there
  • Type enforce defining jti for custom jwt settings
  • API Versioning

fastify-jumpstart's People

Contributors

dependabot[bot] avatar maybemaby avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.