Closed Mohit-S-Rane closed 8 months ago
Note: We not prefered to add id field in user model bcz when mongoDB save user in DB, that time create a unique id. This id mongoDB store in BSON data formate not in JSON formate.
// photo store using third-party service, this service give url and you can use where ever you want.
// video and image store separatlly
src/models/{+}user.model.js=>{ import mongoose, {Schema} from "mongoose";//{S}<-use to destructure mongoose Schema
const userSchema = new Schema({ username: { type: String, required: true, unique: true, lowercase: true, trim: true, index: true//for better searching }, email: { type: String, required: true, unique: true, lowercase: true, trim: true }, fullName: { type: String, required: true, trim: true, index: true }, avatar: { type: String //cloudinary url required: true }, coverImage: { type: String //cloudinary url }, watchHistory: [ { type: Schema.Types.ObjectId, ref: "Video" } ], password: { type: String, required: [true, "passowrd is required"] }, refreshToken: { type: String }
}, {timestamps: true})
export const User = mongoose.model("User", userSchema) }
src/models/{+}video.model.js=>{ import mongoose, {Schema} from "mongoose" ^3) import mongooseAggregatePaginate from "mongoose-aggregate-paginate-v2"
const videoSchema = new Schema({ videoFile: { type: String,//cloudinary url required: true }, thumbnail: { type: String,//cloudinary url required: true }, title: { type: String, required: true }, description: { type: String, required: true }, duration: { type: Number,//time get after uploading file->that give details as response required: true }, views: { type: Number, default: 0 }, isPublished: { type: Boolean, default: true }, owner: { type: Schema.Types.ObjectId, ref: "User" } }, {timestamps: true})
^3)=>{ //this aggregate(pipeline) framework is comme to late in mongoDB, thatway we use it as plugin. here we now write aggregation query's, normal query also possible. videoSchema.plugin(mongooseAggregatePaginate) }
export const Video = mongoose.model("Video", videoSchema) }
MongoDB true power -> Aggregation query ./ npm i mongoose-aggregate-paginate-v2 // insert code in video.model.js file
./ npm i bcrypt -> help to hash password. (npm install --save bcryptjs) //bcrypt is for nodejs. bcryptjs is optimized in js
npm i bcrypt jsonwebtoken // JWT -> consist of Header(define which algo use), payload(data), verify signature-> here secret exist to make every token unique, this actuaaly protect form world
+/user.models.js =>{ import jwt from "jsonwebtoken" import bcrypt from "bcrypt"
//after userSchema // not use arrow fun in pre hook-> in js arrow fun not come with "this" ref, here he never know context. thatway here prefered to use function() {}.
userSchema.pre("save", async function(next){ if(!this.isModified("password")) return next() this.password = await bcrypt.hash(this.password, 10) next() })
//custom method
userSchema.methods.isPasswordCorrect = async function(password){ return await bcrypt.compare(passowrd, this.password) }
// need to be add code in .env file
^7=>{ userSchema.methods.generateAccessToken = function(){ return jwt.sign( { //payload _id: this._id email: this.email, username: this.username, fullName: this.fullName }, //access token process.env.ACCESS_TOKEN_SECRET, { expiresIn: process.env.ACCESS_TOKEN_EXPIRY } ) } userSchema.methods.generateRefreshToken = function(){ return jwt.sign( { //payload _id: this._id email: this.email, username: this.username, fullName: this.fullName }, //access token process.env.REFRESH_TOKEN_SECRET, { expiresIn: process.env.REFRESH_TOKEN_EXPIRY } ) } //refresh token carry less information, bcz this refresh token refresh it again and again. This token hold only "_id".
}
//JWT is bearer token, bearer indicate that those hold that token are correct person. i.e those bear that token are valid person, and i will return data to that person.
//before export
}
User and Video model with hooks and JWT, bcrypt