From 27ad7a4cf7720a6680b30cea05f7af87cb051dc1 Mon Sep 17 00:00:00 2001 From: Harvey Tindall Date: Sat, 24 Jul 2021 14:15:10 +0100 Subject: [PATCH] setup: add descriptive "test connection" messages; disable next button for #129. --- lang/setup/en-us.json | 6 ++++- setup.go | 24 +++++++++++++++++-- ts/setup.ts | 56 ++++++++++++++++++++++++------------------- 3 files changed, 59 insertions(+), 27 deletions(-) diff --git a/lang/setup/en-us.json b/lang/setup/en-us.json index 93324a2..52f89e9 100644 --- a/lang/setup/en-us.json +++ b/lang/setup/en-us.json @@ -15,7 +15,11 @@ "serverAddress": "Server Address", "emailSubject": "Email Subject", "URL": "URL", - "apiKey": "API Key" + "apiKey": "API Key", + "errorInvalidUserPass": "Invalid username/password.", + "errorNotAdmin": "User is not allowed to manage server.", + "errorUserDisabled": "User may be disabled.", + "error404": "404, check the internal URL." }, "startPage": { "welcome": "Welcome!", diff --git a/setup.go b/setup.go index 9b2ed97..1d9d058 100644 --- a/setup.go +++ b/setup.go @@ -55,15 +55,35 @@ type testReq struct { func (app *appContext) TestJF(gc *gin.Context) { var req testReq gc.BindJSON(&req) + if !(strings.HasPrefix(req.Server, "http://") || strings.HasPrefix(req.Server, "https://")) { + req.Server = "http://" + req.Server + } serverType := mediabrowser.JellyfinServer if req.ServerType == "emby" { serverType = mediabrowser.EmbyServer } tempjf, _ := mediabrowser.NewServer(serverType, req.Server, "jfa-go-setup", app.version, "auth", "auth", mediabrowser.NewNamedTimeoutHandler("authJF", req.Server, true), 30) - _, status, err := tempjf.Authenticate(req.Username, req.Password) + user, status, err := tempjf.Authenticate(req.Username, req.Password) if !(status == 200 || status == 204) || err != nil { + msg := "" + switch status { + case 401: + msg = "errorInvalidUserPass" + case 403: + msg = "errorUserDisabled" + case 404: + msg = "error404" + } app.info.Printf("Auth failed with code %d (%s)", status, err) - gc.JSON(401, map[string]bool{"success": false}) + if msg != "" { + respond(status, msg, gc) + } else { + respondBool(status, false, gc) + } + return + } + if !user.Policy.IsAdministrator { + respond(403, "errorNotAdmin", gc) return } gc.JSON(200, map[string]bool{"success": true}) diff --git a/ts/setup.ts b/ts/setup.ts index cac6c18..02fb37d 100644 --- a/ts/setup.ts +++ b/ts/setup.ts @@ -461,18 +461,22 @@ window.onpopstate = (event: PopStateEvent) => { } window.scrollTo(0, 0); }); } - if (next) { next.addEventListener("click", () => { - let found = false; - for (let ind = 0; ind < cards.length; ind++) { - cards[ind].classList.add("unfocused"); - if (ind > i && !(cards[ind].classList.contains("hidden")) && !found) { - cards[ind].classList.remove("unfocused"); - changePage(pageNames[ind][0], pageNames[ind][1]); - found = true; + if (next) { + const func = () => { + if (next.hasAttribute("disabled")) return; + let found = false; + for (let ind = 0; ind < cards.length; ind++) { + cards[ind].classList.add("unfocused"); + if (ind > i && !(cards[ind].classList.contains("hidden")) && !found) { + cards[ind].classList.remove("unfocused"); + changePage(pageNames[ind][0], pageNames[ind][1]); + found = true; + } } - } - window.scrollTo(0, 0); - }); } + window.scrollTo(0, 0); + }; + next.addEventListener("click", func) + } } })(); @@ -491,20 +495,8 @@ window.onpopstate = (event: PopStateEvent) => { _post("/jellyfin/test", send, (req: XMLHttpRequest) => { if (req.readyState == 4) { toggleLoader(button); - const success = req.response["success"] as boolean; - if (success) { - nextButton.removeAttribute("disabled"); - button.textContent = window.lang.strings("success"); - button.classList.add("~positive"); - button.classList.remove("~urge"); - setTimeout(() => { - button.textContent = ogText; - button.classList.add("~urge"); - button.classList.remove("~positive"); - }, 5000); - } else { + if (req.status != 200) { nextButton.setAttribute("disabled", ""); - button.textContent = window.lang.strings("error"); button.classList.add("~critical"); button.classList.remove("~urge"); setTimeout(() => { @@ -512,7 +504,23 @@ window.onpopstate = (event: PopStateEvent) => { button.classList.add("~urge"); button.classList.remove("~critical"); }, 5000); + const errorMsg = req.response["error"] as string; + if (!errorMsg) { + button.textContent = window.lang.strings("error"); + } else { + button.textContent = window.lang.strings(errorMsg); + } + return; } + nextButton.removeAttribute("disabled"); + button.textContent = window.lang.strings("success"); + button.classList.add("~positive"); + button.classList.remove("~urge"); + setTimeout(() => { + button.textContent = ogText; + button.classList.add("~urge"); + button.classList.remove("~positive"); + }, 5000); } }, true, () => {}); };