2017-09-18 19:14:06 +02:00
|
|
|
const db = require('../models');
|
2017-09-17 02:50:22 +02:00
|
|
|
const PassportLocalStrategy = require('passport-local').Strategy;
|
|
|
|
const lbryApi = require('../helpers/lbryApi.js');
|
2017-09-18 19:14:06 +02:00
|
|
|
const logger = require('winston');
|
2017-09-17 02:50:22 +02:00
|
|
|
|
|
|
|
module.exports = new PassportLocalStrategy(
|
|
|
|
{
|
2017-12-01 00:13:17 +01:00
|
|
|
usernameField: 'username',
|
|
|
|
passwordField: 'password',
|
2017-09-17 02:50:22 +02:00
|
|
|
},
|
2017-12-01 00:13:17 +01:00
|
|
|
(username, password, done) => {
|
2017-10-17 21:05:22 +02:00
|
|
|
logger.verbose(`new channel signup request. user: ${username} pass: ${password} .`);
|
2017-10-11 02:46:26 +02:00
|
|
|
let userInfo = {};
|
2017-09-21 20:00:06 +02:00
|
|
|
// server-side validaton of inputs (username, password)
|
2017-09-20 19:14:00 +02:00
|
|
|
|
2017-09-17 02:50:22 +02:00
|
|
|
// create the channel and retrieve the metadata
|
2017-09-26 07:49:27 +02:00
|
|
|
return lbryApi.createChannel(`@${username}`)
|
2017-09-21 21:05:04 +02:00
|
|
|
.then(tx => {
|
2017-09-21 20:00:06 +02:00
|
|
|
// create user record
|
|
|
|
const userData = {
|
2017-09-26 06:03:43 +02:00
|
|
|
userName: username,
|
|
|
|
password: password,
|
2017-09-21 20:00:06 +02:00
|
|
|
};
|
2017-10-17 21:05:22 +02:00
|
|
|
logger.verbose('userData >', userData);
|
2017-09-26 06:03:43 +02:00
|
|
|
// create user record
|
|
|
|
const channelData = {
|
|
|
|
channelName : `@${username}`,
|
|
|
|
channelClaimId: tx.claim_id,
|
|
|
|
};
|
2017-10-17 21:05:22 +02:00
|
|
|
logger.verbose('channelData >', channelData);
|
2017-09-20 03:50:25 +02:00
|
|
|
// create certificate record
|
|
|
|
const certificateData = {
|
2017-09-21 21:05:04 +02:00
|
|
|
claimId: tx.claim_id,
|
2017-09-26 07:49:27 +02:00
|
|
|
name : `@${username}`,
|
2017-09-22 01:03:45 +02:00
|
|
|
// address,
|
2017-09-20 03:50:25 +02:00
|
|
|
};
|
2017-10-17 21:05:22 +02:00
|
|
|
logger.verbose('certificateData >', certificateData);
|
2017-09-21 20:00:06 +02:00
|
|
|
// save user and certificate to db
|
2017-09-26 06:03:43 +02:00
|
|
|
return Promise.all([db.User.create(userData), db.Channel.create(channelData), db.Certificate.create(certificateData)]);
|
2017-09-20 03:50:25 +02:00
|
|
|
})
|
2017-09-26 06:03:43 +02:00
|
|
|
.then(([newUser, newChannel, newCertificate]) => {
|
2017-10-17 21:05:22 +02:00
|
|
|
logger.verbose('user and certificate successfully created');
|
2017-09-25 20:55:56 +02:00
|
|
|
logger.debug('user result >', newUser.dataValues);
|
2017-10-11 02:46:26 +02:00
|
|
|
userInfo['id'] = newUser.id;
|
|
|
|
userInfo['userName'] = newUser.userName;
|
2017-10-17 21:05:22 +02:00
|
|
|
logger.verbose('channel result >', newChannel.dataValues);
|
2017-10-11 02:46:26 +02:00
|
|
|
userInfo['channelName'] = newChannel.channelName;
|
|
|
|
userInfo['channelClaimId'] = newChannel.channelClaimId;
|
2017-10-17 21:05:22 +02:00
|
|
|
logger.verbose('certificate result >', newCertificate.dataValues);
|
2017-09-21 20:00:06 +02:00
|
|
|
// associate the instances
|
2017-09-26 06:03:43 +02:00
|
|
|
return Promise.all([newCertificate.setChannel(newChannel), newChannel.setUser(newUser)]);
|
2017-10-11 02:46:26 +02:00
|
|
|
})
|
|
|
|
.then(() => {
|
2017-10-17 21:05:22 +02:00
|
|
|
logger.verbose('user and certificate successfully associated');
|
2017-10-31 18:05:15 +01:00
|
|
|
return db.Certificate.getShortChannelIdFromLongChannelId(userInfo.channelClaimId, userInfo.channelName);
|
2017-10-11 02:46:26 +02:00
|
|
|
})
|
|
|
|
.then(shortChannelId => {
|
|
|
|
userInfo['shortChannelId'] = shortChannelId;
|
|
|
|
return done(null, userInfo);
|
2017-09-17 02:50:22 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-09-26 07:49:27 +02:00
|
|
|
logger.error('signup error', error);
|
2017-09-17 02:50:22 +02:00
|
|
|
return done(error);
|
|
|
|
});
|
2017-09-18 19:14:06 +02:00
|
|
|
}
|
2017-09-17 02:50:22 +02:00
|
|
|
);
|