Guest User

Untitled

a guest
Apr 9th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. var LoginController = {
  2. errors: [],
  3. getUser: function() {
  4. return document.getElementById("login_username").value;
  5. },
  6. getPassword: function() {
  7. return document.getElementById("login_password").value;
  8. },
  9. validateEntry: function(user, pw) {
  10. user = user || this.getUser();
  11. pw = pw || this.getPassword();
  12. if (!(user && pw)) {
  13. return this.failure("Please enter a username and password!");
  14. } else if (pw.length < 5) {
  15. return this.failure("Password must be 5+ characters!");
  16. }
  17.  
  18. return true;
  19. },
  20. showDialog: function(title, msg) {
  21. // display success message to user in dialog
  22. },
  23. failure: function(err) {
  24. this.errors.push(err);
  25. this.showDialog("Error", "Login invalid" + err);
  26. }
  27. }
  28.  
  29. var AuthController = Object.create(LoginController);
  30.  
  31. AuthController.errors = [];
  32. AuthController.checkAuth = function() {
  33. var user = this.getUser();
  34. var pw = this.getPassword();
  35.  
  36. if (this.validateEntry(user, pw)) {
  37. this.server("/check-auth", {
  38. user: user,
  39. pw: pw
  40. }).then(this.accepted.bind(this))
  41. .fail(this.rejected.bind(this));
  42. }
  43. };
  44. AuthController.server = function(url, data) {
  45. return $.ajax({
  46. url: url,
  47. data: data
  48. });
  49. };
  50. AuthController.accepted = function() {
  51. this.showDialog("Success", "Authenticated!");
  52. }
  53. AuthController.rejected = function(err) {
  54. this.failure("Auth Failed! " + err);
  55. }
  56.  
  57.  
  58. AuthController.checkAuth();
Add Comment
Please, Sign In to add comment