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-20 03:50:25 +02:00
|
|
|
const config = require('config');
|
2017-09-17 02:50:22 +02:00
|
|
|
|
|
|
|
module.exports = new PassportLocalStrategy(
|
|
|
|
{
|
2017-09-18 19:14:06 +02:00
|
|
|
usernameField : 'username', // sets the custom name of parameters in the POST body message
|
2017-09-17 02:50:22 +02:00
|
|
|
passwordField : 'password', // sets the custom name of parameters in the POST body message
|
|
|
|
session : false, // set to false because we will use token approach to auth
|
|
|
|
passReqToCallback: true, // we want to be able to read the post body message parameters in the callback
|
|
|
|
},
|
|
|
|
(req, username, password, done) => {
|
2017-09-20 03:50:25 +02:00
|
|
|
logger.debug('new channel signup request');
|
|
|
|
const address = config.get('WalletConfig.LbryClaimAddress');
|
|
|
|
// validate raw inputs (username, password)
|
|
|
|
username = '@' + username;
|
2017-09-17 02:50:22 +02:00
|
|
|
// create the channel and retrieve the metadata
|
2017-09-20 03:50:25 +02:00
|
|
|
return lbryApi.createChannel(username)
|
|
|
|
.then(channelTx => {
|
|
|
|
// create certificate record
|
|
|
|
const certificateData = {
|
|
|
|
address,
|
|
|
|
claimId: channelTx.claim_id,
|
|
|
|
name : username,
|
|
|
|
};
|
|
|
|
logger.debug('certificateData >', certificateData);
|
|
|
|
return db.Certificate.create(certificateData);
|
|
|
|
})
|
|
|
|
.then(certificate => {
|
|
|
|
logger.debug('certificate result >', certificate.dataValues);
|
|
|
|
logger.debug('Certificate record was created successfully');
|
|
|
|
// define an object that contains all the user data
|
2017-09-17 02:50:22 +02:00
|
|
|
const userData = {
|
2017-09-18 19:14:06 +02:00
|
|
|
channelName : username,
|
2017-09-20 03:50:25 +02:00
|
|
|
channelClaimId: certificate.claimId,
|
2017-09-18 19:14:06 +02:00
|
|
|
password : password,
|
2017-09-20 03:50:25 +02:00
|
|
|
address,
|
2017-09-17 02:50:22 +02:00
|
|
|
};
|
|
|
|
return db.User.create(userData);
|
2017-09-20 03:50:25 +02:00
|
|
|
}).then(user => {
|
2017-09-18 19:14:06 +02:00
|
|
|
logger.debug('User record was created successfully');
|
2017-09-19 17:47:24 +02:00
|
|
|
return done(null, user); // user.datavalues?
|
2017-09-17 02:50:22 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-09-18 19:14:06 +02:00
|
|
|
logger.debug(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
|
|
|
);
|