Redesign 1 bcrypt #226

Merged
bones7242 merged 156 commits from redesign-1-bcrypt into master 2017-10-30 15:55:14 +01:00
8 changed files with 92 additions and 24 deletions
Showing only changes of commit 203d18e736 - Show all commits

View file

@ -11,6 +11,7 @@ module.exports = new PassportLocalStrategy(
},
(req, username, password, done) => {
logger.debug(`verifying loggin attempt ${username} ${password}`);
let userInfo = {};
return db.User
.findOne({where: {userName: username}})
.then(user => {
@ -23,9 +24,18 @@ module.exports = new PassportLocalStrategy(
return done(null, false, {message: 'Incorrect username or password.'});
}
logger.debug('user found:', user.dataValues);
return user.getChannel().then(channel => {
return done(null, user);
});
userInfo['id'] = user.id;
userInfo['userName'] = user.userName;
return user.getChannel();
})
.then(channel => {
userInfo['channelName'] = channel.channelName;
userInfo['channelClaimId'] = channel.channelClaimId;
return db.getShortChannelIdFromLongChannelId(channel.channelClaimId, channel.channelName);
})
.then(shortChannelId => {
userInfo['shortChannelId'] = shortChannelId;
return done(null, userInfo);
})
.catch(error => {
return done(error);

View file

@ -80,6 +80,31 @@ function createProgressBar(element, size){
setInterval(addOne, 300);
}
function getCookie(cname) {
const name = cname + "=";
const decodedCookie = decodeURIComponent(document.cookie);
const ca = decodedCookie.split(';');
for(let i = 0; i <ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function checkCookie() {
const channelName = getCookie("channel_name");
if (channelName != "") {
console.log(`cookie found for ${channelName}`);
} else {
console.log('no channel_name cookie found');
}
}
// Create new error objects, that prototypically inherit from the Error constructor
function FileError(message) {
this.name = 'FileError';

View file

@ -9,7 +9,13 @@ module.exports = (app) => {
});
// route for log in
app.post('/login', passport.authenticate('local-login'), (req, res) => {
logger.debug(req.user);
logger.debug('successful login');
res.status(200).json(true);
res.status(200).json({
success : true,
channelName : req.user.channelName,
channelClaimId: req.user.channelClaimId,
shortChannelId: req.user.shortChannelId,
});
});
};

View file

@ -61,7 +61,7 @@ passport.deserializeUser((id, done) => { // this populates req.user
.then(shortChannelId => {
userInfo['shortChannelId'] = shortChannelId;
done(null, userInfo);
return null;
return null; // note: why return null and not the done?
})
.catch(error => {
logger.error('sequelize error', error);
@ -85,7 +85,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);
// logger.verbose(req.user);
res.locals.user = {
id : req.user.id,
userName : req.user.userName,

View file

@ -61,6 +61,8 @@
<script src="/assets/js/publishFileFunctions.js"></script>
<script typ="text/javascript">
checkCookie();
var socket = io();
var uploader = new SocketIOFileUpload(socket);
var stagedFiles = null;
@ -68,12 +70,10 @@
/* drop zone functions */
function showInstructions () {
console.log('showing instructions');
document.getElementById('preview-dropzone-instructions').hidden = false;
document.getElementById('asset-preview').style.opacity = 0.3;
}
function hideInstructions () {
console.log('hiding instructions');
document.getElementById('preview-dropzone-instructions').hidden = true;
document.getElementById('asset-preview').style.opacity = 1;
}

View file

@ -25,20 +25,41 @@
<script type="text/javascript">
function loginToChannel (event) {
const channelName = document.getElementById('login-channel-name').value;
const userName = document.getElementById('login-channel-name').value;
const password = document.getElementById('login-channel-password').value;
const loginErrorDisplayElement = document.getElementById('login-error-display-element');
// prevent default
event.preventDefault()
// send request
sendAuthRequest(channelName, password, '/login')
.then(() => {
console.log('login success');
window.location.href = '/';
const channelLoginForm = document.getElementById('channel-login-form');
sendAuthRequest(userName, password, '/login')
// update session cookie with new channel name and ids
.then(result => {
console.log('login success', result);
// replace the current cookies
document.cookie = `channel_name=${result.channelName}`;
document.cookie = `channel_claim_id=${result.channelClaimId}`;
document.cookie = `short_channel_id=${result.shortChannelId}`;
return result;
})
// update channel selection
.then(result => {
const channelSelect = document.getElementById('channel-name-select');
// remove the old channel option
const oldChannel = document.getElementById('channel-option')
if (oldChannel){
oldChannel.parentNode.removeChild(oldChannel);
}
// add new channel option & select it
const newChannelOption = document.createElement('option');
newChannelOption.setAttribute('value', result.channelName);
newChannelOption.setAttribute('id', 'channel-option');
newChannelOption.setAttribute('selected', '');
newChannelOption.innerText = result.channelName;
channelSelect.insertBefore(newChannelOption, channelSelect.firstChild);
// update selection
toggleSelectedChannel(result.channelName);
})
.catch(error => {
showError(loginErrorDisplayElement, error);

View file

@ -17,9 +17,9 @@
<label class="label" for="channel-name-select">Channel:</label>
</div><div class="column column--7 column--sml-10">
<div id="input-error-channel-select" class="info-message info-message--failure"></div>
<select type="text" id="channel-name-select" class="select select--primary select--arrow" onchange="toggleChannelSelect(event.target.selectedOptions[0].value)">
<select type="text" id="channel-name-select" class="select select--primary select--arrow" onchange="toggleSelectedChannel(event.target.selectedOptions[0].value)">
{{#if user}}
<option value="{{user.channelName}}" >{{user.channelName}}</option>
<option value="{{user.channelName}}" id="channel-option">{{user.channelName}}</option>
{{/if}}
<option value="login">Existing</option>
<option value="new" >New</option>
@ -51,18 +51,18 @@
} else if (selectedOption === 'in a channel') {
channelSelectOptions.hidden = false;
// update url
const selectedChannel = document.getElementById('channel-name-select').selectedOptions[0].value
toggleChannelSelect(selectedChannel);
let selectedChannel = document.getElementById('channel-name-select').selectedOptions[0].value
toggleSelectedChannel(selectedChannel);
} else {
console.log('selected option was not recognized');
}
}
// show or hide the channel create/login tool
function toggleChannelSelect (selectedChannel) {
function toggleSelectedChannel (selectedChannel) {
const createChannelTool = document.getElementById('channel-create-details');
const loginToChannelTool = document.getElementById('channel-login-details');
console.log('toggleChannelSelect event triggered', selectedChannel);
console.log('toggleSelectedChannel event triggered', selectedChannel);
// show/hide the login and new channel forms
if (selectedChannel === 'new') {
createChannelTool.hidden = false;
@ -93,7 +93,10 @@
urlChannelPlaceholder.hidden = true;
} else {
urlChannel.hidden = false;
urlChannel.innerText = `{{user.channelName}}:{{user.shortChannelId}}`;
// show channel and short id
const selectedChannel = getCookie('channel_name');
const shortChannelId = getCookie('short_channel_id');
urlChannel.innerText = `${selectedChannel}:${shortChannelId}`;
urlNoChannelPlaceholder.hidden = true;
urlChannelPlaceholder.hidden = true;
}

View file

@ -25,7 +25,10 @@
console.log(event);
const selectedOption = event.target.selectedOptions[0].value;
if (selectedOption === 'logout') {
console.log('login');
console.log('log out');
// remove session cookies
// send logout request to server
window.location.href = '/logout';
} else if (selectedOption === 'view') {
console.log('view channel');