fixed view for Channel and User tables

This commit is contained in:
bill bittner 2017-09-25 22:49:27 -07:00
parent a2b3ed1ab2
commit 84c525dcc0
9 changed files with 25 additions and 23 deletions

View file

@ -122,7 +122,7 @@ module.exports = {
});
});
},
createChannel (name, claimAddress) {
createChannel (name) {
return new Promise((resolve, reject) => {
axios
.post('http://localhost:5279/lbryapi', {

View file

@ -17,7 +17,7 @@ module.exports = (sequelize, { STRING }) => {
);
User.associate = db => {
User.hasOne(db.Certificate);
User.hasOne(db.Channel);
};
User.prototype.validPassword = (givenpassword, thispassword) => {

View file

@ -24,8 +24,6 @@ module.exports = new PassportLocalStrategy(
}
logger.debug('user found:', user.dataValues);
return user.getChannel().then(channel => {
user['channelName'] = channel.channelClaimId;
user['channelClaimId'] = channel.channelClaimId;
return done(null, user);
});
})

View file

@ -2,7 +2,6 @@ const db = require('../models');
const PassportLocalStrategy = require('passport-local').Strategy;
const lbryApi = require('../helpers/lbryApi.js');
const logger = require('winston');
const config = require('config');
module.exports = new PassportLocalStrategy(
{
@ -12,13 +11,12 @@ module.exports = new PassportLocalStrategy(
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');
logger.debug(`new channel signup request: ${username} ${password}`);
let user;
// server-side validaton of inputs (username, password)
// create the channel and retrieve the metadata
return lbryApi.createChannel(username, address)
return lbryApi.createChannel(`@${username}`)
.then(tx => {
// create user record
const userData = {
@ -35,7 +33,7 @@ module.exports = new PassportLocalStrategy(
// create certificate record
const certificateData = {
claimId: tx.claim_id,
name : username,
name : `@${username}`,
// address,
};
logger.debug('certificateData >', certificateData);
@ -43,9 +41,7 @@ module.exports = new PassportLocalStrategy(
return Promise.all([db.User.create(userData), db.Channel.create(channelData), db.Certificate.create(certificateData)]);
})
.then(([newUser, newChannel, newCertificate]) => {
user = newUser; // save outside scope of this function
user['channelName'] = newChannel.channelClaimId;
user['channelClaimId'] = newChannel.channelClaimId;
user = newUser;
logger.debug('user and certificate successfully created');
logger.debug('user result >', newUser.dataValues);
logger.debug('user result >', newChannel.dataValues);
@ -54,11 +50,10 @@ module.exports = new PassportLocalStrategy(
return Promise.all([newCertificate.setChannel(newChannel), newChannel.setUser(newUser)]);
}).then(() => {
logger.debug('user and certificate successfully associated');
logger.debug('user ===', user.dataValues);
return done(null, user);
})
.catch(error => {
logger.debug(error);
logger.error('signup error', error);
return done(error);
});
}

View file

@ -45,10 +45,18 @@ app.use(passport.session());
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser((id, done) => {
passport.deserializeUser((id, done) => { // this populates req.user
db.User.findOne({ where: { id } })
.then(user => {
done(null, user);
user.getChannel().then(channel => {
let userInfo = {};
userInfo['id'] = user.id;
userInfo['userName'] = user.userName;
userInfo['channelName'] = channel.channelName;
userInfo['channelClaimId'] = channel.channelClaimId;
done(null, userInfo);
});
// done(null, user);
return null;
})
.then()
@ -74,6 +82,7 @@ app.set('view engine', 'handlebars');
// middleware to pass user info back to client (for handlebars access), if user is logged in
app.use((req, res, next) => {
if (req.user) {
logger.verbose(req.user);
res.locals.user = {
id : req.user.id,
userName : req.user.userName,

View file

@ -19,7 +19,7 @@
<script type="text/javascript">
function publishNewChannel (event) {
const channelName = `@${document.getElementById('new-channel-name').value}`;
const channelName = document.getElementById('new-channel-name').value;
const password = document.getElementById('new-channel-password').value;
const channelNameErrorDisplayElement = document.getElementById('input-error-channel-name');
const passwordErrorDisplayElement = document.getElementById('input-error-channel-password');

View file

@ -16,7 +16,7 @@
<script type="text/javascript">
function loginToChannel (event) {
const channelName = `@${document.getElementById('login-channel-name').value}`;
const channelName = document.getElementById('login-channel-name').value;
const password = document.getElementById('login-channel-password').value;
const loginErrorDisplayElement = document.getElementById('login-error-display-element');
// prevent default

View file

@ -4,7 +4,7 @@
<label for="channel-name-select">Channel:</label>
<select type="text" id="channel-name-select" class="select select--primary" value="channel" onclick="toggleChannel(event)">
{{#if user}}
<option value="{{user.channelName}}" >{{user.channelName}}</option>
<option value="@{{user.userName}}" >@{{user.userName}}</option>
{{/if}}
<option value="none" >None</option>
<option value="login">Login</option>

View file

@ -6,7 +6,7 @@
<a href="/about" class="top-bar-right">help</a>
{{#if user}}
<a href="/{{user.channelName}}:{{user.channelClaimId}}" class="top-bar-right">{{user.channelName}}</a>
<a href="/{{user.channelName}}:{{user.channelClaimId}}" class="top-bar-right">@{{user.userName}}</a>
<a href="/logout" class="top-bar-right">logout</a>
{{else}}
<a href="/login" class="top-bar-right">login</a>