72 lines
1.8 KiB
JavaScript
72 lines
1.8 KiB
JavaScript
// Devs: Replace this with the name of your hyperware package
|
|
const APP_PATH = '/loginex:login-example:sortugdev.os';
|
|
// Devs: Replace this with the name of your app
|
|
const APP_NAME = "Moomdeck";
|
|
document.addEventListener("DOMContentLoaded", checkAuth);
|
|
|
|
|
|
async function checkAuth() {
|
|
const res = await call_app({ CheckAuth: null });
|
|
const j = await res.json();
|
|
console.log({j})
|
|
if (j) {
|
|
const button = document.getElementById('login-button');
|
|
button.innerText = "Logout"
|
|
document.getElementById('form').addEventListener('submit', logout);
|
|
} else {
|
|
document.getElementById('form').addEventListener('submit', login);
|
|
}
|
|
}
|
|
async function call_app(body) {
|
|
return await fetch(APP_PATH + "/hypr-login", {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify(body),
|
|
});
|
|
}
|
|
const logout = async (e) => {
|
|
e.preventDefault();
|
|
await call_app({Logout: null});
|
|
window.location.reload();
|
|
}
|
|
const login = (e) => {
|
|
e.preventDefault();
|
|
const body = {
|
|
Sign: null
|
|
};
|
|
send(body)
|
|
};
|
|
document.getElementById('login-button').innerText += ` ${APP_NAME}`;
|
|
const errorDiv = document.getElementById("error-div");
|
|
async function send(body) {
|
|
try {
|
|
const res = await call_app(body);
|
|
let j = await res.json();
|
|
console.log("signature...?", j);
|
|
if ("error" in j) errorDiv += j.error;
|
|
else redirect();
|
|
} catch (e) {
|
|
console.log(`${e}`)
|
|
}
|
|
}
|
|
|
|
async function redirect() {
|
|
window.location.replace(APP_PATH);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ('serviceWorker' in navigator) {
|
|
navigator.serviceWorker.register(APP_PATH + '/sw.js')
|
|
.then(registration => {
|
|
console.log('Service Worker registered with scope:', registration.scope);
|
|
})
|
|
.catch(error => {
|
|
console.error('Service Worker registration failed:', error);
|
|
});
|
|
}
|