Skip to content

AvinashMarkad/Authentication-fullStack

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Authentication-fullStack

Here's an explanation of the use cases for the npm packages you've listed:


1. jsonwebtoken

  • Purpose: To generate and verify JSON Web Tokens (JWT), commonly used for user authentication and secure data exchange.
  • Usage:
    • Generate a token:
      const token = jwt.sign({ id: user._id }, process.env.JWT_SECRET, { expiresIn: '1h' });
    • Verify a token:
      jwt.verify(token, process.env.JWT_SECRET, (err, decoded) => { ... });

2. express

  • Purpose: A minimalist web framework for building server-side applications and APIs in Node.js.
  • Usage:
    • Create a server:
      const express = require('express');
      const app = express();
      
      app.get('/', (req, res) => res.send('Hello World!'));
      app.listen(3000, () => console.log('Server running on port 3000'));

3. nodemon

  • Purpose: A development tool that automatically restarts the Node.js server when file changes are detected.
  • Usage:
    • Run the server with nodemon:
      nodemon index.js

4. bcrypt

  • Purpose: To securely hash passwords and verify them during user authentication.
  • Usage:
    • Hash a password:
      const bcrypt = require('bcrypt');
      const hashedPassword = await bcrypt.hash('password123', 10);
    • Compare a password:
      const isMatch = await bcrypt.compare('password123', hashedPassword);

5. body-parser

  • Purpose: Middleware to parse incoming request bodies (JSON, URL-encoded) for Express applications. It's now included in Express but still available as a standalone package.
  • Usage:
    const bodyParser = require('body-parser');
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: true }));

6. dotenv

  • Purpose: To load environment variables from a .env file into process.env. Useful for storing sensitive configuration like API keys.
  • Usage:
    require('dotenv').config();
    console.log(process.env.JWT_SECRET);

7. mongoose

  • Purpose: An ODM (Object Data Modeling) library for MongoDB and Node.js. Simplifies interactions with MongoDB.
  • Usage:
    • Connect to a MongoDB database:
      const mongoose = require('mongoose');
      mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true });
    • Define a schema:
      const UserSchema = new mongoose.Schema({ name: String, email: String });
      const User = mongoose.model('User', UserSchema);

8. joi

  • Purpose: A validation library for data, useful for validating request payloads, query parameters, etc.
  • Usage:
    const Joi = require('joi');
    const schema = Joi.object({
        name: Joi.string().min(3).required(),
        email: Joi.string().email().required(),
    });
    
    const { error } = schema.validate(req.body);
    if (error) return res.status(400).send(error.details[0].message);

9. cors

  • Purpose: Middleware for enabling CORS (Cross-Origin Resource Sharing) in Express. Allows requests from other domains or origins.
  • Usage:
    const cors = require('cors');
    app.use(cors());

How They Work Together

In a typical backend project:

  1. express handles routing and middleware integration.
  2. dotenv loads sensitive configuration.
  3. mongoose connects to the MongoDB database.
  4. body-parser parses incoming request data.
  5. bcrypt hashes passwords for secure storage.
  6. jsonwebtoken manages authentication tokens.
  7. joi validates incoming request data.
  8. cors allows secure cross-origin requests.
  9. nodemon restarts the server automatically during development.

Let me know if you'd like help setting up a project with these!

Releases

No releases published

Packages

No packages published