api - login/out possible
This commit is contained in:
@@ -31,7 +31,7 @@ $(document).ready(function(){
|
||||
'use strict'
|
||||
|
||||
var isAJAX = true; //Enables or disable AJAX page transitions and loading.
|
||||
var isDevelopment = true; // Enables development mode. Clean cache & Stops BG & Highlights from changing defaults.
|
||||
var isDevelopment = false; // Enables development mode. Clean cache & Stops BG & Highlights from changing defaults.
|
||||
|
||||
function init_template(){
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ $(document).ready(function(){
|
||||
|
||||
var pwaVersion = '<?php echo PWA_VERSION; ?>'; //must be identical to _manifest.json version. If not it will create update window loop
|
||||
var pwaCookie = true; // if set to false, the PWA prompt will appear even if the user selects "maybe later"
|
||||
var pwaNoCache = true; // always keep the cache clear to serve the freshest possible content
|
||||
var pwaNoCache = false; // always keep the cache clear to serve the freshest possible content
|
||||
|
||||
|
||||
$('[data-pwa-version]').data('pwa-version', pwaVersion);
|
||||
@@ -132,7 +132,7 @@ $(document).ready(function(){
|
||||
caches.delete('workbox-runtime').then(function() {
|
||||
console.log('Content Updated - Cache Removed!');
|
||||
});
|
||||
localStorage.clear();
|
||||
//localStorage.clear();
|
||||
sessionStorage.clear()
|
||||
caches.keys().then(cacheNames => {
|
||||
cacheNames.forEach(cacheName => {
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
|
||||
?>
|
||||
|
||||
const apiUrl = '<?php echo SERVER_ADDR; ?>/api/';
|
||||
|
||||
var randomId = function() { return '_' + Math.random().toString(36).substr(2, 9); }
|
||||
|
||||
var badges = {
|
||||
@@ -96,8 +98,115 @@ var toastWarn = function (text, time = 3000) { return makeToast('bg-yellow1-dar
|
||||
var toastInfo = function (text, time = 3000) { return makeToast('bg-blue2-dark', 'fa-info', text, time); }
|
||||
var toastError = function (text, time = 3000) { return makeToast('bg-red2-dark', 'fa-times', text, time); }
|
||||
|
||||
var login = function() {
|
||||
showLoader();
|
||||
var username = $('#input-login-username').val();
|
||||
var password = $('#input-login-password').val();
|
||||
$('#input-login-username').val('');
|
||||
$('#input-login-password').val('');
|
||||
$.ajax({
|
||||
url: apiUrl + 'login',
|
||||
method: 'POST',
|
||||
data: {
|
||||
username: username,
|
||||
password: password,
|
||||
device: navigator.userAgent
|
||||
},
|
||||
error: function (xhr, status, error) {
|
||||
if (xhr.status == 401) {
|
||||
toastError('Benutzername oder Passwort falsch');
|
||||
$('#input-login-username').val(username);
|
||||
} else if (xhr.status == 0) {
|
||||
toastError('Du bist momentan offline.<br>Stelle eine Internetverbindung her, um Dich anzumelden');
|
||||
$('#menu-login').hideMenu();
|
||||
} else {
|
||||
console.log('Login: unbekannter Fehler', status, error);
|
||||
console.log(xhr);
|
||||
toastError('Ein unbekannter Fehler ist aufgetreten. Bitte versuche es noch einmal', 5000);
|
||||
}
|
||||
hideLoader();
|
||||
},
|
||||
success: function (data, status, xhr) {
|
||||
localStorage.setItem('auth_id', data.id);
|
||||
localStorage.setItem('auth_hash', data.auth);
|
||||
localStorage.setItem('auth_user', data.user);
|
||||
localStorage.setItem('auth_username', data.username);
|
||||
location.reload();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
var logoutClearStorage = function() {
|
||||
localStorage.removeItem('auth_id');
|
||||
localStorage.removeItem('auth_hash');
|
||||
localStorage.removeItem('auth_user');
|
||||
localStorage.removeItem('auth_username');
|
||||
location.reload();
|
||||
}
|
||||
|
||||
var logout = function() {
|
||||
showLoader();
|
||||
var auth = {
|
||||
id: localStorage.getItem('auth_id'),
|
||||
hash: localStorage.getItem('auth_hash')
|
||||
}
|
||||
if ((auth.id === null) || (auth.hash === null)) {
|
||||
console.log('Not logged in');
|
||||
logoutClearStorage();
|
||||
return;
|
||||
}
|
||||
$.ajax({
|
||||
url: apiUrl + 'logout',
|
||||
method: 'POST',
|
||||
data: {
|
||||
auth: auth
|
||||
},
|
||||
error: function (xhr, status, error) {
|
||||
if (xhr.status == 401) {
|
||||
console.log('Not logged in');
|
||||
logoutClearStorage();
|
||||
} else if (xhr.status == 0) {
|
||||
console.log('Could not delete auth from server');
|
||||
logoutClearStorage();
|
||||
} else {
|
||||
console.log('Logout: unbekannter Fehler', status, error);
|
||||
console.log(xhr);
|
||||
toastError('Ein unbekannter Fehler ist aufgetreten. Bitte versuche es noch einmal', 5000);
|
||||
hideLoader();
|
||||
}
|
||||
},
|
||||
success: function (data, status, xhr) {
|
||||
logoutClearStorage();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
var initRegatten = function() {
|
||||
loggedin = true;
|
||||
loggedin = (localStorage.getItem('auth_id') !== null);
|
||||
|
||||
if (loggedin) {
|
||||
var auth = {
|
||||
id: localStorage.getItem('auth_id'),
|
||||
hash: localStorage.getItem('auth_hash')
|
||||
}
|
||||
var user = {
|
||||
id: localStorage.getItem('auth_user'),
|
||||
name: localStorage.getItem('auth_username')
|
||||
}
|
||||
if ((auth.hash === null) || (user.id === null) || (user.name === null)) {
|
||||
logoutClearStorage();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (loggedin) {
|
||||
$('.show-notloggedin').css('display', 'none');
|
||||
$('.replace-userid-href').attr('href', $('.replace-userid-href').attr('href').replace('%USERID%', user.id));
|
||||
$('.replace-username').html(user.name);
|
||||
} else {
|
||||
$('.show-loggedin').css('display', 'none');
|
||||
}
|
||||
|
||||
if (typeof siteScript !== 'undefined') {
|
||||
siteScript();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user