admin-login.html 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <dom-module id="admin-login">
  2. <style is="custom-style" include="iron-flex iron-positioning"></style>
  3. <style>
  4. paper-card {
  5. display: block;
  6. margin-top: 32px;
  7. margin-left: auto;
  8. margin-right: auto;
  9. max-width: 500px;
  10. }
  11. .msg {
  12. padding: 10px 20px 9px;
  13. margin-bottom: 4px;
  14. }
  15. .msg-error {
  16. background: var(--paper-red-100);
  17. border-left: 3px solid var(--paper-red-500);
  18. color: var(--paper-red-900);
  19. }
  20. .btn {
  21. color: white;
  22. }
  23. .btn-primary {
  24. background-color: var(--paper-blue-300);
  25. }
  26. </style>
  27. <template>
  28. <paper-card heading="Admin Login">
  29. <div class="card-content">
  30. <div class="layout horizontal center-justified">
  31. <paper-spinner id="spinner"></paper-spinner>
  32. </div>
  33. <paper-material class="msg msg-error" hidden="{{!loginError}}">{{errorMessage}}</paper-material>
  34. <form id="form" class="layout vertical" is="iron-form" method="post" action="/api/admin/login">
  35. <paper-input id="username" label="Username (Portal Creds)" type="text" required></paper-input>
  36. <paper-input id="password" label="Password" type="password" required></paper-input>
  37. <paper-button id="submitButton" class="btn btn-primary" raised on-tap="_submit">Login</paper-button>
  38. </form>
  39. </div>
  40. </paper-card>
  41. </template>
  42. <script>
  43. Polymer({
  44. is: 'admin-login',
  45. properties: {
  46. /**
  47. * Error message to display on the failure toast
  48. */
  49. errorMessage: {
  50. type: String
  51. },
  52. /**
  53. * Whether or not the request is in flight
  54. */
  55. inFlight: {
  56. type: Boolean,
  57. value: false,
  58. observer: '_inFlight'
  59. },
  60. /**
  61. * Whether or not there was a login error
  62. */
  63. loginError: {
  64. type: Boolean,
  65. value: false
  66. }
  67. },
  68. attached: function () {
  69. var me = this;
  70. var form = me.$.form;
  71. var usernameInput = me.$.username;
  72. var passwordInput = me.$.password;
  73. //Grab the inputs from the form just before the form is submitted
  74. form.addEventListener('iron-form-presubmit', function (event) {
  75. this.request.contentType = 'application/json';
  76. this.request.body = {username: usernameInput.value, password: passwordInput.value};
  77. });
  78. //When the form is submitted, mark it as in flight
  79. form.addEventListener('iron-form-submit', function (event) {
  80. me.inFlight = true;
  81. });
  82. //On HTTP error, mark an error and set the error message
  83. form.addEventListener('iron-form-error', function (event) {
  84. me.inFlight = false;
  85. me.errorMessage = event.detail.request.response.errors;
  86. me.loginError = true;
  87. });
  88. //When a valid response is recv
  89. form.addEventListener('iron-form-response', function (event) {
  90. me.inFlight = false;
  91. var response = event.detail.response;
  92. //Fire an event so listeners know that a response was recv and was successful
  93. me.fire('admin-login-success', {token: response.token});
  94. });
  95. },
  96. /**
  97. * Helper method for disabling the form
  98. * @private
  99. */
  100. _disableForm: function () {
  101. this.$.username.disabled = true;
  102. this.$.password.disabled = true;
  103. this.$.submitButton.disabled = true;
  104. },
  105. /**
  106. * Helper method for enabling the form
  107. * @private
  108. */
  109. _enableForm: function () {
  110. this.$.username.disabled = false;
  111. this.$.password.disabled = false;
  112. this.$.submitButton.disabled = false;
  113. },
  114. /**
  115. * Observer method for when the request is in flight or not.
  116. * If it is, form is disabled and spinner is active.
  117. * Otherwise, form is enabled and spinner is disabled.
  118. * @private
  119. */
  120. _inFlight: function () {
  121. if (this.inFlight == true) {
  122. this.$.spinner.active = true;
  123. this._disableForm();
  124. }
  125. else {
  126. this.$.spinner.active = false;
  127. this._enableForm();
  128. }
  129. },
  130. /**
  131. * Helper method for submitting the form through the native submit()
  132. * @private
  133. */
  134. _submit: function () {
  135. this.$.form.submit();
  136. }
  137. });
  138. </script>
  139. </dom-module>