Reputation: 1164
I have an express app and I'm trying to add users with passport local mongoose, but whenever I try to create a user, I get an error saying MissingUsernameError: No username was given
.
Here's the relevant code:
User Schema:
var mongoose = require('mongoose');
var passportLocalMongoose = require('passport-local-mongoose');
var UserSchema = new mongoose.Schema({
username: String,
password: String,
});
UserSchema.plugin(passportLocalMongoose);
module.exports = mongoose.model("User", UserSchema);
register.ejs:
<div class="field">
<div class="two fields">
<div class="field">
<input type="text" name="email" value="<%= email %>">
</div>
<div class="field">
<input type="password" name="password" placeholder="password">
</div>
</div>
</div>
app.js:
var mongoose = require('mongoose'),
passport = require('passport'),
LocalStrategy = require('passport-local'),
User = require('./models/user'),
app.use(require('express-session')({
secret: 'very secret words',
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
//New user creation
app.post('/newuser', function(req, res){
var newUser = new User({username: req.body.email});
console.log(newUser);
User.register(newUser, req.body.password, function(err, user){
if(err){
console.log('error registering user');
console.log(err);
return res.send('oops');
}
passport.authenticate('local')(req, res, function(){
res.redirect('/matches');
});
});
});
can anyone see what I'm missing? Thank you! :)
Upvotes: 0
Views: 9601
Reputation: 1
while creating a new user does not require creating a variable newUser you can directly do this
//New user creation
app.post('/newuser', function(req, res){
User.register({username: req.body.email}, req.body.password, function(err, user){
if(err){
console.log('error registering user');
console.log(err);
return res.send('oops');
}
passport.authenticate('local')(req, res, function(){
res.redirect('/matches');
});
});
});
Upvotes: 0
Reputation: 21
Change your schema from a normal javascript object to a new "mongoose.Schema()".
const userSchema = new mongoose.Schema({
email: String,
password: String
});
And make sure body-parser is being used before using any variables specified inside the HTML body. A good practice is to put it when we initialize app :
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));enter code here
It might help changing name in the HTML forms but in my case, it worked normally without changing any part in HTML or fields inside the schema
Upvotes: 0
Reputation: 50
I found the solution.
Change your schema structure like this:
const UserSchema = new mongoose.Schema({
username: String,
password: String
})
and then also change your name attributes in <form>
tag:
<input name="username" />
Upvotes: 0
Reputation: 39
Make sure to add the body parser before your routes.
I had the same problem as I was missing
app.use(bodyParser.urlencoded({extended : false}));
Upvotes: 0
Reputation: 197
change the input "name" attribute value from name="email" to name="username" that could fix it
Upvotes: 0
Reputation: 1164
OK so I found the solution to my problem–in case anyone else gets here with the same issue.
I had enctype="multipart/form-data"
in the signup form. Removing this fixes the problem. I don't understand why though, so if anyone can shed some light on that I'd be very grateful.
Also, I can see another problem on the horizon; I want this form to not only register the user's username and password, but also to add info about them to the database, including photos. As far as I understand, if I want to upload files from a form I need enctype="multipart/form-data"
. Does anyone know a solution for this? (Should this be a separate question?)
Upvotes: 1
Reputation: 6242
First of all, make sure to give body-parser
code before all the routes
app.use(express.bodyParser());
After that check that you have given both fields like username
and password
in form during request since passport.authenticate('local')
check these values and if anyone is mismatched then it won't gonna work.
Refer the Passport.js doc for more details
http://passportjs.org/docs/username-password
Update
var User = require('./models/user');
// CHANGE: USE "createStrategy" INSTEAD OF "authenticate"
passport.use(User.createStrategy());
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
The reason for this functionality is that when using the usernameField option to specify an alternative usernameField name, for example "email" passport-local would still expect your frontend login form to contain an input field with name
"username" instead of email
. This can be configured for passport-local but this is double the work. So we got this shortcut implemented.
See the https://github.com/saintedlama/passport-local-mongoose
Upvotes: 2