2017-09-17 02:50:22 +02:00
|
|
|
const PassportLocalStrategy = require('passport-local').Strategy;
|
2018-04-27 08:42:22 +02:00
|
|
|
const { createChannel } = require('../../lbrynet');
|
2017-09-18 19:14:06 +02:00
|
|
|
const logger = require('winston');
|
2018-04-27 08:42:22 +02:00
|
|
|
const db = require('../../models');
|
2019-05-24 22:39:46 +02:00
|
|
|
const {
|
|
|
|
publishing: { closedRegistration },
|
|
|
|
} = require('@config/siteConfig');
|
2017-09-17 02:50:22 +02:00
|
|
|
|
2018-03-10 03:39:32 +01:00
|
|
|
module.exports = new PassportLocalStrategy(
|
|
|
|
{
|
|
|
|
usernameField: 'username',
|
|
|
|
passwordField: 'password',
|
|
|
|
},
|
|
|
|
(username, password, done) => {
|
2018-09-19 21:14:20 +02:00
|
|
|
if (closedRegistration) {
|
|
|
|
return done('Registration is disabled');
|
|
|
|
}
|
|
|
|
|
2018-03-10 03:39:32 +01:00
|
|
|
logger.verbose(`new channel signup request. user: ${username} pass: ${password} .`);
|
|
|
|
let userInfo = {};
|
|
|
|
// server-side validaton of inputs (username, password)
|
|
|
|
// create the channel and retrieve the metadata
|
2018-04-27 08:42:22 +02:00
|
|
|
return createChannel(`@${username}`)
|
2018-03-10 03:39:32 +01:00
|
|
|
.then(tx => {
|
|
|
|
// create user record
|
|
|
|
const userData = {
|
|
|
|
userName: username,
|
|
|
|
password: password,
|
|
|
|
};
|
|
|
|
logger.verbose('userData >', userData);
|
|
|
|
// create user record
|
|
|
|
const channelData = {
|
2019-05-24 22:39:46 +02:00
|
|
|
channelName: `@${username}`,
|
|
|
|
channelClaimId: tx.outputs[0].claim_id,
|
2018-03-10 03:39:32 +01:00
|
|
|
};
|
|
|
|
logger.verbose('channelData >', channelData);
|
|
|
|
// create certificate record
|
|
|
|
const certificateData = {
|
2019-05-24 22:39:46 +02:00
|
|
|
claimId: tx.outputs[0].claim_id,
|
|
|
|
name: `@${username}`,
|
2018-03-10 03:39:32 +01:00
|
|
|
// address,
|
|
|
|
};
|
|
|
|
logger.verbose('certificateData >', certificateData);
|
|
|
|
// save user and certificate to db
|
2019-05-24 22:39:46 +02:00
|
|
|
return Promise.all([
|
|
|
|
db.User.create(userData),
|
|
|
|
db.Channel.create(channelData),
|
|
|
|
db.Certificate.create(certificateData),
|
|
|
|
]);
|
2018-03-10 03:39:32 +01:00
|
|
|
})
|
|
|
|
.then(([newUser, newChannel, newCertificate]) => {
|
|
|
|
logger.verbose('user and certificate successfully created');
|
|
|
|
// store the relevant newUser info to be passed back for req.User
|
|
|
|
userInfo['id'] = newUser.id;
|
|
|
|
userInfo['userName'] = newUser.userName;
|
|
|
|
userInfo['channelName'] = newChannel.channelName;
|
|
|
|
userInfo['channelClaimId'] = newChannel.channelClaimId;
|
|
|
|
// associate the instances
|
|
|
|
return Promise.all([newCertificate.setChannel(newChannel), newChannel.setUser(newUser)]);
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
logger.verbose('user and certificate successfully associated');
|
2019-05-24 22:39:46 +02:00
|
|
|
return db.Certificate.getShortChannelIdFromLongChannelId(
|
|
|
|
userInfo.channelClaimId,
|
|
|
|
userInfo.channelName
|
|
|
|
);
|
2018-03-10 03:39:32 +01:00
|
|
|
})
|
|
|
|
.then(shortChannelId => {
|
|
|
|
userInfo['shortChannelId'] = shortChannelId;
|
|
|
|
return done(null, userInfo);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
logger.error('signup error', error);
|
|
|
|
return done(error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|