Step by step, we are putting all the puzzle pieces together. Now, let's look at the authentication functions we are using to validate whether a user is connected or not and get the user's data. For this, we need to use JSON Web Tokens (JWTs).
What is JSON Web Token?
JWT is an open standard – RFC 7519 (https://tools.ietf.org/html/rfc7519) – which is useful for transmitting information between parties as a JSON object. The advantage of JWTs is that they are digitally signed, which is why they can be verified and trusted. It uses the HMAC algorithm to sign the token by using a secret or a public key pair using RSA or ECDSA.
JWT functions
Let's create some functions that will help verify a JWT and get the user data. For this, we need to create the jwtVerify, getUserData, and createToken functions. This file should be created at /backend/src/lib/jwt.ts:
// Dependencies
import jwt from 'jsonwebtoken'
import { encrypt, setBase64,...