Authentication and authorization
Twelfth chapter of the "Fastify with TypeScript" series. Let's put together plugins, hooks, decorators and error handling to build a complete JWT authentication system with role-based access control. By the end, users will have hashed passwords, a login route will hand out tokens, and two of our routes will require one.
npm install @fastify/jwt argon2
Enter fullscreen mode
Exit fullscreen mode
@fastify/jwt signs and verifies JSON Web Tokens. argon2 hashes passwords with Argon2id. It is a binding to the reference implementation of the algorithm, written in C, and it does the work off the main thread, so hashing doesn't block the event loop. The package ships prebuilt binaries for Linux (including Alpine), Apple Silicon Macs and Windows, so on those systems there is nothing to compile; elsewhere npm install builds it and needs a C++ compiler.
Storing a password hash
Never store a password.…