Trying to create new user - node.js

Trying to update steam user to User schema if exist, otherwise create new steam user in mongodb. However I got the error below. How can I solve this, am I doing it wrongly?
ValidationError: User validation failed: imageURL: Path `imageURL` is required., username: Path
`username` is required., profileURL: Path `profileURL` is required.
const User = require('./models/user');
User.findOne({ steamId: steamId}, function(err, user){
if(user){
return done(null, user);
}
else{
request(profileURL, function (error, response, body){
if (!error && response.statusCode === 200) {
var data = JSON.parse(body);
var profile = data.response.players[0];
var user = new User();
user.username = profile.personame;
user.profileURL = profile.profileURL;
user.profileImageURL = profile.avatarmedium;
user.steamId = steamId;
user.save(function(err){
done(err, user);
});
}
else{
done(err, null);
}
});
}
});

Removed require: true inside my schema

Related

Update existing user data in database

Currently, I have managed to the save user information inside my database. However I wanted to update the user information inside my database when they logged in if there is changes of information inside Steam database. So it is the same inside my database.
Below are example of information inside my User schema
const UserSchema = mongoose.Schema({
username:{
type: String,
},
profileURL:{
type: String,
},
profileImageURL:{
type: String,
},
steamId:{
type: String,
}
});
Below are example of my app.js. When the user login, I checked if the user steamId exist inside my database, I want to update the user information such as username, profileURL, profileImageURL and its steamID if exist else I create a new user inside my database. How can I achieve this? Currently, I just return done(null, user).
passport.use(new SteamStrategy({
returnURL: 'http://localhost:3000/auth/steam/return',
realm: 'http://localhost:3000/',
apiKey: ''
},
function (identifier, done){
var steamId = identifier.match(/\d+$/)[0];
var profileURL = 'http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=' + 'api_Key' + '&steamids=' + steamId;
User.findOne({ steamId: steamId}, function(err, user){
if(user){
return done(null, user);
}
else{
request(profileURL, function (error, response, body){
if (!error && response.statusCode === 200)
{
var data = JSON.parse(body);
var profile = data.response.players[0];
var user = new User();
user.username = profile.personaname;
user.profileURL = profile.profileurl;
user.profileImageURL = profile.avatarfull;
user.steamId = steamId;
user.save(function(err){
done(err, user);
});
}
else
{
done(err, null);
}
});
}
});
}));
You could do this with an upsert-enabled update call. Try something like this:
request(profileURL, function(err, response, body){
var data = JSON.parse(body);
var user = {
//... construct user object
}
User.findOneAndUpdate({ steamId: steamId }, user, {upsert: true, new: true, setDefaultsOnInsert: true}, function(err, newUser){
if (err) return handleError(err);
done(null, newUser);
});
});

Stripe subscription not updating quantity

I have a fairly simple application that just allows you to create a single admin user that can create sub users under their account. When an admin creates another user I want them to update their subscription on stripe with their current number of users which is gets stored at user.company.subUserCount. But when I do this the user model is not updated with the correct value and stripe will not update at all.
Was hoping someone could take a look at my code snippets and see what is wrong with it that is causing it not to update Stripe.
Route
// POST USER/NEW
app.post('/user/new',
isAuthenticated,
company.postNewUserPlan,
sessions.postSignupSub,
(req, res) => {
User.findById(req.user.id, function(err, user) {
user.company.subUserCount = req.user.company.subUserCount + 1;
user.save();
});
});
company.postNewUserPlan
exports.postNewUserPlan = function(req, res, next){
var plan = req.user.company.stripe.plan;
var coupon = null;
var stripeToken = null;
plan = plan.toLowerCase();
if(req.body.stripeToken){
stripeToken = req.body.stripeToken;
}
User.findById(req.user.id, function(err, user) {
if (err) return next(err);
var quantity = user.company.subUserCount + 1;
user.setPlan(plan, coupon, quantity, stripeToken, function (err) {
var msg;
if (err) {
if(err.code && err.code == 'card_declined'){
msg = 'Your card was declined. Please provide a valid card.';
} else if(err && err.message) {
msg = err.message;
} else {
msg = 'An unexpected error occurred.';
}
req.flash('errors', msg);
return res.redirect('/user/create');
}
});
});
next();
};
sessions.postSignupSub passport code
passport.use('signup-sub', new LocalStrategy({
usernameField: 'email',
passReqToCallback : true
},
function(req, email, password, done) {
User.findOne({ email: req.body.email }, function(err, existingUser) {
if(err){
console.log(err);
}
if (existingUser) {
req.flash('form', {
email: req.body.email
});
return done(null, false, req.flash('error', 'An account with that email address already exists.'));
}
var preRole = req.body.role;
var role = ''
if (preRole === undefined) {
role = 'manager';
} else if (preRole === 'on') {
role = 'employee';
}
// edit this portion to accept other properties when creating a user.
var user = new User({
email: req.body.email,
password: req.body.password, // user schema pre save task hashes this password
role: role,
companyID: req.user.companyID,
isVerified: true
});
user.save(function(err) {
if (err) return done(err, false, req.flash('error', 'Error saving user.'));
var time = 14 * 24 * 3600000;
req.session.cookie.maxAge = time; //2 weeks
req.session.cookie.expires = new Date(Date.now() + time);
req.session.touch();
return done(null, user, req.flash('success', `Your new ${role} has been created`));
});
});
})
);
I managed to solve the issue it was related to code that was not included in the OP after a bit of testing I figured out the subscription was not being updated with a quantity but instead was only being created with it.
Code
schema.methods.setPlan = function(plan, coupon, quantity, stripe_token, cb) {
var user = this;
var subscriptionHandler = function(err, subscription) {
if(err) return cb(err);
user.company.stripe.plan = plan;
user.company.stripe.subscriptionId = subscription.id;
user.company.subUserCount = quantity;
user.save(function(err){
if (err) return cb(err);
return cb(null);
});
};
var createSubscription = function(){
stripe.customers.createSubscription(
user.company.stripe.customerId,
{plan: plan, coupon: coupon, quantity: quantity},
subscriptionHandler
);
};
if(stripe_token) {
user.setCard(stripe_token, function(err){
if (err) return cb(err);
createSubscription();
});
} else {
if (user.company.stripe.subscriptionId){
// update subscription
stripe.customers.updateSubscription(
user.company.stripe.customerId,
user.company.stripe.subscriptionId,
{ plan: plan, coupon: coupon, ***quantity: quantity*** }, <-- Part I had to change
subscriptionHandler
);
} else {
createSubscription();
}
}
};

Mongoose bcryptjs compare password doesn't refer to the document (this)

I have mongoose schema like so
var mongoose = require ('mongoose');
var bcrypt = require('bcryptjs');
var Schema = mongoose.Schema;
var SALT_WORK_FACTOR = 10;
var touristSchema = new Schema ({
local: {
email: String,
password: String
},
facebook: {
id: String,
token: String,
email: String,
name: String,
}
});
touristSchema.pre('save', function(next) {
var user = this;
console.log('bcrypt called by strategy', user);
// if user is facebook user skip the pasword thing.
if (user.facebook.token) {
next();
}
// only hash the password if it has been modified (or is new)
if (!user.isModified('password') && !user.isNew){
console.log('I am in here', user.isNew);
return next();
}
// generate a salt
bcrypt.genSalt(SALT_WORK_FACTOR, function(err, salt) {
console.log('I am in genSalt');
if (err) return next(err);
// hash the password using our new salt
bcrypt.hash(user.local.password, salt, function(err, hash) {
if (err) return next(err);
// override the cleartext password with the hashed one
user.local.password = hash;
next();
});
});
});
touristSchema.methods.comparePassword = function(candidatePassword, cb) {
bcrypt.compare(candidatePassword, this.local.password, function(err, isMatch) {
// console.log(this.local.password);
if (err) return cb(err);
cb(null, isMatch);
});
};
module.exports = mongoose.model('users', touristSchema);
and login strategy like this
passport.use('local-login', new LocalStrategy({
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true
},
function(req, email, password, done) {
process.nextTick(function() {
User.findOne({ 'local.email': email }, function(err, user) {
if(err)
return done(err);
if(!user)
return done(null, false, req.flash('loginMessage', 'No User Found'));
user.comparePassword(password, function(err, isMatch) {
if (err) throw err;
if (isMatch) {
done(null, user);
}
else
done(null, false, req.flash('loginMessage', 'Incorrect password'));
});
});
});
}
));
everything is working as expected but when I try to log in it gives me error:
TypeError: Cannot read property 'password' of undefined.
Turns out that this.local is undefined.
This is very strange as in touristSchema.pre hook we assigned var user = this and on logging this returned user document. When in compare method we are using touristSchema.methods.compare then this should must refer to the document as written in mongoose middleware docs too. I am beating my head over it for a couple of days now and have consumed every available help I could. Any help is greatly appreciated in advance. Thanks
and yes my
mongnodb version is v3.2.15
mongoose version is 4.9.7
which looks compatible to me according to mongoose docs

How update the password in mongodb

app.get('/changePass', function(req, res) {
password = req.body.password;
password1 = req.body.password1;
xyz = req.body.xyz;
User.find({ email: xyz, password: password }, function(err, data) {
if (err) {
throw err;
}
if (data.length === 0) {
return res.json({ success: false, data: 'please enter valid
Old password' })
} else {
// User.update({ password: password }, {
// $set: {
// password: password1
// }
// });
var usr = new User();
usr.update({ "password":password }, password1, { upsert:true
});
//usr.password = password;
usr.save(function(err, data) {
if (err)
throw err;
return res.json({ success: true, data: 'password changed
successfully' });
})
}
})
how i update password in mongodb i am matching password to password and updating password to password1.this is giving me alert with please enter valid old password.
xyz is cookie that contains email.
You should use findOne on model to return userObject.
Then you can update the object that was found like regular javascript object and save it with .save function.
User.findOne({email: xyz, password: req.body.password1}, function(err, user){
if(err)return handleErr(err);
user.password = req.body.password2;
user.save(function(err){
if(err)return handleErr(err);
//user has been updated
});
});

Inserting an array in mongodb using mongoose in a schema where other fields are already added from user registration

I am creating an application for online course.
I have created a schema for user registration. In the schema, I also want to add the name of courses a user in enrolled. Course Name being an array.
User registration is successful. after that I have created a route for /dashboard, where the user sends the POST request to add the course name. That course should be added in the same registration schema field for course Enrolled. However When I save a new object of registration schema, it creates a new document field courseEnrolled field. I want this POST request value to be added in the user's document field of courseEnrolled as an array.
Here is how I have defined my registration schema. Its name in account.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var passportLocalMongoose = require('passport-local-mongoose');
var courseSchema = new Schema({ courseName : String });
var Account = new Schema({
username: {
type: String,
unique: true
},
password: String,
email: String,
firstName: String,
lastName: String,
courseEnrolled: [{courseName : String}]
});
Account.plugin(passportLocalMongoose);
module.exports = mongoose.model('Account', Account);
Here is my passport registration . register.js
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var User = require('../models/account');
var bCrypt = require('bcrypt-nodejs');
var course = require('../models/courseEnrollment');
module.exports = function(passport){
passport.use('register', new LocalStrategy({
passReqToCallback : true // allows us to pass back the entire request to the callback
},
function(req, username, password, done) {
findOrCreateUser = function(){
// find a user in Mongo with provided username
User.findOne({ 'username' : username }, function(err, user) {
// In case of any error, return using the done method
if (err){
console.log('Error in SignUp: '+err);
return done(err);
}
// already exists
if (user) {
console.log('User already exists with username: '+username);
return done(null, false, req.flash('message','User Already Exists'));
} else {
// if there is no user with that email
// create the user
var newUser = new User();
var newCourse = new course();
// set the user's local credentials
newUser.username = username;
newUser.password = createHash(password);
newUser.email = req.body.email;
newUser.firstName = req.body.firstName;
newUser.lastName = req.body.lastName;
newUser.courseEnrolled = req.body.courseEnrolled;
// save the user
newUser.save(function(err) {
if (err){
console.log('Error in Saving user: '+err);
throw err;
}
console.log('User Registration succesful');
return done(null, newUser);
});
}
});
};
// Delay the execution of findOrCreateUser and execute the method
// in the next tick of the event loop
process.nextTick(findOrCreateUser);
})
);
// Generates hash using bCrypt
var createHash = function(password){
return bCrypt.hashSync(password, bCrypt.genSaltSync(10), null);
}
}
I can register a user successfully. After that I have a route for /dashboard, where I handle the POST request to add a course.
Here is the snippet of my /dashboard handling POST request.
var User = require('../models/account');
/* POST dashboard Page */
router.post('/dashboard', isAuthenticated, function (req, res) {
sess = req.session.passport.user;
console.log('session value is: ' + sess);
var newUser = new User();
console.log('newUser id is: ' + newUser._id);
var currentUser = req.user._id;
console.log('current User id is: ' + currentUser);
var myUser = req.user;
console.log('myUsers value is: ' + myUser);
var myUserCourse = req.user.courseEnrolled;
if (sess == currentUser) {
//var newCourse = new course();
console.log('request received: ' + req.body.courseEnrolled);
req.user.courseEnrolled = req.body.courseEnrolled;
newUser.save(function (err, data) {
if(error)
throw error;
else {
console.log('course Updated');
}
});
res.render('home', {user: req.user});
}
});
This newUser.save() function creates a new document in the mongodb and store the courseEnrolled. I want to store the value of req.body.courseEnrolled in the same document field where other user value is defined.
This is getting stored in collection:- 'accounts' for the user
{
"_id" : ObjectId("57f95afd9c78b91c69334f0d"),
"lastName" : "Nehra",
"firstName" : "Ashish",
"email" : "ashish.nehra#stanford.edu",
"password" : "$2a$10$YzLvbQTHFtq5l0ooP0njOux94Rp.pm.Pkb/TugBnCSTUJNhBBonLG",
"username" : "ashish",
"courseEnrolled" : [
"about to change something now"
],
"__v" : 1
}
And there is a new document being created like this in the same collection.
{
"_id" : ObjectId("5803fc4342ca1d3167102300"),
"courseEnrolled" : [ ],
"__v" : 0
}
This is logical because first you do a it on various user objects (req.user / new user):
**var newUser = new User();
This will create a new User object, and then:
newUser.save(function (err, data) {
This will save the newly created user into a new document. If you want to use the .save, rewrite it to (reused your own code):
User.findOne({ 'username' : username }, function(err, user) {
// In case of any error, return using the done method
if (err){
console.log('Error in SignUp: '+err);
return done(err);
}
user.courseEnrolled = req.body.courseEnrolled;
user.save(function (err, data) {
if(err)
throw err;
else {
console.log('course Updated');
}
});

Resources