So when I am using passport.js for authentication while creating a new user, it give me this error
*ctor [MissingUsernameError]: No username was given*
here is my code
app.js
import 'dotenv/config'
import mongoose from "mongoose";
import express from "express";
import bodyParser from "body-parser";
import ejs from "ejs";
import session from 'express-session';
import passport from 'passport';
import passportLocalMongoose from 'passport-local-mongoose';
const port = 3000;
const app = express();
app.set('view engine', 'ejs')
app.use(bodyParser.urlencoded({extended:true}))
app.use(express.static("public"));
app.use(session({
secret: "Our Little Secret.",
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
mongoose.connect("mongodb://127.0.0.1:27017/usersDB");
const userSchema = new mongoose.Schema ({
email:{
type: String,
required: true
},
password: {
type: String,
required: true
}
});
userSchema.plugin(passportLocalMongoose);
const User = new mongoose.model("User", userSchema);
passport.use(User.createStrategy());
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.get("/", (req,res)=>{
res.render("home");
})
app.route("/login")
.get((req,res)=>{
res.render("login");
})
.post((req,res)=>{
const user = new User ({
email: req.body.username,
password: req.body.password
})
req.login(user, (err)=>{
if(err){
console.log(err)
}else{
passport.authenticate("local")(req,res, ()=>{
res.redirect("/secrets")
})
}
})
})
app.get("/register", (req,res)=>{
res.render("register");
})
app.get("/logout", (req,res)=>{
req.logout();
res.redirect("/");
})
app.get("/secrets", (req, res)=>{
if(req.isAuthenticated()){
res.render("secrets");
}else{
res.redirect("/login");
}
})
app.post("/register", (req,res)=>{
User.register({email: req.body.username, active: false}, req.body.password, (err, user)=> {
if (err) {
console.log(err);
res.redirect("/register");
} else {
passport.authenticate("local")(req,res, ()=>{
res.redirect("/secrets")
})
}
});
})
app.listen(port, ()=>{
console.log(`Your server is live on port ${port}`)
})
register.ejs
<%- include('partials/header') %>
<div class="container mt-5">
<h1>Register</h1>
<div class="row">
<div class="col-sm-8">
<div class="card">
<div class="card-body">
<!-- Makes POST request to /register route -->
<form action="/register" method="POST">
<div class="form-group">
<label for="email">Email</label>
<input type="email" class="form-control" name="username">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" name="password">
</div>
<button type="submit" class="btn btn-dark">Register</button>
<% if(locals.alert) { %>
<p style="color: red; font-weight: bold;"><%= alert %></p>
<% } %>
</form>
</div>
</div>
</div>
<!-- <div class="col-sm-4">
<div class="card social-block">
<div class="card-body">
<a class="btn btn-block" href="/auth/google" role="button">
<i class="fab fa-google"></i>
Sign Up with Google
</a>
</div>
</div>
</div> -->
</div>
</div>
<%- include('partials/header') %>
I tried to hit the post route on “/register” but instead of a new user being created it gave me the following error
ctor [MissingUsernameError]: No username was given
it might be a silly question but hopefully I would find an solution for this as I am new to passport.js and would love some help form the stackoverflow community