Commit 5ed56054adfc0cabf81e0b9bdda4e4e0fe108e8c

Authored by Laura Hawkins
1 parent aeb0676a5c

password change working

Showing 2 changed files with 46 additions and 13 deletions Inline Diff

scripts/SettingsController.js View file @ 5ed5605
angular.module('flashy.SettingsController', ['ui.router']). 1 1 angular.module('flashy.SettingsController', ['ui.router']).
2 2
controller('SettingsController', function($scope, $http) { 3 3 controller('SettingsController', function($scope, $http) {
4 $scope.error = false;
5 $scope.success = false;
6 $scope.mismatch = false;
7 $scope.unacceptable = false;
8
$scope.changePassword = function(oldPassword, newPassword, confirmedNewPassword) { 4 9 $scope.changePassword = function(oldPassword, newPassword, confirmedNewPassword) {
10 console.log('in change password');
11
12 $http.patch('/api/me/', {
13 'old_password': oldPassword,
14 'new_password': newPassword
15 }).
16 success(function(data) {
17
18 console.log('password successfully changes');
19
20 }).
21 error(function(data) {
22 console.log('not changed');
23 });
5 24
}; 6 25
console.log('checking to see if chrome'); 7 26 };
27
28
29
30 console.log('checking to see if chrome');
if (!chrome) { 8 31 if (!chrome) {
return; 9 32 return;
} 10 33 }
console.log('chrome'); 11 34 console.log('chrome');
12 35
console.log('executing things outside of module'); 13 36 console.log('executing things outside of module');
var PUSH_SERVER_URL = '/api/subscribe/'; 14 37 var PUSH_SERVER_URL = '/api/subscribe/';
15 38
function onPushSubscription(pushSubscription) { 16 39 function onPushSubscription(pushSubscription) {
console.log('pushSubscription = ', pushSubscription.endpoint); 17 40 console.log('pushSubscription = ', pushSubscription.endpoint);
// Here we would normally send the endpoint 18 41 // Here we would normally send the endpoint
// and subscription ID to our server. 19 42 // and subscription ID to our server.
// In this demo we just use send these values to 20 43 // In this demo we just use send these values to
// our server via XHR which sends a push message. 21 44 // our server via XHR which sends a push message.
22 45
var endpoint = pushSubscription.endpoint; 23 46 var endpoint = pushSubscription.endpoint;
var subscriptionId = pushSubscription.subscriptionId; 24 47 var subscriptionId = pushSubscription.subscriptionId;
25 48
console.log('registration_id: ', subscriptionId); 26 49 console.log('registration_id: ', subscriptionId);
$http.post(PUSH_SERVER_URL, {'registration_id': subscriptionId}); 27 50 $http.post(PUSH_SERVER_URL, {'registration_id': subscriptionId});
} 28 51 }
29 52
function subscribeDevice() { 30 53 function subscribeDevice() {
// We need the service worker registration to access the push manager 31 54 // We need the service worker registration to access the push manager
navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) { 32 55 navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) {
serviceWorkerRegistration.pushManager.subscribe() 33 56 serviceWorkerRegistration.pushManager.subscribe()
.then(onPushSubscription) 34 57 .then(onPushSubscription)
.catch(function(e) { 35 58 .catch(function(e) {
console.log('Error in subscribing'); 36 59 console.log('Error in subscribing');
// Check for a permission prompt issue 37 60 // Check for a permission prompt issue
if ('permissions' in navigator) { 38 61 if ('permissions' in navigator) {
navigator.permissions.query({name: 'push', userVisibleOnly: true}) 39 62 navigator.permissions.query({name: 'push', userVisibleOnly: true})
.then(function(permissionStatus) { 40 63 .then(function(permissionStatus) {
console.log('subscribe() Error: Push permission status = ', 41 64 console.log('subscribe() Error: Push permission status = ',
permissionStatus); 42 65 permissionStatus);
if (permissionStatus.status === 'denied') { 43 66 if (permissionStatus.status === 'denied') {
// The user blocked the permission prompt 44 67 // The user blocked the permission prompt
console.log('Ooops Notifications are Blocked', 45 68 console.log('Ooops Notifications are Blocked',
'Unfortunately you just permanently blocked notifications. ' + 46 69 'Unfortunately you just permanently blocked notifications. ' +
'Please unblock / allow them to switch on push ' + 47 70 'Please unblock / allow them to switch on push ' +
'notifications.'); 48 71 'notifications.');
} else { 49 72 } else {
console.log('Ooops Push Couldn\'t Register', 50 73 console.log('Ooops Push Couldn\'t Register',
'<p>When we tried to ' + 51 74 '<p>When we tried to ' +
'get the subscription ID for GCM, something went wrong,' + 52 75 'get the subscription ID for GCM, something went wrong,' +
' not sure why.</p>' + 53 76 ' not sure why.</p>' +
'<p>Have you defined "gcm_sender_id" and ' + 54 77 '<p>Have you defined "gcm_sender_id" and ' +
'"gcm_user_visible_only" in the manifest?</p>' + 55 78 '"gcm_user_visible_only" in the manifest?</p>' +
'<p>Error message: ' + 56 79 '<p>Error message: ' +
e.message + 57 80 e.message +
'</p>'); 58 81 '</p>');
} 59 82 }
}).catch(function(err) { 60 83 }).catch(function(err) {
console.log('Ooops Push Couldn\'t Register', 61 84 console.log('Ooops Push Couldn\'t Register',
'<p>When we tried to ' + 62 85 '<p>When we tried to ' +
'get the subscription ID for GCM, something went wrong, not ' + 63 86 'get the subscription ID for GCM, something went wrong, not ' +
'sure why.</p>' + 64 87 'sure why.</p>' +
'<p>Have you defined "gcm_sender_id" and ' + 65 88 '<p>Have you defined "gcm_sender_id" and ' +
'"gcm_user_visible_only" in the manifest?</p>' + 66 89 '"gcm_user_visible_only" in the manifest?</p>' +
'<p>Error message: ' + 67 90 '<p>Error message: ' +
err.message + 68 91 err.message +
'</p>'); 69 92 '</p>');
}); 70 93 });
} else { 71 94 } else {
// Use notification permission to do something 72 95 // Use notification permission to do something
if (Notification.permission === 'denied') { 73 96 if (Notification.permission === 'denied') {
console.log('Ooops Notifications are Blocked', 74 97 console.log('Ooops Notifications are Blocked',
'Unfortunately you just permanently blocked notifications. ' + 75 98 'Unfortunately you just permanently blocked notifications. ' +
'Please unblock / allow them to switch on push notifications.'); 76 99 'Please unblock / allow them to switch on push notifications.');
} else { 77 100 } else {
console.log('Ooops Push Couldn\'t Register', 78 101 console.log('Ooops Push Couldn\'t Register',
'<p>When we tried to ' + 79 102 '<p>When we tried to ' +
'get the subscription ID for GCM, something went wrong, not ' + 80 103 'get the subscription ID for GCM, something went wrong, not ' +
'sure why.</p>' + 81 104 'sure why.</p>' +
'<p>Have you defined "gcm_sender_id" and ' + 82 105 '<p>Have you defined "gcm_sender_id" and ' +
'"gcm_user_visible_only" in the manifest?</p>' + 83 106 '"gcm_user_visible_only" in the manifest?</p>' +
'<p>Error message: ' + 84 107 '<p>Error message: ' +
e.message + 85 108 e.message +
'</p>'); 86 109 '</p>');
} 87 110 }
} 88 111 }
}); 89 112 });
}); 90 113 });
} 91 114 }
92 115
function unsubscribeDevice() { 93 116 function unsubscribeDevice() {
navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) { 94 117 navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) {
serviceWorkerRegistration.pushManager.getSubscription().then( 95 118 serviceWorkerRegistration.pushManager.getSubscription().then(
function(pushSubscription) { 96 119 function(pushSubscription) {
// Check we have everything we need to unsubscribe 97 120 // Check we have everything we need to unsubscribe
if (!pushSubscription) { 98 121 if (!pushSubscription) {
return; 99 122 return;
} 100 123 }
101 124
// TODO: Remove the device details from the server 102 125 // TODO: Remove the device details from the server
// i.e. the pushSubscription.subscriptionId and 103 126 // i.e. the pushSubscription.subscriptionId and
// pushSubscription.endpoint 104 127 // pushSubscription.endpoint
105 128
pushSubscription.unsubscribe().then(function(successful) { 106 129 pushSubscription.unsubscribe().then(function(successful) {
console.log('Unsubscribed from push: ', successful); 107 130 console.log('Unsubscribed from push: ', successful);
if (!successful) { 108 131 if (!successful) {
// The unsubscribe was unsuccessful, but we can 109 132 // The unsubscribe was unsuccessful, but we can
// remove the subscriptionId from our server 110 133 // remove the subscriptionId from our server
// and notifications will stop 111 134 // and notifications will stop
// This just may be in a bad state when the user returns 112 135 // This just may be in a bad state when the user returns
console.error('We were unable to unregister from push'); 113 136 console.error('We were unable to unregister from push');
} 114 137 }
115 138
}).catch(function(e) { 116 139 }).catch(function(e) {
console.log('Unsubscribtion error: ', e); 117 140 console.log('Unsubscribtion error: ', e);
}); 118 141 });
}.bind(this)).catch(function(e) { 119 142 }.bind(this)).catch(function(e) {
console.error('Error thrown while revoking push notifications. ' + 120 143 console.error('Error thrown while revoking push notifications. ' +
'Most likely because push was never registered', e); 121 144 'Most likely because push was never registered', e);
}); 122 145 });
}); 123 146 });
} 124 147 }
125 148
function permissionStatusChange(permissionStatus) { 126 149 function permissionStatusChange(permissionStatus) {
console.log('permissionStatusChange = ', permissionStatus); 127 150 console.log('permissionStatusChange = ', permissionStatus);
// If the notification permission is denied, it's a permanent block 128 151 // If the notification permission is denied, it's a permanent block
switch (permissionStatus.status) { 129 152 switch (permissionStatus.status) {
case 'denied': 130 153 case 'denied':
console.log('Ooops Push has been Blocked', 131 154 console.log('Ooops Push has been Blocked',
'Unfortunately the user permanently blocked push. Please unblock / ' + 132 155 'Unfortunately the user permanently blocked push. Please unblock / ' +
'allow them to switch on push notifications.'); 133 156 'allow them to switch on push notifications.');
break; 134 157 break;
case 'granted': 135 158 case 'granted':
// Set the state of the push switch 136 159 // Set the state of the push switch
console.log('case granted'); 137 160 console.log('case granted');
break; 138 161 break;
case 'prompt': 139 162 case 'prompt':
console.log('case prompt'); 140 163 console.log('case prompt');
break; 141 164 break;
} 142 165 }
} 143 166 }
144 167
function setUpPushPermission() { 145 168 function setUpPushPermission() {
navigator.permissions.query({name: 'push', userVisibleOnly: true}) 146 169 navigator.permissions.query({name: 'push', userVisibleOnly: true})
.then(function(permissionStatus) { 147 170 .then(function(permissionStatus) {
// Set the initial state 148 171 // Set the initial state
permissionStatusChange(permissionStatus); 149 172 permissionStatusChange(permissionStatus);
150 173
// Handle Permission State Changes 151 174 // Handle Permission State Changes
permissionStatus.onchange = function() { 152 175 permissionStatus.onchange = function() {
permissionStatusChange(this); 153 176 permissionStatusChange(this);
}; 154 177 };
155 178
// Check if push is supported and what the current state is 156 179 // Check if push is supported and what the current state is
navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) { 157 180 navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) {
// Let's see if we have a subscription already 158 181 // Let's see if we have a subscription already
serviceWorkerRegistration.pushManager.getSubscription() 159 182 serviceWorkerRegistration.pushManager.getSubscription()
.then(function(subscription) { 160 183 .then(function(subscription) {
if (!subscription) { 161 184 if (!subscription) {
// NOOP 162 185 // NOOP
return; 163 186 return;
} 164 187 }
165 188
// Update the current state with the 166 189 // Update the current state with the
// subscriptionid and endpoint 167 190 // subscriptionid and endpoint
onPushSubscription(subscription); 168 191 onPushSubscription(subscription);
}) 169 192 })
.catch(function(e) { 170 193 .catch(function(e) {
console.log('An error occured while calling getSubscription()', e); 171 194 console.log('An error occured while calling getSubscription()', e);
}); 172 195 });
}); 173 196 });
}).catch(function(err) { 174 197 }).catch(function(err) {
console.log('Ooops Unable to check the permission', 175 198 console.log('Ooops Unable to check the permission',
'Unfortunately the permission for push notifications couldn\'t be ' + 176 199 'Unfortunately the permission for push notifications couldn\'t be ' +
'checked. Are you on Chrome 43+?'); 177 200 'checked. Are you on Chrome 43+?');
}); 178 201 });
} 179 202 }
180 203
function setUpNotificationPermission() { 181 204 function setUpNotificationPermission() {
// If the notification permission is denied, it's a permanent block 182 205 // If the notification permission is denied, it's a permanent block
if (Notification.permission === 'denied') { 183 206 if (Notification.permission === 'denied') {
console.log('Ooops Notifications are Blocked', 184 207 console.log('Ooops Notifications are Blocked',
'Unfortunately notifications are permanently blocked. Please unblock / ' + 185 208 'Unfortunately notifications are permanently blocked. Please unblock / ' +
'allow them to switch on push notifications.'); 186 209 'allow them to switch on push notifications.');
return; 187 210 return;
} else if (Notification.permission === 'default') { 188 211 } else if (Notification.permission === 'default') {
console.log('notification permissions === default'); 189 212 console.log('notification permissions === default');
return; 190 213 return;
} 191 214 }
192 215
// Check if push is supported and what the current state is 193 216 // Check if push is supported and what the current state is
navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) { 194 217 navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) {
// Let's see if we have a subscription already 195 218 // Let's see if we have a subscription already
serviceWorkerRegistration.pushManager.getSubscription() 196 219 serviceWorkerRegistration.pushManager.getSubscription()
.then(function(subscription) { 197 220 .then(function(subscription) {
if (!subscription) { 198 221 if (!subscription) {
// NOOP 199 222 // NOOP
console.log('not subscription'); 200 223 console.log('not subscription');
return; 201 224 return;
} 202 225 }
203 226
// Update the current state with the 204 227 // Update the current state with the
// subscriptionid and endpoint 205 228 // subscriptionid and endpoint
console.log('onpushsubscription should be entered'); 206 229 console.log('onpushsubscription should be entered');
onPushSubscription(subscription); 207 230 onPushSubscription(subscription);
}) 208 231 })
.catch(function(e) { 209 232 .catch(function(e) {
console.log('An error occured while calling getSubscription()', e); 210 233 console.log('An error occured while calling getSubscription()', e);
}); 211 234 });
}); 212 235 });
} 213 236 }
214 237
// Once the service worker is registered set the initial state 215 238 // Once the service worker is registered set the initial state
function initialiseState() { 216 239 function initialiseState() {
// Is the Permissions API supported 217 240 // Is the Permissions API supported
if ('permissions' in navigator) { 218 241 if ('permissions' in navigator) {
console.log('setting push permissions'); 219 242 console.log('setting push permissions');
setUpPushPermission(); 220 243 setUpPushPermission();
return; 221 244 return;
} else { 222 245 } else {
console.log('setting notification permissions'); 223 246 console.log('setting notification permissions');
setUpNotificationPermission(); 224 247 setUpNotificationPermission();
} 225 248 }
} 226 249 }
227 250
var enablePushSwitch = $('.js-checkbox'); 228 251 var enablePushSwitch = $('.js-checkbox');
enablePushSwitch.change(function(e) { 229 252 enablePushSwitch.change(function(e) {
console.log('checkbox changed'); 230 253 console.log('checkbox changed');
if (e.target.checked) { 231 254 if (e.target.checked) {
console.log('subscribing device'); 232 255 console.log('subscribing device');
subscribeDevice(); 233 256 subscribeDevice();
templates/settings.html View file @ 5ed5605
<div class="card" id="resetPasswordForm"> 1 1 <div class="card" id="resetPasswordForm">
2 2
<!-- 3 3 <!--
class="js-checkbox" name="notifbox" value="toggle notifs"> --> 4 4 class="js-checkbox" name="notifbox" value="toggle notifs"> -->
<form action="#"> 5 5 <form action="#">
<input type="checkbox" id = "notifbox" class="js-checkbox" /> 6 6 <input type="checkbox" id = "notifbox" class="js-checkbox" />
<label for="notifbox">Check this to enable notifications</label> 7 7 <label for="notifbox">Check this to enable notifications</label>
</form> 8 8 </form>
9 9
<h2>Change Password</h2> 10 10 <h2>Change Password</h2>
11 11
<div class="row"> 12 12 <div class="row">
<form class="col s12"> 13 13 <form class="col s12" name="ChangePasswordForm">
14 14
<div class="row"> 15 15 <div class="row">
<div class="input-field col s12"> 16 16 <div class="input-field col s12">
<input id="password" type="password" ng-model="oldPassword" class="validate"> 17 17 <input id="password" required type="password" name="oldpw" ng-model="oldPassword" class="validate">
<label for="password">Old Password</label> 18 18 <label for="password">Old Password</label>
</div> 19 19 </div>
</div> 20 20 </div>
21 21
22 <div role="alert">
23 <span class="error" ng-show="ChangePasswordForm.oldpw.$error.required">
24 Required!</span>
25 </div>
26
27
<div class="row"> 22 28 <div class="row">
<div class="input-field col s12"> 23 29 <div class="input-field col s12">
<input id="password" type="password" ng-model="newPassword" class="validate"> 24 30 <input id="password" required ng-minlength=8 type="password" name="newpw" ng-model="newPassword" class="validate">
<label for="password">New Password</label> 25 31 <label for="password">New Password</label>
</div> 26 32 </div>
</div> 27 33 </div>
34
35 <div role="alert">
36 <span class="error" ng-show="ChangePasswordForm.newpw.$error.minlength">
37 New password must be at least 8 characters. </span>
38 </div>
28 39
<div class="row"> 29 40 <div class="row">
<div class="input-field col s12"> 30 41 <div class="input-field col s12">
<input id="password" type="password" ng-model="confirmedNewPassword" class="validate"> 31 42 <input id="password" required ng-minlength=8 compare-to="newpw" type="password" name="confirmpw" ng-model="confirmedNewPassword" class="validate">
<label for="password">Confirm New Password</label> 32 43 <label for="password">Confirm New Password</label>
</div> 33 44 </div>
</div> 34 45 </div>
46
47 <div role="alert">
48 <span class="error" ng-show="ChangePasswordForm.confirm.$error.minlength">
49 Must be the same as the new password. </span>
50 </div>
35 51
36 52
</form> 37 53 </form>
38