Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

auth.js 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. function clearAuthState() {
  2. auth.user = ''; // Login name
  3. auth.name = ''; // Full name
  4. auth.mail = ''; // Full mail address
  5. auth.type = ''; // type of login (domain specific)
  6. auth.roles = {}; // Role authorizations e.g. {user: true}
  7. auth.gender = ''; // Gender (e.g. Frau)
  8. auth.abos = []; // Followed tags
  9. auth.bookmarks = []; // Ids of bookmarks
  10. }
  11. clearAuthState();
  12. function fillAuthState(state) {
  13. // Shallow copy
  14. if (auth === undefined) {
  15. auth = state;
  16. }
  17. for (e in auth) {
  18. auth[e] = state[e];
  19. }
  20. }
  21. function updateUserInDB() {
  22. $.ajax({
  23. url: "api/usr",
  24. data: {
  25. _id: auth.user,
  26. name: auth.name,
  27. mail: auth.mail,
  28. type: auth.type,
  29. gender: auth.gender,
  30. roles: JSON.stringify(auth.roles),
  31. abos: JSON.stringify(auth.abos),
  32. bookmarks: JSON.stringify(auth.bookmarks),
  33. },
  34. method: "PUT"
  35. }).done(successful_save).fail(failed_save);
  36. function successful_save(res) {
  37. console.info("User-save: Successful.");
  38. console.info(res);
  39. }
  40. function failed_save(err) {
  41. console.info("User-save: Failed.");
  42. console.error(err);
  43. }
  44. }
  45. // Login component: Login panel (if not logged in) or Logout element (if logged in)
  46. Vue.component('login-panel', {
  47. template: `
  48. <div class="modal-card" style="width: auto">
  49. <header class="modal-card-head">
  50. <p class="modal-card-title">Login (Ohmportal)</p>
  51. </header>
  52. <section class="modal-card-body">
  53. <b-field>
  54. <b-input
  55. v-model=user
  56. placeholder="User"
  57. :value="user"
  58. required>
  59. </b-input>
  60. </b-field>
  61. <b-field>
  62. <b-input
  63. v-model=pwd
  64. type="password"
  65. placeholder="Password"
  66. :value="pwd"
  67. @change="pwd_changed"
  68. @keyup.enter.native="login"
  69. required>
  70. </b-input>
  71. </b-field>
  72. <b-checkbox>Eingeloggt bleiben</b-checkbox>
  73. <div v-if="error" :key="error" style="color:red;"> Versuche es nochmal.</div>
  74. </section>
  75. <footer class="modal-card-foot">
  76. <button class="button" type="button" @click.prevent="$parent.close()">Close</button>
  77. <button class="button is-primary" @click.prevent="login">Login</button>
  78. </footer>
  79. </div>`,
  80. data: function () {
  81. return {
  82. user: '',
  83. pwd: '',
  84. error: false,
  85. };
  86. },
  87. // TODO: Check if enough for session cookie re-login; checkbox
  88. // Doesn't work
  89. beforeMount: function() {
  90. this.checkData();
  91. },
  92. methods: {
  93. closeLoginPanel: function() {
  94. this.$emit('close-login-panel');
  95. },
  96. showLoginPanel: function() {
  97. this.$emit('show-login-panel');
  98. },
  99. pwd_changed: function(evt) {
  100. // Only required for PasswordInsert Apps etc. who send only(!) change events
  101. this.pwd = e.target.value;
  102. // Alternative call the input event
  103. //e.target.dispatchEvent(new Event('input'));
  104. },
  105. login: function() {
  106. clearAuthState();
  107. $.ajax({
  108. url: "api/login",
  109. data: {
  110. user: this.user,
  111. pwd: this.pwd
  112. },
  113. method: "POST"
  114. }).done(successful_login).fail(failed_login);
  115. function successful_login(resData) {
  116. fillAuthState(resData);
  117. // updateUserInDB();
  118. auth.abos = [];
  119. auth.bookmarks = [];
  120. console.info("Correct credentials");
  121. this.closeLoginPanel;
  122. //console.log(resData);
  123. router.push('/home');
  124. }
  125. function failed_login(err) {
  126. console.info("Wrong credentials");
  127. this.error=true;
  128. this.showLoginPanel;
  129. console.log("error: " + err.responseText);
  130. console.log(err);
  131. }
  132. },
  133. checkData: function() {
  134. clearAuthState();
  135. $.ajax({
  136. url: "api/login",
  137. data: undefined,
  138. method: "POST"
  139. }).done(successful_login).fail(failed_login);
  140. function successful_login(res) {
  141. console.info("Re-Auth: Correct credentials");
  142. this.closeLoginPanel;
  143. vueForceRender('key');
  144. //console.log(res);
  145. router.push('/home')
  146. }
  147. function failed_login(err) {
  148. console.info("Re-Auth: Wrong credentials");
  149. this.error=true;
  150. this.showLoginPanel;
  151. console.log(err);
  152. }
  153. },
  154. },
  155. });