spee.ch/passport/local-signup.js

60 lines
2.3 KiB
JavaScript
Raw Normal View History

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');
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) => {
logger.debug('new channel signup request');
const address = config.get('WalletConfig.LbryClaimAddress');
let user;
let certificate;
// 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
return lbryApi.createChannel(username)
.then(channelTx => {
// create user record
const userData = {
channelName : username,
channelClaimId: channelTx.claim_id,
password : password,
address,
};
logger.debug('userData >', userData);
// create certificate record
const certificateData = {
address,
claimId: channelTx.claim_id,
name : username,
};
logger.debug('certificateData >', certificateData);
// save user and certificate to db
return Promise.all([db.User.create(userData), db.Certificate.create(certificateData)]);
})
.then(result => {
user = result[0];
certificate = result[1];
logger.debug('user and certificate successfully created');
2017-09-21 09:18:34 +02:00
logger.debug('user result >', user.dataValues);
logger.debug('certificate result >', certificate.dataValues);
// associate the instances
return Promise.all([certificate.setUser(user), user.setCertificate(certificate)]);
}).then(result => {
logger.debug('user and certificate successfully associated');
2017-09-21 09:18:34 +02:00
return done(null, user);
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
);