171 lines
4.8 KiB
JavaScript
171 lines
4.8 KiB
JavaScript
function clearAuthState() {
|
|
auth.user = ''; // Login name
|
|
auth.name = ''; // Full name
|
|
auth.mail = ''; // Full mail address
|
|
auth.type = ''; // type of login (domain specific)
|
|
auth.roles = {}; // Role authorizations e.g. {user: true}
|
|
auth.gender = ''; // Gender (e.g. Frau)
|
|
auth.abos = []; // Followed tags
|
|
auth.bookmarks = []; // Ids of bookmarks
|
|
}
|
|
clearAuthState();
|
|
|
|
function fillAuthState(state) {
|
|
// Shallow copy
|
|
if (auth === undefined) {
|
|
auth = state;
|
|
}
|
|
for (e in auth) {
|
|
auth[e] = state[e];
|
|
}
|
|
}
|
|
|
|
function updateUserInDB() {
|
|
$.ajax({
|
|
url: "api/usr",
|
|
data: {
|
|
_id: auth.user,
|
|
name: auth.name,
|
|
mail: auth.mail,
|
|
type: auth.type,
|
|
gender: auth.gender,
|
|
roles: JSON.stringify(auth.roles),
|
|
abos: JSON.stringify(auth.abos),
|
|
bookmarks: JSON.stringify(auth.bookmarks),
|
|
},
|
|
method: "PUT"
|
|
}).done(successful_save).fail(failed_save);
|
|
|
|
function successful_save(res) {
|
|
console.info("User-save: Successful.");
|
|
console.info(res);
|
|
}
|
|
|
|
function failed_save(err) {
|
|
console.info("User-save: Failed.");
|
|
console.error(err);
|
|
}
|
|
}
|
|
|
|
// Login component: Login panel (if not logged in) or Logout element (if logged in)
|
|
Vue.component('login-panel', {
|
|
template: `
|
|
<div class="modal-card" style="width: auto">
|
|
<header class="modal-card-head">
|
|
<p class="modal-card-title">Login (Ohmportal)</p>
|
|
</header>
|
|
<section class="modal-card-body">
|
|
<b-field>
|
|
<b-input
|
|
v-model=user
|
|
placeholder="User"
|
|
:value="user"
|
|
required>
|
|
</b-input>
|
|
</b-field>
|
|
<b-field>
|
|
<b-input
|
|
v-model=pwd
|
|
type="password"
|
|
placeholder="Password"
|
|
:value="pwd"
|
|
@change="pwd_changed"
|
|
@keyup.enter.native="login"
|
|
required>
|
|
</b-input>
|
|
</b-field>
|
|
<b-checkbox>Eingeloggt bleiben</b-checkbox>
|
|
<div v-if="error" :key="error" style="color:red;"> Versuche es nochmal.</div>
|
|
</section>
|
|
<footer class="modal-card-foot">
|
|
<button class="button" type="button" @click.prevent="$parent.close()">Close</button>
|
|
<button class="button is-primary" @click.prevent="login">Login</button>
|
|
</footer>
|
|
</div>`,
|
|
data: function () {
|
|
return {
|
|
user: '',
|
|
pwd: '',
|
|
error: false,
|
|
};
|
|
},
|
|
// TODO: Check if enough for session cookie re-login; checkbox
|
|
// Doesn't work
|
|
beforeMount: function() {
|
|
this.checkData();
|
|
},
|
|
methods: {
|
|
closeLoginPanel: function() {
|
|
this.$emit('close-login-panel');
|
|
},
|
|
showLoginPanel: function() {
|
|
this.$emit('show-login-panel');
|
|
},
|
|
pwd_changed: function(evt) {
|
|
// Only required for PasswordInsert Apps etc. who send only(!) change events
|
|
this.pwd = e.target.value;
|
|
// Alternative call the input event
|
|
//e.target.dispatchEvent(new Event('input'));
|
|
},
|
|
login: function() {
|
|
clearAuthState();
|
|
|
|
$.ajax({
|
|
url: "api/login",
|
|
data: {
|
|
user: this.user,
|
|
pwd: this.pwd
|
|
},
|
|
method: "POST"
|
|
}).done(successful_login).fail(failed_login);
|
|
|
|
function successful_login(resData) {
|
|
fillAuthState(resData);
|
|
// updateUserInDB();
|
|
auth.abos = [];
|
|
auth.bookmarks = [];
|
|
console.info("Correct credentials");
|
|
this.closeLoginPanel;
|
|
|
|
//console.log(resData);
|
|
router.push('/home');
|
|
}
|
|
|
|
function failed_login(err) {
|
|
console.info("Wrong credentials");
|
|
this.error=true;
|
|
this.showLoginPanel;
|
|
|
|
console.log("error: " + err.responseText);
|
|
console.log(err);
|
|
}
|
|
},
|
|
checkData: function() {
|
|
clearAuthState();
|
|
|
|
$.ajax({
|
|
url: "api/login",
|
|
data: undefined,
|
|
method: "POST"
|
|
}).done(successful_login).fail(failed_login);
|
|
|
|
function successful_login(res) {
|
|
console.info("Re-Auth: Correct credentials");
|
|
this.closeLoginPanel;
|
|
vueForceRender();
|
|
|
|
//console.log(res);
|
|
router.push('/home')
|
|
}
|
|
|
|
function failed_login(err) {
|
|
console.info("Re-Auth: Wrong credentials");
|
|
this.error=true;
|
|
this.showLoginPanel;
|
|
|
|
console.log(err);
|
|
}
|
|
},
|
|
},
|
|
});
|