Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 51 additions & 32 deletions app/scripts/services/djangoAuth.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';
(function() {
'use strict';

angular.module('angularDjangoRegistrationAuthApp')
.service('djangoAuth', function djangoAuth($q, $http, $cookies, $rootScope) {
angular.module('angularDjangoRegistrationAuthApp')
.service('djangoAuth', function djangoAuth($q, $http, $cookies, $rootScope) {
// AngularJS will instantiate a singleton by calling "new" on this function
var service = {
/* START CUSTOMIZATION HERE */
Expand All @@ -11,6 +12,8 @@ angular.module('angularDjangoRegistrationAuthApp')
// Set use_session to true to use Django sessions to store security token.
// Set use_session to false to store the security token locally and transmit it as a custom header.
'use_session': true,
// django-rest-auth settings variable
'OLD_PASSWORD_FIELD_ENABLED': true,
/* END OF CUSTOMIZATION */
'authenticated': null,
'authPromise': null,
Expand All @@ -23,10 +26,10 @@ angular.module('angularDjangoRegistrationAuthApp')
params = args.params || {}
args = args || {};
var deferred = $q.defer(),
url = this.API_URL + args.url,
method = args.method || "GET",
params = params,
data = args.data || {};
url = this.API_URL + args.url,
method = args.method || "GET",
params = params,
data = args.data || {};
// Fire the request, as configured.
$http({
url: url,
Expand All @@ -36,10 +39,10 @@ angular.module('angularDjangoRegistrationAuthApp')
params: params,
data: data
})
.success(angular.bind(this,function(data, status, headers, config) {
.success(angular.bind(this,function onRequestSuccess(data, status, headers, config) {
deferred.resolve(data, status);
}))
.error(angular.bind(this,function(data, status, headers, config) {
.error(angular.bind(this,function onRequestError(data, status, headers, config) {
console.log("error syncing with: " + url);
// Set request status
if(data){
Expand All @@ -64,7 +67,17 @@ angular.module('angularDjangoRegistrationAuthApp')
}));
return deferred.promise;
},
'register': function(username,password1,password2,email,more){
'onLoginSuccess': function onLoginSuccess(data) {
var djangoAuth = this;
if(!djangoAuth.use_session){
$http.defaults.headers.common.Authorization = 'Token ' + data.key;
$cookies.token = data.key;
}
djangoAuth.authenticated = true;
$rootScope.$broadcast("djangoAuth.logged_in", data);
},
'register': function register(username,password1,password2,email,more){
var djangoAuth = this;
var data = {
'username':username,
'password1':password1,
Expand All @@ -75,10 +88,12 @@ angular.module('angularDjangoRegistrationAuthApp')
return this.request({
'method': "POST",
'url': "/registration/",
'data' :data
'data': data
}).then(function(data){
djangoAuth.onLoginSuccess(data);
});
},
'login': function(username,password){
'login': function login(username,password){
var djangoAuth = this;
return this.request({
'method': "POST",
Expand All @@ -88,15 +103,10 @@ angular.module('angularDjangoRegistrationAuthApp')
'password':password
}
}).then(function(data){
if(!djangoAuth.use_session){
$http.defaults.headers.common.Authorization = 'Token ' + data.key;
$cookies.token = data.key;
}
djangoAuth.authenticated = true;
$rootScope.$broadcast("djangoAuth.logged_in", data);
djangoAuth.onLoginSuccess(data);
});
},
'logout': function(){
'logout': function logout(){
var djangoAuth = this;
return this.request({
'method': "POST",
Expand All @@ -108,17 +118,24 @@ angular.module('angularDjangoRegistrationAuthApp')
$rootScope.$broadcast("djangoAuth.logged_out");
});
},
'changePassword': function(password1,password2){
'changePassword': function changePassword(password1,password2,old_password){
var data = {
'new_password1': password1,
'new_password2': password2
};
if (old_password === undefined) {
old_password = '';
}
if (this.OLD_PASSWORD_FIELD_ENABLED === true) {
data['old_password'] = old_password;
}
return this.request({
'method': "POST",
'url': "/password/change/",
'data':{
'new_password1':password1,
'new_password2':password2
}
'data': data
});
},
'resetPassword': function(email){
'resetPassword': function resetPassword(email){
return this.request({
'method': "POST",
'url': "/password/reset/",
Expand All @@ -127,27 +144,27 @@ angular.module('angularDjangoRegistrationAuthApp')
}
});
},
'profile': function(){
'profile': function profile(){
return this.request({
'method': "GET",
'url': "/user/"
});
},
'updateProfile': function(data){
'updateProfile': function updateProfile(data){
return this.request({
'method': "PATCH",
'url': "/user/",
'data':data
});
},
'verify': function(key){
'verify': function verify(key) {
return this.request({
'method': "POST",
'url': "/registration/verify-email/",
'data': {'key': key}
});
},
'confirmReset': function(uid,token,password1,password2){
'confirmReset': function confirmReset(uid,token,password1,password2){
return this.request({
'method': "POST",
'url': "/password/reset/confirm/",
Expand All @@ -159,7 +176,7 @@ angular.module('angularDjangoRegistrationAuthApp')
}
});
},
'authenticationStatus': function(restrict, force){
'authenticationStatus': function authenticationStatus(restrict, force){
// Set restrict to true to reject the promise if not logged in
// Set to false or omit to resolve when status is known
// Set force to true to ignore stored value and query API
Expand Down Expand Up @@ -197,12 +214,14 @@ angular.module('angularDjangoRegistrationAuthApp')
}
return getAuthStatus.promise;
},
'initialize': function(url, sessions){
'initialize': function initialize(url, sessions){
this.API_URL = url;
this.use_session = sessions;
return this.authenticationStatus();
}

}
return service;
});
});

})();
90 changes: 46 additions & 44 deletions app/scripts/services/validate.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,52 @@
'use strict';
(function() {
'use strict';

angular.module('angularDjangoRegistrationAuthApp')
.service('Validate', function Validate() {
return {
'message': {
'minlength': 'This value is not long enough.',
'maxlength': 'This value is too long.',
'email': 'A properly formatted email address is required.',
'required': 'This field is required.'
},
'more_messages': {
'demo': {
'required': 'Here is a sample alternative required message.'
}
},
'check_more_messages': function(name,error){
return (this.more_messages[name] || [])[error] || null;
},
validation_messages: function(field,form,error_bin){
var messages = [];
for(var e in form[field].$error){
if(form[field].$error[e]){
var special_message = this.check_more_messages(field,e);
if(special_message){
messages.push(special_message);
}else if(this.message[e]){
messages.push(this.message[e]);
}else{
messages.push("Error: " + e)
angular.module('angularDjangoRegistrationAuthApp')
.service('Validate', function Validate() {
return {
'message': {
'minlength': 'This value is not long enough.',
'maxlength': 'This value is too long.',
'email': 'A properly formatted email address is required.',
'required': 'This field is required.'
},
'more_messages': {
'demo': {
'required': 'Here is a sample alternative required message.'
}
},
'check_more_messages': function(name,error){
return (this.more_messages[name] || [])[error] || null;
},
validation_messages: function(field,form,error_bin){
var messages = [];
for(var e in form[field].$error){
if(form[field].$error[e]){
var special_message = this.check_more_messages(field,e);
if(special_message){
messages.push(special_message);
}else if(this.message[e]){
messages.push(this.message[e]);
}else{
messages.push("Error: " + e)
}
}
}
}
var deduped_messages = [];
angular.forEach(messages, function(el, i){
if(deduped_messages.indexOf(el) === -1) deduped_messages.push(el);
});
if(error_bin){
error_bin[field] = deduped_messages;
}
},
'form_validation': function(form,error_bin){
for(var field in form){
if(field.substr(0,1) != "$"){
this.validation_messages(field,form,error_bin);
var deduped_messages = [];
angular.forEach(messages, function(el, i){
if(deduped_messages.indexOf(el) === -1) deduped_messages.push(el);
});
if(error_bin){
error_bin[field] = deduped_messages;
}
},
'form_validation': function(form,error_bin){
for(var field in form){
if(field.substr(0,1) != "$"){
this.validation_messages(field,form,error_bin);
}
}
}
}
}
});
});
})();
7 changes: 4 additions & 3 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ module.exports = function(config) {
'app/bower_components/angular-route/angular-route.js',
'app/scripts/*.js',
'app/scripts/**/*.js',
'test/mock/**/*.js',
'test/spec/**/*.js'
// 'test/mock/**/*.js',
// 'test/spec/**/*.js'
'test/spec/services/djangoAuth.js'
],

// list of files / patterns to exclude
Expand All @@ -35,7 +36,7 @@ module.exports = function(config) {


// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,
autoWatch: true,


// Start these browsers, currently available:
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@
"grunt-rev": "~0.1.0",
"grunt-svgmin": "~0.2.0",
"grunt-usemin": "~2.0.0",
"jasmine": "^2.4.1",
"jshint-stylish": "~0.1.3",
"karma": "^0.13.19",
"karma-chrome-launcher": "^0.2.2",
"karma-jasmine": "^0.3.7",
"load-grunt-tasks": "~0.2.0",
"time-grunt": "~0.2.1"
},
Expand Down
Loading