cleanup + js modules

This commit is contained in:
ideclon 2022-11-30 20:46:02 +00:00
parent b2f9dceef1
commit 353ed600ef
Signed by: ideclon
GPG Key ID: 3E186629301AFBFD
8 changed files with 59 additions and 42 deletions

View File

@ -1,3 +1,4 @@
<?php require("template/header.html")?>
You are connected to Mastodon as <b><span class='mastodon_user'></span></b> on server <b><span class="mastodon_server"></span></b>
<?php $script = 'js/index.js'; require("template/footer.php")?>

View File

@ -0,0 +1,9 @@
import {accountDetails} from "./setup.js";
document.querySelectorAll('.mastodon_server').forEach((element) => {
element.innerHTML = accountDetails.prefs.user_server_uri
});
document.querySelectorAll('.mastodon_user').forEach((element) => {
element.innerHTML = JSON.parse(accountDetails.prefs.account_details).acct
});

View File

@ -1,3 +1,5 @@
import {account} from './setup.js';
account.get().then((response) => {
location.href = '/';
}).catch((err) => {

View File

@ -6,39 +6,40 @@ const account = new Appwrite.Account(client);
const databases = new Appwrite.Databases(client);
const functions = new Appwrite.Functions(client);
let accountDetailsPromise = account.get().then((res) => {
accountDetailsPromise = res
})
let accountDetails;
if(window.location.pathname !== '/login.html') {
if (window.location.pathname !== '/login.php') {
account.get().then((response) => {
console.log(response);
document.querySelectorAll('.fill-name').forEach(el => el.innerHTML = response.name);
if(response.emailVerification == false) {
if (response.emailVerification == false) {
message_box('error', 'Your account is not verified!');
return
}
if(response.prefs.user_token == undefined) {
if (response.prefs.user_token == undefined) {
message_box('info', "You haven't connected to Mastodon yet! <a href='connect.php'>Do it now</a>");
}
}).catch((err) => {
if(err == "AppwriteException: User (role: guests) missing scope (account)") {
location.href = "login.html";
if (err == "AppwriteException: User (role: guests) missing scope (account)") {
location.href = "login.php";
}
})
document.getElementById('logout').addEventListener('click', () => {
account.deleteSession('current').then((res) => {
window.location.reload();
});
})
accountDetails = await account.get();
}
document.getElementById('logout').addEventListener('click', () => {
account.deleteSession('current').then((res) => {
window.location.reload();
});
})
function message_box(type, message) {
export function message_box(type, message) {
const message_box = document.getElementById('message-box');
message_box.classList.remove('info', 'error', 'success');
@ -46,4 +47,6 @@ function message_box(type, message) {
message_box.innerHTML = message;
message_box.classList.add(type);
message_box.style.display = 'block';
}
}
export { account, databases, functions, accountDetails };

View File

@ -1,18 +0,0 @@
<!DOCTYPE html>
<head>
<title>Login | Mastodon Algorithm</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<script src="https://cdn.jsdelivr.net/npm/appwrite@10.1.0"></script>
<body>
<fieldset>
<legend>Login</legend>
<label>Email: </label> <input type="email" id="email" /><br>
<label>Password: </label> <input type="password" id="password" /><br>
<span id="error-message" style="color: red;"></span>
<button type="button" id="login">Log in</button>
</fieldset>
</body>
<script src="js/setup.js"></script>
<script src="js/login.js"></script>

20
login.php Normal file
View File

@ -0,0 +1,20 @@
<!DOCTYPE html>
<head>
<title>Login | Mastodon Algorithm</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<script src="https://cdn.jsdelivr.net/npm/appwrite@10.1.0"></script>
<body>
<fieldset>
<legend>Login</legend>
<label>Email: </label> <input type="email" id="email" /><br>
<label>Password: </label> <input type="password" id="password" /><br>
<span id="error-message" style="color: red;"></span>
<button type="button" id="login">Log in</button>
</fieldset>
</body>
<?php $script = 'js/login.js';
require("template/footer.php") ?>

View File

@ -4,5 +4,5 @@ const APPWRITE_ENDPOINT = '<?=getenv('APPWRITE_ENDPOINT')?>';
const APPWRITE_PROJECT_ID = '<?=getenv('APPWRITE_PROJECT_ID')?>';
const APPWRITE_DB_ID = '<?=getenv('APPWRITE_DB_ID')?>';
</script>
<script src="js/setup.js"></script>
<?=(isset($script))?"<script src=\"$script\"></script>":''?>
<script type="module" src="js/setup.js"></script>
<?=(isset($script))?"<script type='module' src=\"$script\"></script>":''?>

View File

@ -7,6 +7,6 @@
</head>
<script src="https://cdn.jsdelivr.net/npm/appwrite@10.1.0"></script>
<body>
<span id="welcome">Welcome, <span class="fill-name"></span>. Not you? <a href="#" id='logout'>Logout</a></span>
<span id="welcome">Welcome, <span class="fill-name"></span>. Not you? <a href="#" id='logout'>Logout</a></span><br>
<span class="message-box" id="message-box"></span>
<span class="message-box" id="message-box"></span><br>