forked from IBM-Cloud/nodejs-MEAN-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
257 lines (229 loc) · 8.03 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// See LICENSE.MD for license information.
var app = angular.module('MEANapp', ['ngRoute', 'ngStorage']);
/*********************************
Controllers
*********************************/
app.controller('HeaderController', function($scope, $localStorage, $sessionStorage, $location, $http){
// Set local scope to persisted user data
$scope.user = $localStorage;
// Logout function
$scope.logout = function(){
$http({
method: 'GET',
url: '/account/logout'
})
.success(function(response){
alert(response);
$localStorage.$reset();
$location.path('/');
})
.error(function(response){
alert(response);
$location.path('/account/login');
}
);
};
});
app.controller('HomeController', function($scope, $localStorage, $sessionStorage){});
app.controller('LoginController', function($scope, $localStorage, $sessionStorage, $location, $http){
// Login submission
$scope.submitLogin = function(){
// Login request
$http({
method: 'POST',
url: '/account/login',
data: {
'username': $scope.loginForm.username,
'password': $scope.loginForm.password
}
})
.success(function(response){
// $localStorage persists data in browser's local storage (prevents data loss on page refresh)
$localStorage.status = true;
$localStorage.user = response;
$location.path('/');
})
.error(function(){
alert('Login failed. Check username/password and try again.');
}
);
};
// Redirect to account creation page
$scope.createAccount = function(){
$location.path('/account/create');
}
});
app.controller('CreateAccountController', function($scope, $localStorage, $sessionStorage, $http, $location){
// Create account
$scope.submitForm = function(){
$http({
method: 'POST',
url: '/account/create',
data: {
'username': $scope.newUser.username,
'password': $scope.newUser.password,
'name' : $scope.newUser.name,
'email' : $scope.newUser.email
}
})
.success(function(response){
alert(response);
$location.path('/account/login');
})
.error(function(response){
// When a string is returned
if(typeof response === 'string'){
alert(response);
}
// When array is returned
else if (Array.isArray(response)){
// More than one message returned in the array
if(response.length > 1){
var messages = [],
allMessages;
for (var i = response.length - 1; i >= 0; i--) {
messages.push(response[i]['msg']);
if(response.length == 0){
allMessages = messages.join(", ");
alert(allMessages);
console.error(response);
}
}
}
// Single message returned in the array
else{
alert(response[0]['msg']);
console.error(response);
}
}
// When something else is returned
else{
console.error(response);
alert("See console for error.");
}
}
);
};
});
app.controller('AccountController', function($scope, $localStorage, $sessionStorage, $http, $location){
// Create static copy of user data for form usage (otherwise any temporary changes will bind permanently to $localStorage)
$scope.formData = $.extend(true,{},$localStorage.user);
// Update user's account with new data
$scope.updateAccount = function(){
$http({
method: 'POST',
url: '/account/update',
data: {
'username': $scope.formData.username,
'password': $scope.password,
'name' : $scope.formData.name,
'email' : $scope.formData.email
}
})
.success(function(response){
$localStorage.user = $scope.formData;
alert(response);
})
.error(function(response){
// When a string is returned
if(typeof response === 'string'){
alert(response);
}
// When an array is returned
else if (Array.isArray(response)){
// More than one message returned in the array
if(response.length > 1){
var messages = [],
allMessages;
for (var i = response.length - 1; i >= 0; i--) {
messages.push(response[i]['msg']);
if(response.length == 0){
allMessages = messages.join(", ");
alert(allMessages);
console.error(response);
}
}
}
// Single message returned in the array
else{
alert(response[0]['msg']);
console.error(response);
}
}
// When something else is returned
else{
console.error(response);
alert("See console for error.");
}
}
);
};
// Delete user's account
$scope.deleteAccount = function(){
var response = confirm("Are you sure you want to delete your account? This cannot be undone!");
if(response == true){
$http({
method: 'POST',
url: '/account/delete',
data: {
'username': $scope.formData.username
}
})
.success(function(response){
$localStorage.$reset();
alert(response);
$location.path('/');
})
.error(function(response){
alert(response);
}
);
}
};
});
app.controller('ProtectedController', function($scope, $location, $http){
$http({
method: 'GET',
url: '/protected'
})
.success(function(response){
$scope.message = response;
})
.error(function(response){
alert(response);
$location.path('/account/login');
}
);
});
/*********************************
Routing
*********************************/
app.config(function($routeProvider) {
'use strict';
$routeProvider.
//Root
when('/', {
templateUrl: 'views/home.html',
controller: 'HomeController'
}).
//Login page
when('/account/login', {
templateUrl: 'views/login.html',
controller: 'LoginController'
}).
//Account page
when('/account', {
templateUrl: 'views/account.html',
controller: 'AccountController'
}).
//Create Account page
when('/account/create', {
templateUrl: 'views/create_account.html',
controller: 'CreateAccountController'
}).
//Protected page
when('/protected', {
templateUrl: 'views/protected.html',
controller: 'ProtectedController'
});
});