-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathauth.js
More file actions
356 lines (329 loc) · 12.4 KB
/
auth.js
File metadata and controls
356 lines (329 loc) · 12.4 KB
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
devise.provider('Auth', function AuthProvider() {
/**
* The default paths.
*/
var paths = {
login: '/users/sign_in.json',
logout: '/users/sign_out.json',
register: '/users.json',
sendResetPasswordInstructions: '/users/password.json',
resetPassword: '/users/password.json'
};
/**
* The default HTTP methods to use.
*/
var methods = {
login: 'POST',
logout: 'DELETE',
register: 'POST',
sendResetPasswordInstructions: 'POST',
resetPassword: 'PUT'
};
/**
* The default host URL.
*/
var baseUrl = '';
/**
* Default devise resource_name is 'user', can be set to any string.
* If it's falsey, it will not namespace the data.
*/
var resourceName = 'user';
/**
* The parsing function used to turn a $http
* response into a "user".
*
* Can be swapped with another parsing function
* using
*
* angular.module('myModule', ['Devise']).
* config(function(AuthProvider) {
* AuthProvider.parse(function(response) {
* return new User(response.data);
* });
* });
*/
var _parse = function(response) {
return response.data;
};
// A helper function that will setup the ajax config
// and merge the data key if provided
function httpConfig(action, data, additionalConfig) {
var config = {
method: methods[action].toLowerCase(),
url: paths[action]
};
if (data) {
if (resourceName) {
config.data = {};
config.data[resourceName] = data;
} else {
config.data = data;
}
}
angular.extend(config, additionalConfig);
return config;
}
// A helper function to define our configure functions.
// Loops over all properties in obj, and creates a get/set
// method for [key + suffix] to set that property on obj.
function configure(obj, suffix) {
angular.forEach(obj, function(v, action) {
this[action + suffix] = function(param) {
if (param === undefined) {
return obj[action];
}
obj[action] = param;
return this;
};
}, this);
}
configure.call(this, methods, 'Method');
configure.call(this, paths, 'Path');
// The baseUrl config function
this.baseUrl = function(value) {
if (value === undefined) {
return baseUrl;
}
baseUrl = value;
return this;
};
// The resourceName config function
this.resourceName = function(value) {
if (value === undefined) {
return resourceName;
}
resourceName = value;
return this;
};
// The parse configure function.
this.parse = function(fn) {
if (typeof fn !== 'function') {
return _parse;
}
_parse = fn;
return this;
};
// Creates a function that always
// returns a given arg.
function constant(arg) {
return function() {
return arg;
};
}
this.$get = function($q, $http, $rootScope) {
// Our shared save function, called
// by `then`s.
function save(user) {
service._currentUser = user;
return user;
}
// A reset that saves null for currentUser
function reset() {
save(null);
service._promise = null;
}
function broadcast(name) {
return function(data) {
$rootScope.$broadcast('devise:' + name, data);
return data;
};
}
var service = {
/**
* The Auth service's current user.
* This is shared between all instances of Auth
* on the scope.
*/
_currentUser: null,
/**
* The Auth service's parsing function.
* Defaults to the parsing function set in the provider,
* but may also be overwritten directly on the service.
*/
parse: _parse,
/**
* The Auth service's current promise
* This is shared between all instances of Auth
* on the scope.
*/
_promise: null,
/* reset promise and current_user, after call this method all
* xhr request will be reprocessed when they will be call
*/
reset: function(){
reset();
service.currentUser();
},
/**
* A login function to authenticate with the server.
* Keep in mind, credentials are sent in plaintext;
* use a SSL connection to secure them. By default,
* `login` will POST to '/users/sign_in.json'.
*
* The path and HTTP method used to login are configurable
* using
*
* angular.module('myModule', ['Devise']).
* config(function(AuthProvider) {
* AuthProvider.loginPath('path/on/server.json');
* AuthProvider.loginMethod('GET');
* });
*
* @param {Object} [creds] A hash of user credentials.
* @param {Object} [config] Optional, additional config which
* will be added to http config for underlying
* $http.
* @returns {Promise} A $http promise that will be resolved or
* rejected by the server.
*/
login: function(creds, config) {
var withCredentials = arguments.length > 0,
loggedIn = service.isAuthenticated();
creds = creds || {};
return $http(httpConfig('login', creds, config))
.then(service.parse)
.then(save)
.then(function(user) {
if (withCredentials && !loggedIn) {
return broadcast('new-session')(user);
}
return user;
})
.then(broadcast('login'));
},
/**
* A logout function to de-authenticate from the server.
* By default, `logout` will DELETE to '/users/sign_out.json'.
*
* The path and HTTP method used to logout are configurable
* using
*
* angular.module('myModule', ['Devise']).
* config(function(AuthProvider) {
* AuthProvider.logoutPath('path/on/server.json');
* AuthProvider.logoutMethod('GET');
* });
* @param {Object} [config] Optional, additional config which
* will be added to http config for underlying
* $http.
* @returns {Promise} A $http promise that will be resolved or
* rejected by the server.
*/
logout: function(config) {
var returnOldUser = constant(service._currentUser);
return $http(httpConfig('logout', undefined, config))
.then(reset)
.then(returnOldUser)
.then(broadcast('logout'));
},
/**
* A register function to register and authenticate
* with the server. Keep in mind, credentials are sent
* in plaintext; use a SSL connection to secure them.
* By default, `register` will POST to '/users.json'.
*
* The path and HTTP method used to login are configurable
* using
*
* angular.module('myModule', ['Devise']).
* config(function(AuthProvider) {
* AuthProvider.registerPath('path/on/server.json');
* AuthProvider.registerMethod('GET');
* });
*
* @param {Object} [creds] A hash of user credentials.
* @param {Object} [config] Optional, additional config which
* will be added to http config for underlying
* $http.
* @returns {Promise} A $http promise that will be resolved or
* rejected by the server.
*/
register: function(creds, config) {
creds = creds || {};
return $http(httpConfig('register', creds, config))
.then(service.parse)
.then(save)
.then(broadcast('new-registration'));
},
/**
* A function to send the reset password instructions to the
* user email.
* By default, `sendResetPasswordInstructions` will POST to '/users/password.json'.
*
* The path and HTTP method used to send instructions are configurable
* using
*
* angular.module('myModule', ['Devise']).
* config(function(AuthProvider) {
* AuthProvider.sendResetPasswordInstructionsPath('path/on/server.json');
* AuthProvider.sendResetPasswordInstructionsMethod('POST');
* });
*
* @param {Object} [creds] A hash containing user email.
* @returns {Promise} A $http promise that will be resolved or
* rejected by the server.
*/
sendResetPasswordInstructions: function(creds, config) {
creds = creds || {};
return $http(httpConfig('sendResetPasswordInstructions', creds, config))
.then(service.parse)
.then(broadcast('send-reset-password-instructions-successfully'));
},
/**
* A reset function to reset user password.
* By default, `resetPassword` will PUT to '/users/password.json'.
*
* The path and HTTP method used to reset password are configurable
* using
*
* angular.module('myModule', ['Devise']).
* config(function(AuthProvider) {
* AuthProvider.resetPasswordPath('path/on/server.json');
* AuthProvider.resetPasswordMethod('POST');
* });
*
* @param {Object} [creds] A hash containing password, password_confirmation and reset_password_token.
* @returns {Promise} A $http promise that will be resolved or
* rejected by the server.
*/
resetPassword: function(creds) {
creds = creds || {};
return $http(httpConfig('resetPassword', creds))
.then(service.parse)
.then(save)
.then(broadcast('reset-password-successfully'));
},
/**
* A helper function that will return a promise with the currentUser.
* Three different outcomes can happen:
* 1. Auth has authenticated a user, and will resolve with it
* 2. Auth has not authenticated a user but the server has an
* authenticated session, Auth will attempt to retrieve that
* session and resolve with its user.
* 3. Neither Auth nor the server has an authenticated session,
* and will reject with an unauthenticated error.
*
* @returns {Promise} A $http promise that will be resolved or
* rejected by the server.
*/
currentUser: function() {
if (service.isAuthenticated()) {
return $q.when(service._currentUser);
}
if(service._promise === null){
service._promise = service.login();
}
return service._promise;
},
/**
* A helper function to determine if a currentUser is present.
*
* @returns Boolean
*/
isAuthenticated: function(){
return !!service._currentUser;
}
};
return service;
};
});