-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3566a4c
commit d2e93ac
Showing
12 changed files
with
220 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Chaos Game Visualiser Admin</title> | ||
|
||
<link rel="stylesheet" href="https://static.danielhoward.me/lib/bootstrap-5.0.2.min.css"> | ||
<link rel="stylesheet" href="https://static.danielhoward.me/lib/bootstrap-icons-1.11.0.min.css"> | ||
</head> | ||
<body> | ||
<div id="loading" class="center"> | ||
<div class="spinner-border" role="status" style="height: 15rem; width: 15rem;"> | ||
<span class="visually-hidden">Loading...</span> | ||
</div> | ||
</div> | ||
<div id="login" class="center hidden"> | ||
<button id="loginButton" type="button" class="btn btn-primary"><i class="bi bi-cloud-fill"></i> Log in</button> | ||
</div> | ||
<div id="errorAlert" class="center hidden"> | ||
<div role="alert" class="alert alert-danger"> | ||
<h4 class="alert-heading">An error has occured</h4> | ||
An error has occured whilst loading the page. Please try again later. | ||
</div> | ||
</div> | ||
<div id="unauthorisedAlert" class="center hidden"> | ||
<div role="alert" class="alert alert-warning"> | ||
<h4 class="alert-heading">Unauthorised</h4> | ||
You need to login with an admin account in order to view this page. | ||
</div> | ||
</div> | ||
<div id="page" class="hidden"> | ||
<div class="settings-container"> | ||
<div class="container"> | ||
<div class="row"> | ||
<div class="col-12 col-lg-8"> | ||
<fieldset> | ||
<legend>Presets</legend> | ||
</fieldset> | ||
</div> | ||
<div class="col-12 col-lg-4"> | ||
<fieldset> | ||
<legend>Screenshots</legend> | ||
</fieldset> | ||
<fieldset> | ||
<legend>Admins</legend> | ||
</fieldset> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import './../styles/admin.css'; | ||
|
||
import {fetchUserSaves} from './../lib/backend'; | ||
import {hasAuthInStorage, openLoginPopup} from './../lib/sso'; | ||
|
||
function $<T extends HTMLElement = HTMLElement>(id: string): T { | ||
return <T> document.getElementById(id); | ||
} | ||
|
||
const errorAlert = $('errorAlert'); | ||
const unauthorisedAlert = $('unauthorisedAlert'); | ||
|
||
const loading = $('loading'); | ||
|
||
const login = $('login'); | ||
const loginButton = $('loginButton'); | ||
|
||
const page = $('page'); | ||
|
||
function showView(show: HTMLElement) { | ||
([ | ||
errorAlert, | ||
unauthorisedAlert, | ||
loading, | ||
login, | ||
page, | ||
]).forEach((c) => c.classList.toggle('hidden', c !== show)); | ||
} | ||
|
||
async function init() { | ||
const {account} = await fetchUserSaves(); | ||
|
||
if (!account.admin) { | ||
showView(unauthorisedAlert); | ||
return; | ||
} | ||
|
||
showView(page); | ||
} | ||
|
||
function onload() { | ||
if (hasAuthInStorage()) { | ||
init(); | ||
} else { | ||
loginButton.addEventListener('click', async () => { | ||
const loggedIn = await openLoginPopup(); | ||
if (loggedIn) { | ||
showView(loading); | ||
init(); | ||
} | ||
}); | ||
|
||
showView(login); | ||
} | ||
} | ||
|
||
window.addEventListener('DOMContentLoaded', onload); |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import {ssoPath} from './paths'; | ||
|
||
import type {LocalStorageAuth} from './../types.d'; | ||
|
||
export function getAuthStorage(): LocalStorageAuth { | ||
const auth = JSON.parse(localStorage.getItem('auth')) as LocalStorageAuth; | ||
if (!auth) throw new Error('No auth in storage'); | ||
if (auth.expires < Date.now()) throw new Error('accessToken expired'); | ||
return auth; | ||
} | ||
|
||
export function hasAuthInStorage(): boolean { | ||
try { | ||
getAuthStorage(); | ||
return true; | ||
} catch (_) { | ||
return false; | ||
} | ||
} | ||
|
||
let loginWindow: Window; | ||
export function openLoginPopup(): Promise<boolean> { | ||
return new Promise((res) => { | ||
if (loginWindow?.closed === false) loginWindow.focus(); | ||
else loginWindow = window.open(ssoPath, '', 'width=500, height=600'); | ||
|
||
const interval = setInterval(() => { | ||
// Test if the page has been closed | ||
if (loginWindow.closed) { | ||
clearInterval(interval); | ||
res(false); | ||
return; | ||
} | ||
|
||
// Test if the origin has changed, meaning it has been authed | ||
let originsMatch = false; | ||
try { | ||
originsMatch = window.location.origin === loginWindow.location.origin; | ||
} catch (err) { | ||
// Ignore error thrown by browser since it is expected when on a different origin | ||
} | ||
if (originsMatch && localStorage.getItem('auth') !== null) { | ||
clearInterval(interval); | ||
loginWindow.close(); | ||
res(true); | ||
} | ||
}, 1000); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
.hidden { | ||
display: none !important; | ||
} | ||
|
||
.center { | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
min-height: 100vh; | ||
} | ||
|
||
body:has(#page:not(.hidden)) { | ||
background: whitesmoke; | ||
} | ||
|
||
.settings-container { | ||
margin: 20px; | ||
} | ||
|
||
@media (min-width: 992px) { | ||
.settings-container { | ||
height: calc(100vh - 40px); | ||
} | ||
} | ||
|
||
.settings-container fieldset { | ||
background: white; | ||
border: lightgray 1px solid; | ||
border-radius: 5px; | ||
padding: 10px; | ||
} | ||
.settings-container legend { | ||
float: initial; | ||
width: auto; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters