doing a bit of work on this, we can do admin login now ayyy

This commit is contained in:
Iris Lightshard 2023-01-25 22:57:31 -07:00
parent 7f18668f44
commit 50ea1c2b12
Signed by: Iris Lightshard
GPG key ID: 3B7FBC22144E6398
7 changed files with 61 additions and 5 deletions

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
felt felt
user.db
mongodb/data/* mongodb/data/*
mongodb/.env mongodb/.env

View file

@ -1,6 +1,7 @@
package admin package admin
import ( import (
"html/template"
"net/http" "net/http"
"nilfm.cc/git/felt/admin/util" "nilfm.cc/git/felt/admin/util"
"nilfm.cc/git/felt/models" "nilfm.cc/git/felt/models"
@ -145,7 +146,7 @@ func apiDestroyTable(next http.Handler, udb auth.UserStore, dbAdapter mongodb.Db
func CreateAdminInterface(udb auth.UserStore, dbAdapter mongodb.DbAdapter) http.Handler { func CreateAdminInterface(udb auth.UserStore, dbAdapter mongodb.DbAdapter) http.Handler {
// create quartzgun router // create quartzgun router
rtr := &router.Router{} rtr := &router.Router{ Fallback: *template.Must(template.ParseFiles("static/error.html")) }
scopes := map[string]string{} scopes := map[string]string{}

View file

@ -13,6 +13,7 @@ import (
"nilfm.cc/git/felt/mongodb" "nilfm.cc/git/felt/mongodb"
"nilfm.cc/git/quartzgun/auth" "nilfm.cc/git/quartzgun/auth"
"nilfm.cc/git/quartzgun/cookie" "nilfm.cc/git/quartzgun/cookie"
"nilfm.cc/git/quartzgun/renderer"
"sync" "sync"
"time" "time"
) )
@ -40,8 +41,8 @@ func New(adapter mongodb.DbAdapter, udb auth.UserStore) *GameTableServer {
publishLimiter: rate.NewLimiter(rate.Every(time.Millisecond*100), 8), publishLimiter: rate.NewLimiter(rate.Every(time.Millisecond*100), 8),
dbAdapter: adapter, dbAdapter: adapter,
} }
srvr.serveMux.Handle("/table/", http.FileServer(http.Dir("./static"))) srvr.serveMux.Handle("/table/", http.StripPrefix("/table/", renderer.Subtree("./static")))
srvr.serveMux.Handle("/admin/", admin.CreateAdminInterface(udb, adapter)) srvr.serveMux.Handle("/admin/", http.StripPrefix("/admin", admin.CreateAdminInterface(udb, adapter)))
srvr.serveMux.HandleFunc("/subscribe", srvr.subscribeHandler) srvr.serveMux.HandleFunc("/subscribe", srvr.subscribeHandler)
srvr.serveMux.HandleFunc("/publish", srvr.publishHandler) srvr.serveMux.HandleFunc("/publish", srvr.publishHandler)

View file

@ -35,6 +35,8 @@ func run() error {
udb := indentalUserDB.CreateIndentalUserDB( udb := indentalUserDB.CreateIndentalUserDB(
"./user.db") "./user.db")
udb.AddUser("nilix", "questing")
gt := gametable.New(dbEngine, udb) gt := gametable.New(dbEngine, udb)
s := &http.Server{ s := &http.Server{
Handler: gt, Handler: gt,

36
static/admin.js Normal file
View file

@ -0,0 +1,36 @@
let adminToken = null;
async function doLogin() {
const adminUsrInput = document.getElementById("input_admin_usr");
const adminPassInput = document.getElementById("input_admin_pass");
const errDiv = document.getElementById("loginErr");
if (adminUsrInput && adminPassInput) {
adminToken = await getAdminToken(adminUsrInput.value, adminPassInput.value);
if (adminToken) {
// render admin interface
} else {
if (errDiv) {
errDiv.innerHTML = "Incorrect credentials";
}
}
}
}
async function getAdminToken(user, pass) {
const headers = new Headers();
headers.set('Authorization', 'Basic ' + btoa(user + ":" + pass));
try {
const res = await fetch('/admin/api/auth/', {
method: 'POST',
headers: headers
});
if (res.ok) {
return await res.json();
}
return null;
} catch (err) {
return null;
}
}

8
static/error.html Normal file
View file

@ -0,0 +1,8 @@
{{ $params := (.Context).Value "params" }}
<html>
<body>
<h1>ERR {{ $params.ErrorCode }}</h1>
<p>{{ $params.ErrorMessage }}</p>
</body>
</html>

View file

@ -4,7 +4,9 @@
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<title>Felt</title> <title>Felt</title>
<meta name="viewport" content="width=device-width" /> <meta name="viewport" content="width=device-width" />
<link href="/style.css" rel="stylesheet" /> <link href="./style.css" rel="stylesheet" />
<script src="./admin.js" type="text/javascript"></script>
<script src="./index.js" type="text/javascript"></script>
</head> </head>
<body> <body>
<nav> <nav>
@ -12,7 +14,12 @@
<button id="goto_table">Change Table</button> <button id="goto_table">Change Table</button>
<button id="admin_login">Admin Login</button> <button id="admin_login">Admin Login</button>
</nav> </nav>
<div id="dynamic_modal"></div> <div id="admin_modal">
<label>usr<input id="input_admin_usr"></label>
<label>pass<input type="password" id="input_admin_pass"></label>
<button id="admin_login" onclick="doLogin()">login</button>
<div id="loginErr"></div>
</div>
<div id="dice_log"></div> <div id="dice_log"></div>
<select name="num_dice"> <select name="num_dice">
<option>1</option> <option>1</option>