Add login states & transition
This commit is contained in:
parent
466740675d
commit
37338232c2
@ -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 = () => (
|
||||
<div>
|
||||
<Router>
|
||||
<Route path="/" component={Login} />
|
||||
<Route path="/home" component={Home} />
|
||||
</Router>
|
||||
</div>
|
||||
);
|
||||
|
15
frontend/src/routes/Home.tsx
Normal file
15
frontend/src/routes/Home.tsx
Normal file
@ -0,0 +1,15 @@
|
||||
import { createSignal, onMount } from "solid-js";
|
||||
|
||||
export function Home() {
|
||||
const [hidden, setHidden] = createSignal(true);
|
||||
|
||||
onMount(() => {
|
||||
setTimeout(() => setHidden(false) , 150);
|
||||
});
|
||||
|
||||
return (
|
||||
<div class={`min-h-screen ${hidden() ? "opacity-0" : "opacity-100"} transition-opacity`}>
|
||||
Home :D
|
||||
</div>
|
||||
);
|
||||
}
|
@ -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:");
|
||||
}
|
||||
GoLogin(server(), username(), password())
|
||||
.then(() => {
|
||||
setFade(true);
|
||||
setTimeout(() => {
|
||||
navigate("/home");
|
||||
}, 150);
|
||||
})
|
||||
.catch((err) => {
|
||||
setError(err);
|
||||
setLoading(false);
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<div class="w-screen h-screen flex items-center justify-center">
|
||||
<div class={`w-screen h-screen flex items-center justify-center ${fade() ? "opacity-0" : "opacity-100"} transition-opacity`}>
|
||||
<div class="w-80">
|
||||
<form onSubmit={login} >
|
||||
<h1 class="text-center font-black text-xl">Login</h1>
|
||||
@ -65,6 +74,12 @@ export function Login() {
|
||||
<div class="text-center">
|
||||
<button type="submit" class="btn btn-primary">Login</button>
|
||||
</div>
|
||||
|
||||
<Show when={error() !== ""}>
|
||||
<div role="alert" class="alert alert-error">
|
||||
<span class="text-white">Error: {error()}</span>
|
||||
</div>
|
||||
</Show>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
22
session.go
22
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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user