Registration, authentication & authorization
-
Check about existing user schema & model& use schema inP.S. Remember about reexports form schemas/index.js and models/index.js
REGISTRATION
-
Add the route for registration
routes\auth.routes.js
See the Pen 44__Register-route by Andrii (@imitator) on CodePen.
P.S. Remember about reexport in routes\index.jsconst auth = require('./auth.routes');module.exports = {auth};Also remember about route in main server.js (index.js)app.use('/api/v1/auth', routes.auth); -
Main 2 steps in register
-
Look at an user existing in DB
-
services\user.services.js
See the Pen 42__getOneUser_service by Andrii (@imitator) on CodePen.
P.S. Remember about reexport from services folder in services\index.jsconst userServices = require('./user.services');module.exports = { userServices, }; -
controllers\auth\registerCTRL.js
See the Pen 43__registerCTRL__checkUserExistsInDB by Andrii (@imitator) on CodePen.
-
services\user.services.js
-
Add user.
-
services\user.services.js
There are two ways of using services to add users (USE ONLY ONE OF THEM). These two ways is making with hashing password. So install for this npm i bcryptjs- Way 1: is simplest one - use hashing inside in service
See the Pen 45__addUserService by Andrii (@imitator) on CodePen.
P.S. If you do it for the first time, try it without hashing.- Way 2 - hash password in separate function in models\schemas\user.schema.jsIn this way the service will be -
controllers\auth\registerCTRL.js
See the Pen 46__Register_CTRL__add-user by Andrii (@imitator) on CodePen.
-
services\user.services.js
-
Look at an user existing in DB
LOGIN (authentication)
-
Add the route for login
routes\auth.routes.jsrouter.post('/login', ctrl.loginCTRL);
-
Look at an user exists in DB
and compare password at the same time (there are two
ways, USE ONLY ONE OF THEM).
(In both ways a business approach used in most of the times, b/c user should think about smth incorrect, he wouldn't be known about what exactly wrong, so we response the general answer: code: 400, message: `Bad request`, or message: `Incorrect password or login`)WAY 1. Existing user & compare password in one place - controllers\auth\loginCTRL.js
See the Pen 49__loginCTRL_approach1__use-ComparePaasword-In-CTRL by Andrii (@imitator) on CodePen.
P.S. But if business give us task to write all answers, we should do it like this code -
Generate token and return it in response
- npm i jsonwebtoken
- .env - add some kind of SECRET_KEY=someString
-
See the Pen 52__loginCTRL__generateTOKEN by Andrii (@imitator) on CodePen.
LOGOUT
Main task in logout technique is deleting token in front- and back-end. F.e. in frontend token was deleted, but if someone copy token from local storage, he may goes to forbidden routes, b/c in backend the token wasn't deleted. There are some options for a user logout.
We show easiest way to delete a token in backend - saving it in db.
-
Add token field to the user schema.
-
schemas\user.schema.js
token: {},type: String,default: null,
-
schemas\user.schema.js
-
Create service for user update, f.e. by id.
-
services\users\updateUserByIdSRV.js
See the Pen 78__logout-step2__updateUserByIdSRV by Andrii (@imitator) on CodePen.
-
services\users\updateUserByIdSRV.js
-
Add saving new token in login.
-
controllers\auth\loginCTRL.js
await service.updateUserByIdSRV(user._id, { token });
See the Pen 79__step-3_in-LOGOUT__loginCTRL by Andrii (@imitator) on CodePen.
-
controllers\auth\loginCTRL.js
-
Create controller for logout.
-
controllers\auth\logoutCTRL.js
See the Pen 80__LOGOUT-STEP-4__logoutCTRL by Andrii (@imitator) on CodePen.
-
controllers\auth\logoutCTRL.js
-
Forbid of entering saving routes after logout by changing
authMDV.
-
middlewares\authMDW.js
old lineif (error || !user) {to new lineif (error || !user || !user.token) {
See the Pen 81__LOGOUT-STEP-5__ by Andrii (@imitator) on CodePen.
-
middlewares\authMDW.js
-
Add logout route.
-
routes\auth.routes.js
add lines
const { authMDW } = require('../middlewares');router.get('/logout', authMDW, ctrl.logoutCTRL);
See the Pen 82__LOGOUT-STEP-6__ by Andrii (@imitator) on CodePen.
-
routes\auth.routes.js
add lines
- Add token for get request when LOGOUT