Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Login/Signup Feature #7

Open
mahendra785 opened this issue Aug 20, 2024 · 1 comment
Open

Login/Signup Feature #7

mahendra785 opened this issue Aug 20, 2024 · 1 comment

Comments

@mahendra785
Copy link
Collaborator

Implement a login and signup feature that allows users to create an account and log in to the website. Ensure that user credentials are securely handled and stored in the database.

@D-Vspec
Copy link
Collaborator

D-Vspec commented Aug 20, 2024

Pseudocode: Implement Login/Signup Feature

Objective:

Implement a login and signup feature that allows users to create an account and log in to the website. Ensure that user credentials are securely handled and stored in the database.

Steps:

1. Define the User Schema in Mongoose

  • User Schema Structure:

    • Fields:
      • username: Unique identifier for the user (e.g., String, required)
      • email: User's email address (e.g., String, required, unique)
      • password: User's hashed password (e.g., String, required)
      • createdAt: Timestamp for when the account was created (e.g., Date, default: current date/time)
  • Mongoose Schema Example:

    const mongoose = require('mongoose');
    const bcrypt = require('bcryptjs');
    
    const userSchema = new mongoose.Schema({
        username: {
            type: String,
            required: true,
            unique: true
        },
        email: {
            type: String,
            required: true,
            unique: true
        },
        password: {
            type: String,
            required: true
        },
        createdAt: {
            type: Date,
            default: Date.now
        }
    });
    
    // Pre-save hook to hash the password before saving to the database
    userSchema.pre('save', async function(next) {
        if (!this.isModified('password')) return next();
        this.password = await bcrypt.hash(this.password, 10);
        next();
    });
    
    // Method to compare entered password with the hashed password
    userSchema.methods.matchPassword = async function(enteredPassword) {
        return await bcrypt.compare(enteredPassword, this.password);
    };
    
    const User = mongoose.model('User', userSchema);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants