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

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