/fastify-oauth

Primary LanguageTypeScriptThe UnlicenseUnlicense

Fastify OAuth

The idea of this project is to learn how to build a fast server to be used as a complete OAuth2 server.

Features

  • User registration and login with Credential Authentication
  • Creation of profiles to be used as OAuth2 clients
  • Application Code Grant
  • Access token and Refresh token Grant
  • Token expiration and Token revocation
  • Protected routes
  • And many more...

Technologies used

  • NodeJS as the runtime environment
  • TypeScript for type safety (This is a modern and secure way to write JavaScript)
  • Fastify as a web framework that is faster and has a better UX than Express
  • Zod for data validation and type inference (instead of JSON Schema for example)
  • Prisma as an ORM for a sqlite database (This enable us to create a local database for development that is lightweight and easy to use)
  • Argon2 for password hashing and verification (instead of the commonly used bcrypt, which is slower and less secure)
  • Pino for logging (This is a fast and simple logger)
  • Pino-pretty for pretty logging (This transforms the logs from pino into a more readable format to be used in development)
  • Vitest for testing (Instead of Jest, which is slower and has a worse DX)

Getting started

Prerequisites

  • - NodeJS v19.2.0 or higher installed (Technically, you can use any version of NodeJS, but I recommend using the latest version)
  • - Pnpm v7.18.1 or higher installed (This is a package manager that is faster than npm and yarn, but you can use npm or yarn if you want to)

Installation

  1. Clone the repo
  2. Install dependencies with pnpm i
  3. Apply migrations, seed and generate typings with pnpm db:setup
  4. Run the dev script with pnpm dev
  5. Use the API with Postman or Insomnia or any other API client of your choice

Testing

Testing is done with Vitest. To run the tests, run bun run test, to check the coverage, run bun run coverage.

Building

  • To build the project, run pnpm build. This will create a dist folder with the compiled code. To run the compiled code, run pnpm start.

Why should I build the project when I can run it with pnpm dev?

Bundling is the process of combining multiple files, such as JavaScript or CSS files, into a single file. This allows for fewer requests to the server, which can improve the performance of the website or application.

Minifying is the process of removing unnecessary characters, such as white space, comments, and new lines, from the code to make it smaller and more efficient. This can further improve the performance by reducing the size of the code that needs to be transferred to the user's device.

Together, bundling and minifying can help to optimize the performance of a website or application by reducing the number of requests and the amount of code that needs to be transferred. The pnpm dev command should be used during development, while the pnpm build command, which includes bundling and minifying, should be used in production. This can ensure that the website or application performs well and provides a good user experience.


Contact

Discord -
    Ninjonas#6919 LinkedIn -
    joaocarloscode

Contributing

Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

Distributed under the Unlicense License. See LICENSE for more information.