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