|
- package helpers
-
- import "fmt"
-
- type setupStatusResponse struct {
- Success bool `json:"success"`
- Data struct {
- Status bool `json:"status"`
- RootInit bool `json:"root_init"`
- } `json:"data"`
- Message string `json:"message,omitempty"`
- }
-
- type setupResponse struct {
- Success bool `json:"success"`
- Message string `json:"message,omitempty"`
- }
-
- type loginResponse struct {
- Success bool `json:"success"`
- Data struct {
- ID int `json:"id"`
- } `json:"data"`
- Message string `json:"message,omitempty"`
- }
-
- func SetupRoot(client *APIClient, username, password string) error {
- var setupStatus setupStatusResponse
- if err := client.GetJSON("/api/setup", 200, &setupStatus); err != nil {
- return err
- }
- if setupStatus.Data.Status {
- return nil
- }
-
- var resp setupResponse
- if err := client.PostJSON("/api/setup", map[string]any{
- "username": username,
- "password": password,
- "confirmPassword": password,
- "SelfUseModeEnabled": false,
- "DemoSiteEnabled": false,
- }, 200, &resp); err != nil {
- return err
- }
- if !resp.Success {
- return fmt.Errorf("setup root failed: %s", resp.Message)
- }
- return nil
- }
-
- func Login(client *APIClient, username, password string) error {
- var resp loginResponse
- if err := client.PostJSON("/api/user/login", map[string]any{
- "username": username,
- "password": password,
- }, 200, &resp); err != nil {
- return err
- }
- if !resp.Success {
- return fmt.Errorf("login failed: %s", resp.Message)
- }
- if resp.Data.ID == 0 {
- return fmt.Errorf("login succeeded but user id is empty")
- }
- client.UserID = resp.Data.ID
- return nil
- }
-
- func SetupAndLoginRoot(client *APIClient, username, password string) error {
- if err := SetupRoot(client, username, password); err != nil {
- return err
- }
- return Login(client, username, password)
- }
|