Advertisement
Guest User

Untitled

a guest
Sep 29th, 2016
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. <form role="form" ng-submit="controller.register()">
  2. <div ng-show="controller.user.username.length > 20">Username must be 20 characters or less.</div>
  3. <br>
  4. <div ng-show="controller.user.username = null">Username is blank.</div>
  5. <br>
  6. <input type="text" name="username" ng-model="controller.user.username" placeholder="Choose a username."/>
  7. <br/>
  8. <div ng-show="controller.user.password = null">Password is blank.</div>
  9. <br>
  10. <input type="password" name="password" ng-model="controller.user.password" placeholder="Choose a password."/>
  11. <input class="button" type="submit" value="Submit">
  12. <div>{{controller.msg}}</div>
  13. </form>
  14.  
  15. wishingWell.controller('register', function($http, $location, $rootScope) {
  16.  
  17. var noerrors = false;
  18. var self = this;
  19. self.user = {};
  20. self.msg = undefined;
  21.  
  22.  
  23. if (self.user.username != null && self.user.username.length <= 20 && self.user.password != null) {
  24. noerrors = true;
  25. }
  26.  
  27. self.register = function() {
  28.  
  29. if (noerrors) {
  30. $http.post('register', user).success(function (response) {
  31.  
  32. if (response.data.code == "200") {
  33. $rootScope.authenticated = true;
  34. $location.path("/user-home");
  35. } else {
  36. msg = response.data.msg;
  37. $location.path("/sign-up");
  38. }
  39.  
  40. });
  41. } else {
  42. $location.path("/sign-up");
  43. }
  44. };
  45. });
  46.  
  47. wishingWell.controller('navigation',
  48.  
  49. function($rootScope, $http, $location) {
  50.  
  51. var self = this;
  52.  
  53. var authenticate = function(credentials, callback) {
  54.  
  55. var headers = credentials ? {authorization : "Basic "
  56. + btoa(credentials.username + ":" + credentials.password)
  57. } : {};
  58.  
  59. $http.get('user', {headers : headers}).then(function(response) {
  60. if (response.data.name) {
  61. $rootScope.authenticated = true;
  62. } else {
  63. $rootScope.authenticated = false;
  64. }
  65. callback && callback();
  66. }, function() {
  67. $rootScope.authenticated = false;
  68. callback && callback();
  69. });
  70.  
  71. }
  72.  
  73. authenticate();
  74. self.credentials = {};
  75. self.login = function() {
  76. authenticate(self.credentials, function() {
  77. if ($rootScope.authenticated) {
  78. $location.path("/");
  79. self.error = false;
  80. } else {
  81. $location.path("/login");
  82. self.error = true;
  83. }
  84. });
  85. };
  86. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement