Guest User

Untitled

a guest
Sep 12th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. So I have a view, it's the end of a login page, 'login' being my controller
  2.  
  3. <!-- ... -->
  4.  
  5. <button type="submit" class="col-sm-6 col-sm-12" ng-disabled="login_form.$invalid" class="btn btn-primary">Log in</button>
  6.  
  7. <div class="col-sm-12 alert alert-warning" ng-show="login.failed()">
  8. Bad username, email or password
  9. </div>
  10. <div class="col-sm-12 alert alert-warning" ng-show="login.error()">
  11. Something went wrong... The guru's probably meditating.
  12. </div>
  13.  
  14. /* Login Controller definition */
  15. /* Here I'm injecting an 'auth' service to implement RESTAPI calls */
  16.  
  17. ar.module('frontendApp')
  18. .controller('LoginCtrl', ['$scope', 'auth', function ($scope, auth) {
  19. var self = this;
  20.  
  21. this._response = undefined;
  22. this.remember_me = false;
  23. this._error = false;
  24.  
  25. /* Called on a button click */
  26. this.login = function () {
  27. self._response = undefined;
  28. self._error = false;
  29. /* I'm trying to auth, then set _response with the answer */
  30. auth.login(self.user, self.remember_me).then(function (answer) {
  31. self._response = answer;
  32. }, function (data) {
  33. console.error('LoginCtrl: error caught. Something went wrong.');
  34. console.error('data', data);
  35. self._error = true;
  36. });
  37. };
  38.  
  39. /* These are the methods called by warnings, they use self._response */
  40. this.failed = function () {
  41. return self._response != undefined && !self._response.authentified;
  42. };
  43.  
  44. this.error = function () {
  45. return self._error;
  46. };
  47.  
  48. }]);
  49.  
  50. /* This is my 'auth' service */
  51.  
  52. angular.module('frontendApp')
  53. .service('auth', ['$http', function ($http) {
  54. var self = this;
  55.  
  56. this.login = function (user, remember_me) {
  57. remember_me = remember_me === undefined ? false : remember_me;
  58.  
  59. user.username = user.login; /* TODO: useless once converted to express - test only */
  60. /* Return a promise that is solved if the response from the server makes sense. */
  61. return new Promise(function (accept, reject) {
  62. try {
  63. $http.post(SERVER_PREFIX + 'login', user).then(function (data) { // On accept
  64. expect(data.data).to.contain.keys('id', 'userId');
  65. accept({
  66. authentified: true,
  67. user: user,
  68. answer: data.data
  69. });
  70. }, function (data) { // On rejected
  71. expect(data.data).to.contain.keys('error');
  72. expect(data.data.error).to.contain.keys('status', 'code');
  73. expect(data.data.error.code).to.equal('LOGIN_FAILED');
  74. accept({
  75. authentified: false,
  76. user: user,
  77. answer: data.data
  78. });
  79. });
  80. }
  81. catch (error) {
  82. if (error instanceof AssertionError) reject(error.data); // Bad data sent by the server
  83. else throw error;
  84. }
  85. });
  86. };
  87. }]);
Advertisement
Add Comment
Please, Sign In to add comment