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.2KB

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