minimized cookie usage as state

This commit is contained in:
bill bittner 2017-11-30 18:23:27 -08:00
parent c509c35926
commit 272e4747ab
2 changed files with 12 additions and 23 deletions

View file

@ -33,15 +33,13 @@ function publishNewChannel (event) {
return sendAuthRequest(userName, password, '/signup') // post the request return sendAuthRequest(userName, password, '/signup') // post the request
}) })
.then(result => { .then(result => {
setUserCookies(result.channelName, result.channelClaimId, result.shortChannelId);
showChannelCreateDoneDisplay(); showChannelCreateDoneDisplay();
// refresh window logged in as the channel // if user is on the home page, update the needed elements without reloading
setUserCookies(result.channelName, result.channelClaimId, result.shortChannelId); // set cookies
})
.then(() => {
if (window.location.pathname === '/') { if (window.location.pathname === '/') {
// remove old channel and replace with new one & select it replaceChannelOptionInPublishChannelSelect(result.channelName);
replaceChannelOptionInPublishChannelSelect(); replaceChannelOptionInNavBarChannelSelect(result.channelName);
replaceChannelOptionInNavBarChannelSelect(); // if user is not on home page, redirect to home page
} else { } else {
window.location = '/'; window.location = '/';
} }

View file

@ -1,11 +1,9 @@
function replaceChannelOptionInPublishChannelSelect() { function replaceChannelOptionInPublishChannelSelect(loggedInChannel) {
// remove the old channel option // remove the old channel option
const oldChannel = document.getElementById('publish-channel-select-channel-option') const oldChannel = document.getElementById('publish-channel-select-channel-option')
if (oldChannel){ if (oldChannel){
oldChannel.parentNode.removeChild(oldChannel); oldChannel.parentNode.removeChild(oldChannel);
} }
// get channel details from cookies
const loggedInChannel = getCookie('channel_name');
// create new channel option // create new channel option
const newChannelOption = document.createElement('option'); const newChannelOption = document.createElement('option');
newChannelOption.setAttribute('value', loggedInChannel); newChannelOption.setAttribute('value', loggedInChannel);
@ -19,14 +17,12 @@ function replaceChannelOptionInPublishChannelSelect() {
toggleSelectedChannel(loggedInChannel); toggleSelectedChannel(loggedInChannel);
} }
function replaceChannelOptionInNavBarChannelSelect () { function replaceChannelOptionInNavBarChannelSelect (loggedInChannel) {
// remove the old channel option // remove the old channel option
const oldChannel = document.getElementById('nav-bar-channel-select-channel-option'); const oldChannel = document.getElementById('nav-bar-channel-select-channel-option');
if (oldChannel){ if (oldChannel){
oldChannel.parentNode.removeChild(oldChannel); oldChannel.parentNode.removeChild(oldChannel);
} }
// get channel details from cookies
const loggedInChannel = getCookie('channel_name');
// create new channel option & select it // create new channel option & select it
const newChannelOption = document.createElement('option'); const newChannelOption = document.createElement('option');
newChannelOption.setAttribute('value', loggedInChannel); newChannelOption.setAttribute('value', loggedInChannel);
@ -49,20 +45,15 @@ function loginToChannel (event) {
event.preventDefault() event.preventDefault()
validationFunctions.validateNewChannelLogin(userName, password) validationFunctions.validateNewChannelLogin(userName, password)
.then(() => { .then(() => {
// send request
return sendAuthRequest(userName, password, '/login') return sendAuthRequest(userName, password, '/login')
}) })
.then(result => { .then(result => {
// update session cookie with new channel name and id's setUserCookies(result.channelName, result.channelClaimId, result.shortChannelId);
setUserCookies(result.channelName, result.channelClaimId, result.shortChannelId); // replace the current cookies // if user is on the home page, update the needed elements without reloading
})
.then(() => {
// update channel selection
if (window.location.pathname === '/') { if (window.location.pathname === '/') {
// remove old channel and replace with new one & select it replaceChannelOptionInPublishChannelSelect(result.channelName);
replaceChannelOptionInPublishChannelSelect(); replaceChannelOptionInNavBarChannelSelect(result.channelName);
// remove old channel and replace with new one & select it // if user is not on home page, redirect to home page
replaceChannelOptionInNavBarChannelSelect();
} else { } else {
window.location = '/'; window.location = '/';
} }