- installing nodemon
- setting type as module in package.json
- adding gitignore file and generate it from online gitignore generator
- installing prettier
- making prettierignore file
- creating env file
- creating necessary folder structure in src
- create account on mongo atlas
- create project and set up access database user and access
- create network access, set ip - 0.0.0.0/0. Don't set this ip for production.
- Add port to env file
- Connect db with backend using connection string from compass.
- Install mongoose, dotenv and express.
- Install cookie-parser, cors.
- cors
- static data
- data from url(url encoding)
- cookieParser
- streamlining error and response.
- user modal
- video modal
- install mongoose aggregate paginate
- install bcrypt (password encryption)
- install jsonWebToken (token encryption)
- encrypting password before saving using bcrypt
- generating access token and refresh token
- install couldinary, multer(file upload)
- writing cloudinary logic for file upload from local.
- writing multer middleware for saving file to local.
- Configuting routes and controlles.
- Writing logic for user.
- Accept fields from req.body
- use multer middleware in user.routes.js and get files uploaded on system.
- check for validation for each filed in user. For errors use ApiError Wrapper.
- check whether user with similar username or email exist or not using operators.
- Getting images and uploading to cloudinary.
- Creating user, removing password and refreshToken.
- Conforming user creation and sending response.
- Testing on Postman
- Creating login logic for user in controller.
- Creating function for token generation in user controller.
- Creating middleware for auth checking using jwt.
- Creating logic for logging out user.
- Adding new Endpoint for updatingAccessToken in user controller if refreshToken is not expired.
- Creating subscription model
- Creating endPoints in userController to update password, getCurrentUser, updateAccountDetails, updateAvatarImage and updateCoverImage.
- Creating new endpoint in users controller for getting user channel Info.
- Adding WatchHistory endPoint.
- Adding routes for user controller.
- Add comment, like, playlist, tweet models.
-
In simple terms : Jaha bhi jaa rhe ho ik baar milke jaana.
-
Now user send some request on instagram.com/something now api will be called to serve this request and give some response. Before serving this request we want to check whether user is logged in or not. This is done by middleware and we can can add more than one middleware before these request.
-
WhenEven user logs into a system accessToken and RefreshToken is given to user. Generally accessToken has short span than refreshToken. Suppose after one day user again hit the endpoint and in backend the code will check whether user is still logged in or not. Since the accessToken expires after one day system will send 401 status to frontend and user have to login again. So instead of this what we can do once frontend get 401 request it will send request to another endpoint to check refresh token for same user in db. If both are matched then system will generate new accessToken to user.
-
So in user experience he/she didn't logged in again but in backend its kind of logging in based on refreshToken.
- Always remember "Database is in another continent" so it will take time to respond.
- Always use async-await and try-catch (because there are more chance of things not going your - - way) for db.
- Use app.use() for middlewares
- JWT is a bearer token. It's like a key.
- When we do req.user._id what we will get some will say mongoDb id but we will only get string ('662552cbbcdf90fbf97fb8ca') not mongoDb id. MongoDb id is like this ObjectId('662552cbbcdf90fbf97fb8ca'). Since we use mongoose it will automatically convert this string to mongoDb id when we use find or findById. But when we use aggregation pipeline mongoose doesn't interfere with aggregation pipeline. The code written in aggregation pipeline goes directly.So here we need to make mongoose ObjectId. See in auth controller endpoint getWatchHistory.