From 6eb24516e233a46dfcfecb8ee5590c7b53871fde Mon Sep 17 00:00:00 2001
From: Andrew Buss <abuss@ucsd.edu>
Date: Tue, 2 Jun 2015 11:26:16 -0700
Subject: [PATCH] clean up reset flow

---
 scripts/RequestResetController.js   |  5 +-
 scripts/ResetPasswordController.js  | 95 +++++++++++++++++--------------------
 scripts/RootController.js           |  2 +-
 scripts/SettingsController.js       |  4 +-
 scripts/UserService.js              | 21 ++++----
 templates/requestpasswordreset.html | 64 ++++++++++++-------------
 templates/resetpassword.html        | 90 +++++++++++++++++------------------
 7 files changed, 135 insertions(+), 146 deletions(-)

diff --git a/scripts/RequestResetController.js b/scripts/RequestResetController.js
index 892cefc..97fbf13 100644
--- a/scripts/RequestResetController.js
+++ b/scripts/RequestResetController.js
@@ -7,9 +7,8 @@ angular.module('flashy.RequestResetController', ['ui.router']).
             $http.post('/api/request_password_reset/', {email: email}).
                 success(function(data) {
                     $scope.success = true;
-                    //$state.go('requestresetsuccess');
-                    console.log('SUCCESS');
-                    console.log(data);
+                    Materialize.toast('A password reset link was sent to your email', 4000);
+                    $state.go('login');
                 }).
                 error(function(data, status, header, config) {
                     if (data.email) {
diff --git a/scripts/ResetPasswordController.js b/scripts/ResetPasswordController.js
index 552d7f1..d29a429 100644
--- a/scripts/ResetPasswordController.js
+++ b/scripts/ResetPasswordController.js
@@ -1,54 +1,45 @@
 angular.module('flashy.ResetPasswordController', ['ui.router']).
 
-controller('ResetPasswordController', ['$scope', '$state', '$http', '$timeout',
-      function($scope, $state, $http, $timeout) {
-    'use strict';
-    var url = document.location.href.split('/');
-    var token = url[url.length - 1];
-    var uid = url[url.length - 2];
-    $scope.error = false;
-    $scope.success = false;
-    $scope.mismatch = false;
-    $scope.unacceptable = false;
-    /*if(token == 'resetpassword') {
-      $state.go('login');
-    }*/
-    console.log('RESETTING');
-    $scope.confirmResetPass = function() {
-                if ($scope.newPassword.length < 8) {
-                        $scope.unacceptable = true;
-                        return;
-                }
-                if ($scope.newPassword != $scope.confirmPassword) {
-                        $scope.mismatch = true;
-                        $scope.confirmPassword.$setPristine();
-                        console.log('mismatch');
-                        return;
-                }
-                /*password passes local tests*/
-                $http.post('/api/reset_password/', JSON.stringify({
-                        'uid': uid,
-                        'token': token,
-                        'new_password': $scope.newPassword
-                        }))
-                        .success(function(data) {
-                                $scope.error = false;
-                                $scope.success = true;
-                                //$state.go('resetpasssuccess');
-                                $timeout(function($state) {
-                                        $state.go('login');
-                                }, 1000);
-                                console.log(data);
-                        })
-                        .error(function(data, status, header, config) {
-                                $scope.error = true;
-                                $scope.success = false;
-                                $scope.mismatch = false;
-                                $scope.unacceptable = false;
-                                console.log(data);
-                        });
-    };
-    $scope.cancelReset = function() {
-        $state.go('login');
-    };
-}]);
+    controller('ResetPasswordController', function($scope, $state, $stateParams, $http, UserService) {
+        $scope.error = false;
+        $scope.success = false;
+        $scope.mismatch = false;
+        $scope.unacceptable = false;
+
+        $scope.confirmResetPass = function() {
+            if ($scope.newPassword.length < 8) {
+                $scope.unacceptable = true;
+                return;
+            }
+            if ($scope.newPassword != $scope.confirmPassword) {
+                $scope.mismatch = true;
+                $scope.confirmPassword.$setPristine();
+                console.log('mismatch');
+                return;
+            }
+            /*password passes local tests*/
+            $http.post('/api/reset_password/', JSON.stringify({
+                'uid': $stateParams.uid,
+                'token': $stateParams.token,
+                'new_password': $scope.newPassword
+            }))
+                .success(function(data) {
+                    $scope.error = false;
+                    $scope.success = true;
+                    UserService.startAuth().then(function() {
+                        $state.go('login');
+                    });
+                    Materialize.toast('Your password was successfully changed', 4000);
+                })
+                .error(function(data, status, header, config) {
+                    $scope.error = true;
+                    $scope.success = false;
+                    $scope.mismatch = false;
+                    $scope.unacceptable = false;
+                    console.log(data);
+                });
+        };
+        $scope.cancelReset = function() {
+            $state.go('login');
+        };
+    });
diff --git a/scripts/RootController.js b/scripts/RootController.js
index e60cac6..2c57375 100644
--- a/scripts/RootController.js
+++ b/scripts/RootController.js
@@ -34,7 +34,7 @@ angular.module('flashy.RootController', ['ui.router', 'ngResource', 'ngSanitize'
             $('#classDropdown').toggle();
         })
         */
-        
+
         var postlogin = function(data) {
             $scope.user = data;
             //UserService.redirectToDefaultState($state);
diff --git a/scripts/SettingsController.js b/scripts/SettingsController.js
index 8394772..05f8c3b 100644
--- a/scripts/SettingsController.js
+++ b/scripts/SettingsController.js
@@ -27,7 +27,7 @@ angular.module('flashy.SettingsController', ['ui.router']).
                         callback(true);
                 }
 
-            console.log("gonna try to launch service worker now");
+            console.log('gonna try to launch service worker now');
             if ('serviceWorker' in navigator) {
 
                 console.log('gonna try to launch service worker now');
@@ -55,4 +55,4 @@ angular.module('flashy.SettingsController', ['ui.router']).
               }
 
 
-	});
\ No newline at end of file
+        });
diff --git a/scripts/UserService.js b/scripts/UserService.js
index f8364aa..1cb744e 100644
--- a/scripts/UserService.js
+++ b/scripts/UserService.js
@@ -19,15 +19,17 @@ angular.module('flashy.UserService', ['ui.router']).
             deferred.resolve(data);
         };
         this.login = login;
-        $http.get('/api/me/').success(function(data) {
-            console.log('user is logged in!');
-            login(data);
-        }).error(function(data) {
-            console.log(data);
-            console.log('not logged in yet: ' + data.detail);
-            _user = {email: false};
-            deferred.resolve(_user);
-        });
+        this.startAuth = function() {
+            return $http.get('/api/me/').success(function(data) {
+                console.log('user is logged in!');
+                login(data);
+            }).error(function(data) {
+                console.log(data);
+                console.log('not logged in yet: ' + data.detail);
+                _user = {email: false};
+                deferred.resolve(_user);
+            });
+        };
 
         this.isResolved = function() {
             return !!_user;
@@ -102,4 +104,5 @@ angular.module('flashy.UserService', ['ui.router']).
                 Materialize.toast('Resent confirmation email! Check your spam folder too.', 4000);
             });
         };
+        this.startAuth();
     });
diff --git a/templates/requestpasswordreset.html b/templates/requestpasswordreset.html
index ba15111..bb81577 100644
--- a/templates/requestpasswordreset.html
+++ b/templates/requestpasswordreset.html
@@ -1,39 +1,37 @@
-<div class="row" ng-show="success">
-  <h1>Request sent!</h1>
-  <h1>Check your email in a few minutes.</h1>
-</div>
+<div class="" style="margin-top:32px;">
+  <div class="row" style="max-width:512px; width:50%; min-width:256px; margin: 0 auto">
+    <div class="card">
+      <form class="col s12" name="passreset_form">
 
-<div class="row" ng-hide="success">
-<div class="offset-s2 col s8">
-<div class="card">
-  <form class="col s12" name="passreset_form">
-    <div class="card-content">
-      <h2>Reset Password</h2>
-    </div>
-    
-    <div class="divider"></div>
-    <div name="passreset" class="card-content">
-      <div class="section">
-        <div ng-show="error" role="error">
-          <i style="color:#8E2323" class="mdi-alert-error"></i>
-          <span style="color:#8E2323">Enter a valid email!</span>
+        <div class="card-content">
+          <h2>Reset Password</h2>
+        </div>
+
+        <div class="divider"></div>
+        <div name="passreset" class="card-content">
+          <div class="section">
+            <div ng-show="error" role="error">
+              <i style="color:#8E2323" class="mdi-alert-error"></i>
+              <span style="color:#8E2323">Enter a valid email!</span>
+            </div>
+          </div>
+          <!--FORM INPUT-->
+          <div class="input-field">
+            <label for="email">Enter Your Email</label>
+            <input id="email" type="email" class="form-control" ng-model="user_email" placeholder="" required/>
+          </div>
         </div>
-      </div>
-      <!--FORM INPUT-->
-      <div class="input-field">
-        <label for="email">Enter Your Email</label>
-        <input id="email" type="email" class="form-control" ng-model="user_email" placeholder="" required />
-      </div>
-    </div>
 
-    <div class="card-action">
-      <button class="btn waves-effect waves-light green right" type="submit" name="action"
-              ng-click="resetPass(user_email)">Reset</button>
-      <button class="btn waves-effect waves-light red" type="submit" name="action"
-              ng-click="cancelReset()">Cancel</button>
+        <div class="card-action">
+          <button class="btn waves-effect waves-light green right" type="submit" name="action"
+                  ng-click="resetPass(user_email)">Reset
+          </button>
+          <button class="btn waves-effect waves-light red" type="submit" name="action"
+                  ng-click="cancelReset()">Cancel
+          </button>
 
+        </div>
+      </form>
     </div>
-  </form>
-</div>
-</div>
+  </div>
 </div>
diff --git a/templates/resetpassword.html b/templates/resetpassword.html
index 385580b..ba0cfcd 100644
--- a/templates/resetpassword.html
+++ b/templates/resetpassword.html
@@ -1,49 +1,47 @@
-<div class="row" ng-show="success">
-  <h1>Reset password successful!</h1>
-</div>
+<div class="" style="margin-top:32px;">
+  <div class="row" style="max-width:512px; width:50%; min-width:256px; margin: 0 auto">
+    <div class="card">
+      <form class="col s12" name="resetpass_form">
+        <div class="card-content">
+          <h2 class="">Reset Password</h2>
+        </div>
+        <div class="divider"></div>
+        <div class="card-content">
+          <!--ERRORS-->
+          <div role="alert" ng-show="error">
+            <i style="color:#8E2323" class="mdi-alert-error"></i>
+            <span style="color:#8E2323">Your password reset link is invalid. Perhaps it has already been used?</span>
+          </div>
+          <div role="alert" ng-show="mismatch && newPassword != confirmPassword">
+            <i style="color:#8E2323" class="mdi-alert-error"></i>
+            <span style="color:#8E2323">Passwords do not match!</span>
+          </div>
+          <div role="alert" ng-show="unacceptable && newPassword.length < 8">
+            <i style="color:#8E2323" class="mdi-alert-error"></i>
+            <span style="color:#8E2323">Please make a password with at least 8 characters!</span>
+          </div>
+          <!--INPUTS-->
+          <div class="input-field">
+            <input id="newpassword" type="password" class="validate" ng-model="newPassword" placeholder="" required/>
+            <label for="newpassword">Password</label>
+          </div>
+          <div class="input-field">
+            <input id="confirmpassword" type="password" class="validate" ng-model="confirmPassword" placeholder=""
+                   required/>
+            <label for="confirmpassword">Confirm password</label>
+          </div>
+        </div>
 
-<div class="container" ng-hide="success">
-<div class="row">
-<div class="offset-s2 col s8">
-<div class="card">
-  <form class="col s12" name="resetpass_form">
-    <div class="card-content">
-      <h2 class="">Reset Password</h2>
-    </div>
-    <div class="divider"></div>
-    <div class="card-content">
-      <!--ERRORS-->
-      <div role="alert" ng-show="error">
-        <i style="color:#8E2323" class="mdi-alert-error"></i>
-        <span style="color:#8E2323">Please check your reset password link!</span>
-      </div>
-      <div role="alert" ng-show="mismatch && newPassword != confirmPassword">
-        <i style="color:#8E2323" class="mdi-alert-error"></i>
-        <span style="color:#8E2323">Passwords do not match!</span>
-      </div>
-      <div role="alert" ng-show="unacceptable && newPassword.length < 8">
-        <i style="color:#8E2323" class="mdi-alert-error"></i>
-        <span style="color:#8E2323">Please make a password with at least 8 characters!</span>
-      </div>
-      <!--INPUTS-->
-      <div class="input-field">
-        <input id="newpassword" type="password" class="validate" ng-model="newPassword" placeholder="" required/>
-        <label for="newpassword">Password</label>
-      </div>
-      <div class="input-field">
-        <input id="confirmpassword" type="password" class="validate" ng-model="confirmPassword" placeholder="" required/>
-        <label for="confirmpassword">Confirm password</label>
-      </div>
-    </div>
-    
-    <div class="card-action">
-      <button class="btn waves-effect waves-light red" type="submit" name="action"
-              ng-click="cancelReset()">Cancel</button>
-      <button class="btn waves-effect waves-light green right" type="submit" name="action"
-              ng-click="confirmResetPass()">Confirm</button>
+        <div class="card-action">
+          <button class="btn waves-effect waves-light green right" type="submit" name="submit"
+                  ng-click="confirmResetPass()">Confirm
+          </button>
+          <button class="btn waves-effect waves-light red" type="submit" name="action"
+                  ng-click="cancelReset()">Cancel
+          </button>
+
+        </div>
+      </form>
     </div>
-  </form>
-</div>  
-</div>
-</div>
+  </div>
 </div>
\ No newline at end of file
-- 
1.9.1