om/public/routes/auth.js

171 lines
4.8 KiB
JavaScript
Raw Normal View History

function clearAuthState() {
auth.user = ''; // Login name
auth.name = ''; // Full name
auth.mail = ''; // Full mail address
2019-07-23 11:31:39 +00:00
auth.type = ''; // type of login (domain specific)
auth.roles = {}; // Role authorizations e.g. {user: true}
auth.gender = ''; // Gender (e.g. Frau)
2019-07-23 08:34:20 +00:00
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];
}
}
2019-07-23 08:34:20 +00:00
function updateUserInDB() {
$.ajax({
url: "api/usr",
data: {
_id: auth.user,
2019-07-23 11:31:39 +00:00
name: auth.name,
mail: auth.mail,
2019-07-23 08:34:20 +00:00
type: auth.type,
2019-07-23 11:31:39 +00:00
gender: auth.gender,
roles: JSON.stringify(auth.roles),
abos: JSON.stringify(auth.abos),
bookmarks: JSON.stringify(auth.bookmarks),
2019-07-23 08:34:20 +00:00
},
2019-07-23 11:31:39 +00:00
method: "PUT"
2019-07-23 08:34:20 +00:00
}).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>
2019-07-23 11:31:39 +00:00
<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
2019-07-23 08:34:20 +00:00
// Doesn't work
2019-07-23 11:31:39 +00:00
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);
2019-07-23 11:31:39 +00:00
// updateUserInDB();
2019-07-23 15:22:01 +00:00
auth.abos = [];
auth.bookmarks = [];
console.info("Correct credentials");
this.closeLoginPanel;
//console.log(resData);
router.push('/home');
}
function failed_login(err) {
console.info("Wrong credentials");
2019-07-23 08:34:20 +00:00
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;
2019-07-23 15:22:01 +00:00
vueForceRender();
//console.log(res);
router.push('/home')
}
function failed_login(err) {
console.info("Re-Auth: Wrong credentials");
2019-07-23 08:34:20 +00:00
this.error=true;
this.showLoginPanel;
console.log(err);
}
},
},
});