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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. @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. methods: {
  63. closeLoginPanel: function() {
  64. this.$emit('close-login-panel');
  65. },
  66. showLoginPanel: function() {
  67. this.$emit('show-login-panel');
  68. },
  69. pwd_changed: function(evt) {
  70. // Only required for PasswordInsert Apps etc. who send only(!) change events
  71. this.pwd = e.target.value;
  72. // Alternative call the input event
  73. //e.target.dispatchEvent(new Event('input'));
  74. },
  75. login: function() {
  76. clearAuthState();
  77. $.ajax({
  78. url: "api/login",
  79. data: {
  80. user: this.user,
  81. pwd: this.pwd
  82. },
  83. method: "POST"
  84. }).done(successful_login).fail(failed_login);
  85. function successful_login(resData) {
  86. fillAuthState(resData);
  87. console.info("Correct credentials");
  88. this.closeLoginPanel;
  89. //console.log(resData);
  90. router.push('/home');
  91. }
  92. function failed_login(err) {
  93. console.info("Wrong credentials");
  94. this.showError=true;
  95. this.showLoginPanel;
  96. console.log("error: " + err.responseText);
  97. console.log(err);
  98. }
  99. },
  100. checkData: function() {
  101. clearAuthState();
  102. $.ajax({
  103. url: "api/login",
  104. data: undefined,
  105. method: "POST"
  106. }).done(successful_login).fail(failed_login);
  107. function successful_login(res) {
  108. console.info("Re-Auth: Correct credentials");
  109. this.closeLoginPanel;
  110. //console.log(res);
  111. router.push('/home')
  112. }
  113. function failed_login(err) {
  114. console.info("Re-Auth: Wrong credentials");
  115. this.showError=true;
  116. this.showLoginPanel;
  117. console.log("error: " + err.responseText);
  118. console.log(err);
  119. }
  120. },
  121. logout: function() {
  122. clearAuthState();
  123. $.ajax({ url: "api/logout", method: "POST" });
  124. this.closeLoginPanel;
  125. }
  126. },
  127. });