Advertisement
Guest User

loginpage

a guest
Jun 13th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1. <!--
  2. A comment describing this element
  3.  
  4. Example:
  5.  
  6. <my-elem></my-elem>
  7.  
  8. Example:
  9.  
  10. <my-elem>
  11. <h2>Hello my-elem</h2>
  12. </my-elem>
  13.  
  14. @demo demo/index.html
  15. -->
  16.  
  17. <dom-module id="login-page">
  18. <template>
  19. <style>
  20. :host {
  21. display: block;
  22. }
  23. .container{
  24. background-color: var(--default-primary-color);
  25. @apply(--layout-vertical); @apply(--layout-fit);@apply(--layout-center-justified);@apply(--layout-center);
  26. }
  27. paper-card{
  28. background-color: white; min-width: 300px;margin-bottom:15px;
  29. }
  30. paper-card div{
  31. padding: 15px 30px;
  32. @apply(--layout-vertical);
  33. @apply(--layout-center);
  34. }
  35. #loginBtn{
  36. background-color: lightgreen;
  37. width: 100%;
  38. margin-top: 30px;
  39. margin-bottom: 15px;
  40. --paper-button-ink-color:white;
  41. }
  42. paper-input{
  43. width: 100%;
  44. }
  45. .error-container{
  46. padding: 5px;
  47. color:red;
  48. text-align: center;
  49. }
  50. </style>
  51. <div class="container">
  52. <paper-card>
  53. <div>
  54. <h1>SE2 TP CMS</h1>
  55. <paper-input
  56. error-message="Este campo es requerido"
  57. required id="username"
  58. label="Nombre de usuario">
  59. </paper-input>
  60.  
  61. <paper-input
  62. error-message="Este campo es requerido"
  63. required type="password"
  64. id="password" label="Contraseña"></paper-input>
  65.  
  66. <paper-button id="loginBtn">Ingresar</paper-button>
  67. </div>
  68. </paper-card>
  69. <paper-card hidden$="[[!hasErrors]]" class="error-container"><span>{{errorMsg}}</span></paper-card>
  70.  
  71. <iron-ajax
  72. id="ironAjax"
  73. handle-as="json"
  74. on-response="handleResponseFn"
  75. on-error="handleError"></iron-ajax>
  76. </div>
  77. </template>
  78. <script>
  79. Polymer({
  80. is: 'login-page',
  81. ready: function () {
  82. this.listen(this.$.loginBtn, 'click', 'onClickIngresar');
  83. this.hasErrors = false;
  84. this.errorMsg = '';
  85. this.thisAjax = this.$.ironAjax;
  86. },
  87. validateInputs: function () {
  88.  
  89. if(this.$.username.validate() === true && this.$.password.validate() === true){
  90. return true;
  91. }else{
  92. return false;
  93. }
  94.  
  95. },
  96. onPageVisible: function () {
  97. console.log('pagina login');
  98. this.hasErrors = false;this.errorMsg = '';
  99. this.$.username.value= '';
  100. this.$.password.value= '';
  101. },
  102. onClickIngresar: function () {
  103. var that = this;
  104.  
  105. if(this.validateInputs() === false) return;
  106.  
  107. var body = {};
  108. body.username = this.$.username.value;
  109. body.password = this.$.password.value;
  110.  
  111. this.thisAjax.url = app.apiBaseUrl + 'users/login';
  112. this.thisAjax.method = 'POST';
  113. this.thisAjax.contentType = 'application/json';
  114. this.thisAjax.withCredentials = true;
  115. this.thisAjax.body = JSON.parse(JSON.stringify(body));
  116. this.handleResponseFn = this.ajaxLogin;
  117. this.thisAjax.generateRequest();
  118.  
  119. },
  120. ajaxLogin: function (e) {
  121. var that = this;
  122. if(e.detail.response && e.detail.status === 200){ //si hay respuesta y es 200 OK
  123. var sid = e.detail.response.id;
  124. that.getMeData(function (errStatus, meData) {
  125. if(errStatus){
  126. that.hasErrors = true;
  127. }else{
  128. app.$.userLs.setData({
  129. name: meData.name,
  130. id: meData.id,
  131. profilePicUrl : meData.profilePicUrl,
  132. sid : sid
  133. });
  134. app.profile = app.$.userLs.data;
  135. page('/');
  136. }
  137. });
  138.  
  139. }
  140. },
  141. handleError: function (e) {
  142. if(e.detail.request.status === 401){
  143. this.hasErrors = true;
  144. this.errorMsg = 'El usuario y/o contraseña no son correctos';
  145. }
  146. },
  147. getMeData: function (callback) {
  148. var that = this;
  149. var request = new XMLHttpRequest();
  150.  
  151. request.open('GET', app.apiBaseUrl + 'users/me', true);
  152. request.withCredentials = true;
  153. request.setRequestHeader("Content-type", "application/json");
  154.  
  155. request.onload = function() {
  156. console.log('request.responseText', request.responseText);
  157. if (request.status >= 200 && request.status < 400) {
  158.  
  159. // Success!
  160. callback(null, JSON.parse(request.responseText));
  161. } else {
  162. // We reached our target server, but it returned an error
  163. callback(request.status, JSON.parse(request.responseText));
  164. }
  165. };
  166.  
  167. request.onerror = function() {
  168. console.log("error al traer datos de usuario");
  169. callback(request.status, JSON.parse(request.responseText));
  170. // There was a connection error of some sort
  171. };
  172.  
  173. request.send();
  174. },
  175. logOut: function () {
  176. this.thisAjax.url = app.apiBaseUrl + 'users/logout';
  177. this.thisAjax.method = 'GET';
  178. this.thisAjax.contentType = 'application/json';
  179. this.thisAjax.withCredentials = true;
  180. this.handleResponseFn = function () {
  181. app.$.userLs.resetData();
  182. app.$.profile = app.$.userLs.data;
  183. page('/login');
  184. };
  185. this.thisAjax.generateRequest();
  186. }
  187. });
  188. </script>
  189. </dom-module>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement