Skip to content

Commit

Permalink
Return 400 status and show an error message, see #17
Browse files Browse the repository at this point in the history
  • Loading branch information
ocean90 committed Jul 14, 2014
1 parent 2b9f77c commit e135fe3
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 18 deletions.
6 changes: 5 additions & 1 deletion app/resources/users/index.js
Expand Up @@ -26,7 +26,11 @@ users.post('/', function(req, res) {

user.save(function(err, user) {
if (err) {
res.json(500, err);
if (err.name === 'ValidationError') {
res.json(400, err);
} else {
res.json(500, err);
}
} else {
acl.setRole(user.username, user.role, function(err) {
if (err) {
Expand Down
5 changes: 3 additions & 2 deletions public/application.js
Expand Up @@ -185,7 +185,7 @@ app.config(function($routeProvider, $locationProvider) {
$locationProvider.html5Mode(true);
});

app.run(function($rootScope, AuthService, AUTH_EVENTS, $location) {
app.run(function($rootScope, AuthService, AUTH_EVENTS, Session, $location) {

AuthService.refresh().then(function() {
$rootScope.$broadcast(AUTH_EVENTS.loginSuccess);
Expand All @@ -198,8 +198,9 @@ app.run(function($rootScope, AuthService, AUTH_EVENTS, $location) {
return;
}

AuthService.refresh().then(function() {
AuthService.refresh().then(function(res) {
// Authentifizierung war erfolgreich
Session.create(Date.now(), res.data);
$rootScope.$broadcast(AUTH_EVENTS.loginRefreshed);
}, function() {
// Authentifizierung ist fehlgeschlagen, User abmelden
Expand Down
3 changes: 2 additions & 1 deletion public/controllers/LoginController.js
Expand Up @@ -8,7 +8,8 @@ angular.module('levelPad').controller('LoginController', function($scope, $rootS

$scope.login = function($event) {
$($event.target).find('button[type=submit]').button('loading');
AuthService.login($scope.credentials).then(function() {
AuthService.login($scope.credentials).then(function(res) {
Session.create(Date.now(), res.data);
$rootScope.$broadcast(AUTH_EVENTS.loginSuccess);
$rootScope.$broadcast(AUTH_EVENTS.loginRefreshed);
}, function() {
Expand Down
3 changes: 2 additions & 1 deletion public/controllers/LogoutController.js
@@ -1,8 +1,9 @@
angular.module('levelPad').controller('LogoutController', function($scope, $rootScope, $location, AUTH_EVENTS, AuthService) {
angular.module('levelPad').controller('LogoutController', function($scope, $rootScope, $location, AUTH_EVENTS, Session, AuthService) {
'use strict';

$scope.logout = function() {
AuthService.logout().then(function() {
Session.destroy();
$rootScope.$broadcast(AUTH_EVENTS.logoutSuccess);
}, function() {
$rootScope.$broadcast(AUTH_EVENTS.logoutFailed);
Expand Down
3 changes: 2 additions & 1 deletion public/controllers/SignupController.js
Expand Up @@ -15,7 +15,8 @@ angular.module('levelPad').controller('SignupController', function($scope, $root
$scope.user.username = Session.user.username;

$($event.target).find('button[type=submit]').button('loading');
AuthService.signup($scope.user).then(function() {
AuthService.signup($scope.user).then(function(res) {
Session.create(Date.now(), res.data);
$rootScope.$broadcast(AUTH_EVENTS.signupSuccess);
$rootScope.$broadcast(AUTH_EVENTS.loginRefreshed);
}, function() {
Expand Down
14 changes: 2 additions & 12 deletions public/services/AuthService.js
Expand Up @@ -5,18 +5,13 @@ angular.module('levelPad').factory('AuthService', function ($http, Session) {
method: 'POST',
url: '/api/login',
data: credentials
})
.then(function(res) {
Session.create(Date.now(), res.data);
});
},

refresh: function() {
return $http({
method: 'GET',
url: '/api/users/me'
}).then(function(res) {
Session.create(Date.now(), res.data);
});
},

Expand All @@ -25,26 +20,21 @@ angular.module('levelPad').factory('AuthService', function ($http, Session) {
method: 'POST',
url: '/api/users',
data: user
}).then(function(res) {
Session.create(Date.now(), res.data);
});
},

logout: function() {
return $http({
method: 'POST',
url: '/api/logout'
})
.then(function() {
Session.destroy();
});
},

isAuthenticated: function () {
isAuthenticated: function() {
return !!Session.user.id;
},

isAuthorized: function (authorizedRoles) {
isAuthorized: function(authorizedRoles) {
if (!angular.isArray(authorizedRoles)) {
authorizedRoles = [authorizedRoles];
}
Expand Down

0 comments on commit e135fe3

Please sign in to comment.