This project is a YouTube clone built using the MERN (MongoDB, Express.js, React.js, Node.js) stack. It aims to replicate core functionalities of YouTube, allowing users to view, upload, share, and interact with videos.
Clone the repository:
$ git clone https://github.com/Dark-Knight-Avi/aktube.git
Install dependencies in both the frontend and backend folders:
$ npm install
Set up environment variables for MongoDB connection and JWT secret. Start the frontend and backend servers:
$ npm start
in both folders.
This script will generate a random 64 bit ASCII string, e.g. that can be used for encrypting JWT tokens/access tokens/refresh tokens:
Run this script on the terminal:
$ node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"
Let’s explain the magic:
- node -e tells Node.js to evaluate a script, in this case, a Javascript string
- crypto is the cryptographic module forming part of Node.js core. It is already installed as part of Node.js, no extra npm package is involved.
- randomBytes() is a function that generates cryptographically strong pseudo-random data. It will return a Buffer object.
- toString() is a method of the Buffer class that decodes the object to a string according to the specified character encoding, which, in this case, is hex, viz. hexadecimal.
Each of the 64 characters can be:
- A numbers from 0 to 9
- A character: A, B, C, D, E and F
There are codes in the ASCII table that will not be used, but the random results are perfect for JWT tokens nevertheless.
This plugin allows you to paginate your Mongoose queries with aggregations.
videoSchema.plugin(mongooseAggregatePaginate)
This hook is executed before an operation occurs. In our schema we use it to hash passwords before saving them into the database.
userSchema.pre("save", async function (next){
if(!this.isModified("password")) return next()
this.password = bcrypt.hash(this.password, 10)
next()
})
Methods are added to schema by adding to schema.method or schema.statics.
userSchema.methods.isPasswordCorrect = async function (password) {
return await bcrypt.compare(password, this.password)
}
Used for hashing passwords before saving them into database.
bcrypt.hash()
is used when creating new password and bcrypt.compare()
is used while checking entered password.
$ npm i bcrypt
userSchema.pre("save", async function (next){
if(!this.isModified("password")) return next()
this.password = bcrypt.hash(this.password, 10)
next()
})
userSchema.methods.isPasswordCorrect = async function (password) {
return await bcrypt.compare(password, this.password)
}
Used for generating JSON Web Tokens that can be used for authentication.
JSON Web Tokens are an open, industry standard RFC 7519 method for representing claims securely between two parties.
$ npm i jsonwebtoken