User Sign Up

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 298:- User Sign Up

To implement user sign up functionality in an Express.js app, we need to follow these steps:

  1. Create a POST route for /signup in the server.js file.
  2. Install and require the bcrypt module to hash the password before storing it in the database.
  3. Create a Mongoose schema for the User model and define the required fields, such as email and password.
  4. In the POST route for /signup, retrieve the email and password from the request body.
  5. Hash the password using bcrypt and save the new user in the database using the User model.
  6. Create a session for the user by adding a user property to the req.session object.
  7. Redirect the user to the home page or any other desired page.

Here's an example implementation of these steps:

phpCopy code

// server.js const express = require('express'); const mongoose = require('mongoose'); const bodyParser = require('body-parser'); const bcrypt = require('bcrypt'); const session = require('express-session'); const app = express(); mongoose.connect('mongodb://localhost:27017/myapp', { useNewUrlParser: true, useUnifiedTopology: true }); const db = mongoose.connection; db.on('error', console.error.bind(console, 'MongoDB connection error:')); app.set('view engine', 'ejs'); app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.json()); app.use(session({  secret: 'myapp',  resave: false,  saveUninitialized: true })); const userSchema = new mongoose.Schema({  email: { type: String, required: true },  password: { type: String, required: true } }); const User = mongoose.model('User', userSchema); app.get('/', (req, res) => {  res.render('index'); }); app.post('/signup', async (req, res) => {  const { email, password } = req.body;  const hashedPassword = await bcrypt.hash(password, 10);  const user = new User({    email,    password: hashedPassword  });  await user.save();  req.session.user = user;  res.redirect('/'); }); app.listen(3000, () => console.log('Server started on port 3000'));

In this example, we've used the bcrypt module to hash the password before storing it in the database. We've also used the express-session middleware to create a session for the user. Finally, we've added a POST route for /signup that saves the new user to the database and creates a session for them.

34. Manual Authentication

2 Comments

@niteshguptav63
niteshguptav63 Nov 17, 2024 at 1:39 PM

I am not able to access videos from second class and further. I have already completed first class

@niteshguptav63
niteshguptav63 Nov 16, 2024 at 10:56 AM

When will I get my course?

@admin79
admin79 Nov 17, 2024 at 1:29 PM

Now, Your query was resolved.

Frequently Asked Questions About Sciaku Courses & Services

Quick answers to common questions about our courses, quizzes, and learning platform

Didn't find what you're looking for?

help_center Contact Support