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

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