wails-player/session.go

57 lines
1.3 KiB
Go
Raw Normal View History

2024-05-27 22:13:13 +00:00
package main
import (
2024-05-27 23:22:42 +00:00
"errors"
2024-05-27 22:13:13 +00:00
"fmt"
"log"
"github.com/go-resty/resty/v2"
)
type AuthSuccess struct {
Id string `json:"id"`
IsAdmin bool `json:"isAdmin"`
Name string `json:"name"`
SubsonicSalt string `json:"subsonicSalt"`
SubsonicToken string `json:"subsonicToken"`
Token string `json:"token"`
2024-05-27 23:22:42 +00:00
Username string `json:"username"`
2024-05-27 22:13:13 +00:00
}
type AuthError struct {
Error string `json:"error"`
}
2024-05-27 23:22:42 +00:00
// (Tries to) login to a remote navidrome server
func (a *App) Login(server, username, password string) (bool, error) {
2024-05-27 22:13:13 +00:00
client := resty.New()
// TODO: check server for leading https and ending /, normalize
successData := AuthSuccess{}
errorData := AuthError{}
response, err := client.R().
SetHeader("Content-Type", "").
SetBody(fmt.Sprintf(`{"username":"%s","password":"%s"}`, username, password)).
SetResult(&successData).
SetError(&errorData).
Post(fmt.Sprintf("%s/auth/login", server))
if err != nil {
log.Print("Login error", err)
2024-05-27 23:22:42 +00:00
return false, err
2024-05-27 22:13:13 +00:00
}
if response.IsSuccess() {
log.Printf("%+v", successData)
2024-05-27 23:22:42 +00:00
return true, nil
2024-05-27 22:13:13 +00:00
} else if response.IsError() {
log.Printf("%+v", errorData)
2024-05-27 23:22:42 +00:00
return false, errors.New(errorData.Error + ".")
} else {
log.Printf("ehhh???")
return false, errors.New("invalid state")
2024-05-27 22:13:13 +00:00
}
}