I am using bcryptjs library to encrypt password field but it is not able to encrypt and send to server.
The error that I’m getting is “localhost didn’t send any data” on my live server and
“/Users/vishnuvardhanvankayalapati/Desktop/A-Complete-Online-Shop/node_modules/bcryptjs/dist/bcrypt.js:214
nextTick(callback.bind(this, Error(“Illegal arguments: “+(typeof s)+’, ‘+(typeof salt))));
^
Error: Illegal arguments: undefined, number
at _async (/Users/vishnuvardhanvankayalapati/Desktop/A-Complete-Online-Shop/node_modules/bcryptjs/dist/bcrypt.js:214:46)
at /Users/vishnuvardhanvankayalapati/Desktop/A-Complete-Online-Shop/node_modules/bcryptjs/dist/bcrypt.js:223:17
at new Promise ()
at bcrypt.hash (/Users/vishnuvardhanvankayalapati/Desktop/A-Complete-Online-Shop/node_modules/bcryptjs/dist/bcrypt.js:222:20)
at User.signup (/Users/vishnuvardhanvankayalapati/Desktop/A-Complete-Online-Shop/models/user.model.js:19:45)
at signup (/Users/vishnuvardhanvankayalapati/Desktop/A-Complete-Online-Shop/controllers/auth.controller.js:17:14)
at Layer.handle [as handle_request] (/Users/vishnuvardhanvankayalapati/Desktop/A-Complete-Online-Shop/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/vishnuvardhanvankayalapati/Desktop/A-Complete-Online-Shop/node_modules/express/lib/router/route.js:149:13)
at Route.dispatch (/Users/vishnuvardhanvankayalapati/Desktop/A-Complete-Online-Shop/node_modules/express/lib/router/route.js:119:3)
at Layer.handle [as handle_request] (/Users/vishnuvardhanvankayalapati/Desktop/A-Complete-Online-Shop/node_modules/express/lib/router/layer.js:95:5)” on terminal.
auth.controller.js
const User = require("../models/user.model");
function getSignup(req, res) {
res.render("customer/auth/signup");
}
async function signup(req, res) {
const user = new User(
req.body.email,
req.body.password,
req.body.fullname,
req.body.street,
req.body.postal,
req.body.city
);
await user.signup();
res.redirect('/login');
}
function getLogin(req, res) {
res.render("customer/auth/login");
}
module.exports = {
getSignup: getSignup,
getLogin: getLogin,
signup: signup,
};
database.js
const mongodb = require('mongodb');
const MongoClient = mongodb.MongoClient;
let database;
async function connectToDatabase() {
const client = await MongoClient.connect('mongodb://localhost:27017');
database = client.db('online-shop');
}
function getDb() {
if (!database) {
throw new Error('You must connect first!');
}
return database;
}
module.exports = {
connectToDatabase: connectToDatabase,
getDb: getDb
}
user.model.js
const bcrypt = require('bcryptjs');
const db = require('../data/database');
class User {
constructor (email, password, fullname, street, postal, city) {
this.email = email;
this.password = password;
this.name = fullname;
this.address = {
street: street,
postalcode: postal,
city: city
};
}
async signup() {
const hashedPassword = await bcrypt.hash(this.password, 12); // crypting the password for safety!
await db.getDb().collection('users').insertOne({
email: this.email,
password: hashedPassword,
name: this.name,
address: this.address
});
}
}
module.exports = User;
signup.js
<%- include('../includes/head', { pageTitle: 'Signup' }) %>
<link rel="stylesheet" href="/styles/forms.css">
<link rel="stylesheet" href="/styles/auth.css">
</head>
<body>
<%- include('../includes/header') %>
<main>
<h1>Create New Account</h1>
<form action="/signup" method="post">
<p>
<label for="email">Email</label>
<input type="email" id="email" name="email" required>
</p>
<p>
<label for="confirm-email">Confirm Email</label>
<input type="email" id="confirm-email" name="confirm-email" required>
</p>
<p>
<label for="Password">Password</label>
<input type="Password" id="Password" name="Password" minlength="6" required>
</p>
<hr>
<p>
<label for="fullname">Full Name</label>
<input type="text" id="fullname" name="fullname" required>
</p>
<p>
<label for="street">Street</label>
<input type="text" id="street" name="street" required>
</p>
<p>
<label for="postal">Postal Code</label>
<input type="text"
id="postal"
name="postal"
minlength="6"
maxlength="6"
required>
</p>
<p>
<label for="city">City</label>
<input type="text" id="city" name="city" required>
</p>
<button class="btn">Create Account</button>
<p>
<a id="switch-form" href="/login">Login instead</a>
</p>
</form>
</main>
<%- include('../includes/footer') %>
app.js
const path = require("path");
const express = require("express");
const db = require("./data/database");
const authRoutes = require("./routes/auth.routes");
const app = express();
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "views"));
app.use(express.static("public"));
app.use(express.urlencoded({ extended: false }));
app.use(authRoutes);
db.connectToDatabase()
.then(function () {
app.listen(3000);
})
.catch(function (error) {
console.log("Failed to connect to the database!");
console.log(error);
});
Vishnu Vardhan Vankayalapati is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.