From 37338232c2312bf05f47109b5c5e6f4f8820706c Mon Sep 17 00:00:00 2001 From: Araozu Date: Mon, 27 May 2024 18:22:42 -0500 Subject: [PATCH] Add login states & transition --- frontend/src/App.tsx | 2 ++ frontend/src/routes/Home.tsx | 15 +++++++++++++++ frontend/src/routes/Login.tsx | 33 ++++++++++++++++++++++++--------- session.go | 22 ++++++++++------------ 4 files changed, 51 insertions(+), 21 deletions(-) create mode 100644 frontend/src/routes/Home.tsx diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index b867c14..e52016a 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -2,11 +2,13 @@ import { Route, HashRouter as Router } from "@solidjs/router"; import type { Component } from "solid-js"; import { Login } from "./routes/Login"; +import { Home } from "./routes/Home"; const App: Component = () => (
+
); diff --git a/frontend/src/routes/Home.tsx b/frontend/src/routes/Home.tsx new file mode 100644 index 0000000..5eec554 --- /dev/null +++ b/frontend/src/routes/Home.tsx @@ -0,0 +1,15 @@ +import { createSignal, onMount } from "solid-js"; + +export function Home() { + const [hidden, setHidden] = createSignal(true); + + onMount(() => { + setTimeout(() => setHidden(false) , 150); + }); + + return ( +
+ Home :D +
+ ); +} diff --git a/frontend/src/routes/Login.tsx b/frontend/src/routes/Login.tsx index 6ad6955..591eb45 100644 --- a/frontend/src/routes/Login.tsx +++ b/frontend/src/routes/Login.tsx @@ -1,27 +1,36 @@ -import { createSignal } from "solid-js"; +import { Show, createSignal } from "solid-js"; import { Login as GoLogin } from "../../wailsjs/go/main/App"; +import { useNavigate } from "@solidjs/router"; export function Login() { const [loading, setLoading] = createSignal(false); + const [error, setError] = createSignal(""); const [server, setServer] = createSignal(""); const [username, setUsername] = createSignal(""); const [password, setPassword] = createSignal(""); + const [fade, setFade] = createSignal(false); + const navigate = useNavigate(); const login = async(ev: Event) => { ev.preventDefault(); setLoading(true); + setError(""); - const success = await GoLogin(server(), username(), password()); - if (success) { - alert(":D"); - } else { - alert("D:"); - } - setLoading(false); + GoLogin(server(), username(), password()) + .then(() => { + setFade(true); + setTimeout(() => { + navigate("/home"); + }, 150); + }) + .catch((err) => { + setError(err); + setLoading(false); + }); }; return ( -
+

Login

@@ -65,6 +74,12 @@ export function Login() {
+ + + +
diff --git a/session.go b/session.go index 9fd0aa1..5981c80 100644 --- a/session.go +++ b/session.go @@ -1,9 +1,9 @@ package main import ( + "errors" "fmt" "log" - "time" "github.com/go-resty/resty/v2" ) @@ -15,15 +15,15 @@ type AuthSuccess struct { SubsonicSalt string `json:"subsonicSalt"` SubsonicToken string `json:"subsonicToken"` Token string `json:"token"` - Username string `json:"fernando"` + Username string `json:"username"` } type AuthError struct { Error string `json:"error"` } -// / (Tries to) login to a remote navidrome server -func (a *App) Login(server, username, password string) bool { +// (Tries to) login to a remote navidrome server +func (a *App) Login(server, username, password string) (bool, error) { client := resty.New() // TODO: check server for leading https and ending /, normalize @@ -40,19 +40,17 @@ func (a *App) Login(server, username, password string) bool { if err != nil { log.Print("Login error", err) - return false + return false, err } if response.IsSuccess() { log.Printf("%+v", successData) - return true + return true, nil } else if response.IsError() { log.Printf("%+v", errorData) - return false + return false, errors.New(errorData.Error + ".") + } else { + log.Printf("ehhh???") + return false, errors.New("invalid state") } - - log.Print("Login: ", server, username, password) - time.Sleep(1 * time.Second) - - return true }