fixed db requirements in passport files

This commit is contained in:
bill bittner 2018-03-09 18:39:32 -08:00
parent 532702c095
commit 00a3e9be23
6 changed files with 124 additions and 126 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -1,66 +1,65 @@
const PassportLocalStrategy = require('passport-local').Strategy; const PassportLocalStrategy = require('passport-local').Strategy;
const logger = require('winston'); const logger = require('winston');
const db = require('../models');
module.exports = (db) => { const returnUserAndChannelInfo = (userInstance) => {
const returnUserAndChannelInfo = (userInstance) => { return new Promise((resolve, reject) => {
return new Promise((resolve, reject) => { let userInfo = {};
let userInfo = {}; userInfo['id'] = userInstance.id;
userInfo['id'] = userInstance.id; userInfo['userName'] = userInstance.userName;
userInfo['userName'] = userInstance.userName; userInstance
userInstance .getChannel()
.getChannel() .then(({channelName, channelClaimId}) => {
.then(({channelName, channelClaimId}) => { userInfo['channelName'] = channelName;
userInfo['channelName'] = channelName; userInfo['channelClaimId'] = channelClaimId;
userInfo['channelClaimId'] = channelClaimId; return db.Certificate.getShortChannelIdFromLongChannelId(channelClaimId, channelName);
return db.Certificate.getShortChannelIdFromLongChannelId(channelClaimId, channelName); })
}) .then(shortChannelId => {
.then(shortChannelId => { userInfo['shortChannelId'] = shortChannelId;
userInfo['shortChannelId'] = shortChannelId; resolve(userInfo);
resolve(userInfo); })
}) .catch(error => {
.catch(error => { reject(error);
reject(error); });
}); });
});
};
return new PassportLocalStrategy(
{
usernameField: 'username',
passwordField: 'password',
},
(username, password, done) => {
return db.User
.findOne({
where: {userName: username},
})
.then(user => {
if (!user) {
logger.debug('no user found');
return done(null, false, {message: 'Incorrect username or password'});
}
return user.comparePassword(password)
.then(isMatch => {
if (!isMatch) {
logger.debug('incorrect password');
return done(null, false, {message: 'Incorrect username or password'});
}
logger.debug('Password was a match, returning User');
return returnUserAndChannelInfo(user)
.then(userInfo => {
return done(null, userInfo);
})
.catch(error => {
return error;
});
})
.catch(error => {
return error;
});
})
.catch(error => {
return done(error);
});
},
);
}; };
module.exports = new PassportLocalStrategy(
{
usernameField: 'username',
passwordField: 'password',
},
(username, password, done) => {
return db.User
.findOne({
where: {userName: username},
})
.then(user => {
if (!user) {
logger.debug('no user found');
return done(null, false, {message: 'Incorrect username or password'});
}
return user.comparePassword(password)
.then(isMatch => {
if (!isMatch) {
logger.debug('incorrect password');
return done(null, false, {message: 'Incorrect username or password'});
}
logger.debug('Password was a match, returning User');
return returnUserAndChannelInfo(user)
.then(userInfo => {
return done(null, userInfo);
})
.catch(error => {
return error;
});
})
.catch(error => {
return error;
});
})
.catch(error => {
return done(error);
});
},
);

View file

@ -1,65 +1,64 @@
const PassportLocalStrategy = require('passport-local').Strategy; const PassportLocalStrategy = require('passport-local').Strategy;
const lbryApi = require('../helpers/lbryApi.js'); const lbryApi = require('../helpers/lbryApi.js');
const logger = require('winston'); const logger = require('winston');
const db = require('../models');
module.exports = (db) => { module.exports = new PassportLocalStrategy(
return new PassportLocalStrategy( {
{ usernameField: 'username',
usernameField: 'username', passwordField: 'password',
passwordField: 'password', },
}, (username, password, done) => {
(username, password, done) => { logger.verbose(`new channel signup request. user: ${username} pass: ${password} .`);
logger.verbose(`new channel signup request. user: ${username} pass: ${password} .`); let userInfo = {};
let userInfo = {}; // server-side validaton of inputs (username, password)
// server-side validaton of inputs (username, password)
// create the channel and retrieve the metadata // create the channel and retrieve the metadata
return lbryApi.createChannel(`@${username}`) return lbryApi.createChannel(`@${username}`)
.then(tx => { .then(tx => {
// create user record // create user record
const userData = { const userData = {
userName: username, userName: username,
password: password, password: password,
}; };
logger.verbose('userData >', userData); logger.verbose('userData >', userData);
// create user record // create user record
const channelData = { const channelData = {
channelName : `@${username}`, channelName : `@${username}`,
channelClaimId: tx.claim_id, channelClaimId: tx.claim_id,
}; };
logger.verbose('channelData >', channelData); logger.verbose('channelData >', channelData);
// create certificate record // create certificate record
const certificateData = { const certificateData = {
claimId: tx.claim_id, claimId: tx.claim_id,
name : `@${username}`, name : `@${username}`,
// address, // address,
}; };
logger.verbose('certificateData >', certificateData); logger.verbose('certificateData >', certificateData);
// save user and certificate to db // save user and certificate to db
return Promise.all([db.User.create(userData), db.Channel.create(channelData), db.Certificate.create(certificateData)]); return Promise.all([db.User.create(userData), db.Channel.create(channelData), db.Certificate.create(certificateData)]);
}) })
.then(([newUser, newChannel, newCertificate]) => { .then(([newUser, newChannel, newCertificate]) => {
logger.verbose('user and certificate successfully created'); logger.verbose('user and certificate successfully created');
// store the relevant newUser info to be passed back for req.User // store the relevant newUser info to be passed back for req.User
userInfo['id'] = newUser.id; userInfo['id'] = newUser.id;
userInfo['userName'] = newUser.userName; userInfo['userName'] = newUser.userName;
userInfo['channelName'] = newChannel.channelName; userInfo['channelName'] = newChannel.channelName;
userInfo['channelClaimId'] = newChannel.channelClaimId; userInfo['channelClaimId'] = newChannel.channelClaimId;
// associate the instances // associate the instances
return Promise.all([newCertificate.setChannel(newChannel), newChannel.setUser(newUser)]); return Promise.all([newCertificate.setChannel(newChannel), newChannel.setUser(newUser)]);
}) })
.then(() => { .then(() => {
logger.verbose('user and certificate successfully associated'); logger.verbose('user and certificate successfully associated');
return db.Certificate.getShortChannelIdFromLongChannelId(userInfo.channelClaimId, userInfo.channelName); return db.Certificate.getShortChannelIdFromLongChannelId(userInfo.channelClaimId, userInfo.channelName);
}) })
.then(shortChannelId => { .then(shortChannelId => {
userInfo['shortChannelId'] = shortChannelId; userInfo['shortChannelId'] = shortChannelId;
return done(null, userInfo); return done(null, userInfo);
}) })
.catch(error => { .catch(error => {
logger.error('signup error', error); logger.error('signup error', error);
return done(error); return done(error);
}); });
} }
); );
};

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long