A progressive Node.js framework for building efficient and scalable server-side applications.
- Writing articles
- Follow & Unfollow different users
- install
class-validator
andclass-transformer
package. - use @UsePipes decorator
- apply validation decorator in DTO
-
install
jwtwebtoken
-
apply sign() method and provide 2 args (payload, JWT_SECRET)
-
that's all. you get back a string in return
- the idea of this is to obtain the jwt token from Authorization header
- decode the token with the JWT_SECRET (YOUR SECRET) and you retrieve your payload
- most likely the payload contains some user info, eg user id and username
- use available info to retrieve user object from the database
- set request.user to user so you can user anywhere
if (!req.user) {
// user is not logged in
}
Guard is the right place to throw Error when the token is not provided or invalid, or unauthorized
Simply create a UserGuard implements CanActivate. If the context logic, if req.user* === null then throw error, else proceed req.user will be set as the request goes through the middleware:)
Do:
docker run --name postgresql-container -p 5432:5432 -e POSTGRES_PASSWORD=123 -d postgres
type: 'postgres',
host: 'localhost',
port: 5432, // this must match the port of the container
username: 'postgres', // stick to postgres
password: '123', // this is the password that is set when u set up docker container
database: 'postgres',
ensure docker is running on the right port
- drop the db (if you need to):
db:drop
- create migration:
db:create 'GIVE_THIS_A_MIGRATION_NAME'
- run the migration:
db:migrate
- seed the database:
db:seed
$ yarn
$ npm run start