Make an article app with the auth process in Express JS using Sequelize, JWT, Passport and Node JS.
git init
npm init
npm install express dotenv ejs pg sequelize
- Create
env
with contents of :- DB_USERNAME=""
- DB_PASSWORD=""
- DB_DATABASE=""
- DB_DIALECT=""
- DB_PORT=""
- DB_HOST=""
- PORT=""
- Create .gitignore with :
- node_modules
- package-lock.json
- .env
sequelize init
- Rename
config.json
->config.js
- Use
dotenv
to use environment variable in sequelize config
- Use
- Change
model/index.js
config variable require to changedconfig.js
sequelize db:create
sequelize model:create --name Users --attributes username:string,password:string,fullName:string
sequelize model:create --name Articles --attributes content:text, userId:string
- At Migration User file, change Data Type :
- id to STRING(22)
- Delete the auto increment on ID
- username to STRING(18)
- fullName to STRING(52)
- At Migration Articles file, change Data Type :
- id to STRING(22)
- Delete the auto increment on ID
- userId STRING(22)
- Add References on userId to model Users with key Id
sequelize db:migrate
- In Articles Model, define the association : relation hasOne to Users Model
- In Users Model, define the association : relation hasMany to Articles Model
- Create
index.js
file- User dotenv
- Initiate Express
- Use express.json() middleware
- Create
route
folder withauthRoute.js
file- /login with return of request body
- /register with return of request body
- Require authRoute.js to index.js and use it as middleware with route prefix
- Add start and dev script in package.json
- Create controller folder with userController.js
npm i bcrypt
-> Library that allowed to encrypted passwordnpm install nanoid
npm install jsonwebtoken
require to the controller- Add JWT_SECRET env variable
- Make register and login method
- User userController to register and login route in authRoute.js
- Create
articleController.js
with CRUD methods - Create
artcileRoute.js
with CRUD methods - Require articleRoute to
index.js
with /article prefix npm i passport passport-jwt
- Create a middleware folder with
passportMiddleware.js
file to verify the header before accessing- Implement
passport jwt middleware
- Implement
- Use passport & middleware to
articleRoute.js
- Modify POST & GET method in
articleRoute.js
to use user id in token
heroku login
heroku create app-name
- Checking the
git remote -v
will be display a new remote named heroku heroku addons:create heroku-postgresql
> add addon postgres- Checking the addon :
heroku addons --all
- Check the addons :
heroku addons:services
heroku addons:services | grep postgres
will show the only line including postgres words
- To see the addon list that has been added :
heroku addons: services --all
- Checking environment variable in Heroku :
heroku config -s
- Set or add the environment variable :
heroku config:set JWT_SECRET="secret jwt" PGSSLMODE=no-verify
npm i -D sequelize-cli
- Add
build
script with valuesequelize:db create
- Change the development setting on
config.js
"use_env_variable": "DATABASE_URL"
logging: false
- Push the file to heroku :
git push -f heroku master