From ef475ba951d8ccff253b011150f7fb9a0ea819c4 Mon Sep 17 00:00:00 2001
From: bill bittner <bittner.w@gmail.com>
Date: Wed, 20 Sep 2017 15:43:42 -0700
Subject: [PATCH] login completed

---
 passport/local-login.js                  |  7 ++++---
 public/assets/js/authFunctions.js        |  6 ++++--
 views/login.handlebars                   | 12 ++++++++----
 views/partials/publishChannel.handlebars |  9 ++++++---
 4 files changed, 22 insertions(+), 12 deletions(-)

diff --git a/passport/local-login.js b/passport/local-login.js
index 719ce1ee..5812abae 100644
--- a/passport/local-login.js
+++ b/passport/local-login.js
@@ -10,18 +10,19 @@ module.exports = new PassportLocalStrategy(
     passReqToCallback: true,
   },
   (req, username, password, done) => {
-    logger.debug('verifying loggin attempt');
-    username = `@${username}`;
+    logger.debug(`verifying loggin attempt ${username} ${password}`);
     return db.User
         .findOne({where: {channelName: username}})
         .then(user => {
           if (!user) {
+            logger.debug('no user found');
             return done(null, false, {message: 'Incorrect username or password.'});
           }
           if (!user.validPassword(password, user.password)) {
+            logger.debug('incorrect password');
             return done(null, false, {message: 'Incorrect username or password.'});
           }
-          logger.debug('user', user.dataValues);
+          logger.debug('user found:', user.dataValues);
           return done(null, user);
         })
         .catch(error => {
diff --git a/public/assets/js/authFunctions.js b/public/assets/js/authFunctions.js
index 5b52ee4c..3cc7a85f 100644
--- a/public/assets/js/authFunctions.js
+++ b/public/assets/js/authFunctions.js
@@ -14,10 +14,12 @@ function sendAuthRequest (channelName, password, url) {  // url === /signup or /
                     if (this.response == true) {
                         resolve();
                     } else {
-                        reject( new NameError("Your request could not be completed"));
+                        reject(new NameError('Your request succedded but could not be completed'));
                     }
+                } else if (this.status == 401) {
+                    reject('Incorrect username or password')
                 } else {
-                    reject("createChannel request failed with status:" + this.status);
+                    reject('Auth request failed with status:' + this.status);
                 };
             }
         };
diff --git a/views/login.handlebars b/views/login.handlebars
index b415c9bf..7823a498 100644
--- a/views/login.handlebars
+++ b/views/login.handlebars
@@ -5,6 +5,7 @@
         <h2>Log In</h2>
         <p>Log in to an existing channel:</p>
         <form id="login-form">
+            <div id="login-error-display-element" class="info-message info-message--failure"></div>
             <div>
                 <label>Username:</label>
                 @ <input type="text" id="login-channel-name" class="input-text input-text--primary"/>
@@ -50,12 +51,13 @@
         // validate submission
         validateNewChannelSubmission(channelName, password)
             .then(() => {
+                document.getElementById('publish-channel-form').innerHTML = '<p>Creating your new channel...</p>';
                 return sendAuthRequest(channelName, password, '/signup'); // post the request
             })
             .then(() => {
                 console.log('signup success');
-                document.getElementById('publish-channel-form').innerHTML = '<p>Your channel has been successfully created! Redirecting you now...</p>';
-                window.location.href = `/${channelName}`;
+                document.getElementById('publish-channel-form').innerHTML = '<p>Your channel has been successfully created! Signing you in now...</p>';
+                window.location.href = '/';
             })
             .catch(error => {
                 if (error.name === 'ChannelNameError'){
@@ -70,15 +72,17 @@
     function loginToChannel (event) {
         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 action
         event.preventDefault()
         // send request
         sendAuthRequest(channelName, password, '/login')
-            .then(() => {
+            .then( () => {
                 console.log('login success');
-                //window.location.href = `/${channelName}`;
+                window.location.href = `/${channelName}`;
             })
             .catch(error => {
+                showError(loginErrorDisplayElement, error);
                 console.log('login failure:', error);
             })
     }
diff --git a/views/partials/publishChannel.handlebars b/views/partials/publishChannel.handlebars
index 97cfb7e4..32bf8c32 100644
--- a/views/partials/publishChannel.handlebars
+++ b/views/partials/publishChannel.handlebars
@@ -15,6 +15,7 @@
     <div id="channel-login-details" hidden="true">
         <p>
         <form id="channel-login-form">
+            <div id="login-error-display-element" class="info-message info-message--failure"></div>
             <div>
                 <label for="login-channel-name">Channel Name: </label>
                 @<input type="text" name="login-channel-name" id="login-channel-name" class="input-text input-text--primary" placeholder="" value="">
@@ -79,14 +80,14 @@
      // validate submission
      validateNewChannelSubmission(channelName, password)
          .then(() => {
-             document.getElementById('channel-create-details').innerHTML = '<p>Your channel is being created...</p>';
+             document.getElementById('channel-create-details').innerHTML = '<p>Creating your new channel...</p>';
              return sendAuthRequest(channelName, password, '/signup') // post the request
          })
          .then(() => {
              console.log('success');
              document.getElementById('channel-create-details').innerHTML = '<p>Your channel has been successfully created!</p>';
              // referesh window logged in as the channel
-             window.location.href = `/`;
+             window.location.href = '/';
          })
          .catch(error => {
              if (error.name === 'ChannelNameError'){
@@ -102,15 +103,17 @@
  function loginToChannel (event) {
      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
      event.preventDefault()
      // send request
      sendAuthRequest(channelName, password, '/login')
          .then(() => {
              console.log('login success');
-             //window.location.href = `/${channelName}`;
+             window.location.href = '/';
          })
          .catch(error => {
+             showError(loginErrorDisplayElement, error);
              console.log('login failure:', error);
          })
  }