From 716a65f0194425aa8a2c97a12bc2a84a529bb8a1 Mon Sep 17 00:00:00 2001 From: Darius klein Date: Sun, 11 Jan 2026 21:05:37 +0100 Subject: [PATCH] added register to client + fixed config bug + fixed register duplicate user bug --- client/todo/clientCommon/config/config.go | 24 ++-- client/todo/clientCommon/config/getConfig.go | 13 ++- client/todo/main.go | 1 + client/todo/register.go | 116 +++++++++++++++++++ common/bolt.go | 2 +- common/files.go | 13 +++ server/handler/registerHandler.go | 4 +- 7 files changed, 157 insertions(+), 16 deletions(-) create mode 100644 client/todo/register.go create mode 100644 common/files.go diff --git a/client/todo/clientCommon/config/config.go b/client/todo/clientCommon/config/config.go index 4548fe9..0d864a3 100644 --- a/client/todo/clientCommon/config/config.go +++ b/client/todo/clientCommon/config/config.go @@ -2,6 +2,7 @@ package config import ( "errors" + "log/slog" "os" "path/filepath" "runtime" @@ -62,15 +63,20 @@ func ReadConfig() (Config, error) { return config, err } - file, err := os.ReadFile(configPath) - if err != nil { - return config, err - } + if common.FileExists(configPath) { - _, err = toml.Decode(string(file), &config) - if err != nil { - return config, err - } + file, err := os.ReadFile(configPath) + if err != nil { + return config, err + } - return config, nil + _, err = toml.Decode(string(file), &config) + if err != nil { + return config, err + } + return config, nil + + } + slog.Error("Config file not found. User [todo config create] to create a new one.") + return config, errors.New("config file not found. User [todo config create] to create a new one") } diff --git a/client/todo/clientCommon/config/getConfig.go b/client/todo/clientCommon/config/getConfig.go index 20b21dc..fb78454 100644 --- a/client/todo/clientCommon/config/getConfig.go +++ b/client/todo/clientCommon/config/getConfig.go @@ -1,10 +1,11 @@ package config import ( + "bytes" "context" "fmt" - "os" + "github.com/BurntSushi/toml" "github.com/urfave/cli/v3" ) @@ -19,15 +20,17 @@ func GetConfig() *cli.Command { // getConfigAction logic for GetConfig func getConfigAction(context context.Context, c *cli.Command) error { - _, configPath, err := GetConfigPath() + configFile, err := ReadConfig() if err != nil { return err } - file, err := os.ReadFile(configPath) - if err != nil { + var buf bytes.Buffer + if err := toml.NewEncoder(&buf).Encode(configFile); err != nil { return err } - fmt.Println(string(file)) + + tomlString := buf.String() + fmt.Println(tomlString) return nil } diff --git a/client/todo/main.go b/client/todo/main.go index 92b5df9..a4a25dc 100644 --- a/client/todo/main.go +++ b/client/todo/main.go @@ -51,6 +51,7 @@ func commands() []*cli.Command { Sync(), Add(), Todo(), + Register(), { Name: "update", Usage: "Update the vennexCLI to a specific version", diff --git a/client/todo/register.go b/client/todo/register.go new file mode 100644 index 0000000..e711805 --- /dev/null +++ b/client/todo/register.go @@ -0,0 +1,116 @@ +package main + +import ( + "bytes" + "context" + "encoding/json" + "fmt" + "io" + "log" + "net/http" + "os" + + "gitea.kleinsense.nl/DariusKlein/kleinTodo/client/todo/clientCommon/config" + "gitea.kleinsense.nl/DariusKlein/kleinTodo/client/todo/httpClient" + "gitea.kleinsense.nl/DariusKlein/kleinTodo/common" + "github.com/BurntSushi/toml" + "github.com/urfave/cli/v3" +) + +// Register Command +func Register() *cli.Command { + return &cli.Command{ + Name: "register", + Usage: "Register new user to kleinTodo server", + Action: registerAction, + Flags: registerFlags(), + } +} + +// registerFlags Register cli flags +func registerFlags() []cli.Flag { + return []cli.Flag{ + &cli.StringFlag{ + Name: usernameFlagName, + Aliases: []string{"u"}, + Required: true, + }, + &cli.StringFlag{ + Name: passwordFlagName, + Aliases: []string{"p"}, + Required: true, + }, + &cli.StringFlag{ + Name: serverUrlFlagName, + Aliases: []string{"url"}, + Required: true, + }, + } +} + +// registerAction logic for Template +func registerAction(context context.Context, c *cli.Command) error { + var username = c.String(usernameFlagName) + var password = c.String(passwordFlagName) + var serverUrl = c.String(serverUrlFlagName) + + err := registerAndGetToken(serverUrl, username, password) + if err != nil { + return err + } + + if (cfg.Server.Credentials.Username != username || cfg.Server.Credentials.Password != password) && + common.AskUserBool("Do you wish to save your credentials? (WARNING: stored in plaintext)") { + cfg.Server.Credentials.Username = username + cfg.Server.Credentials.Password = password + } + + if cfg.Server.Url != serverUrl && common.AskUserBool("Do you wish to save your chosen server url?") { + cfg.Server.Url = serverUrl + } + + path, configPath, err := config.GetConfigPath() + if err != nil { + return err + } + if err = os.MkdirAll(path, 0770); err != nil { + return err + } + configBytes, err := toml.Marshal(cfg) + err = os.WriteFile(configPath, configBytes, 0644) + + log.Println("Registered new user to kleinTodo server successfully") + + return nil +} + +func registerAndGetToken(url, username, password string) error { + credentials := common.Credentials{ + Username: username, + Password: password, + } + + payload, err := json.Marshal(credentials) + if err != nil { + return fmt.Errorf("error marshaling credentials: %w", err) + } + + req, err := http.NewRequest("POST", url+"/register", bytes.NewBuffer(payload)) + if err != nil { + return fmt.Errorf("error creating request: %w", err) + } + req.Header.Set("Content-Type", "application/json") + + resp, err := httpClient.GetHttpClient("").Do(req) + if err != nil { + return fmt.Errorf("error sending request: %w", err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + body, _ := io.ReadAll(resp.Body) + return fmt.Errorf("register failed with status %d: %s", resp.StatusCode, string(body)) + } + + return nil +} diff --git a/common/bolt.go b/common/bolt.go index 6226aad..a783318 100644 --- a/common/bolt.go +++ b/common/bolt.go @@ -194,7 +194,7 @@ func (s *BoltStore) ExistsByKey(bucket, key string) (bool, error) { // Get returns nil if the key doesn't exist valBytes := b.Get([]byte(key)) if valBytes != nil { - exists = string(valBytes) == key + exists = true } return nil }) diff --git a/common/files.go b/common/files.go new file mode 100644 index 0000000..db02370 --- /dev/null +++ b/common/files.go @@ -0,0 +1,13 @@ +package common + +import ( + "os" +) + +func FileExists(filename string) bool { + info, err := os.Stat(filename) + if os.IsNotExist(err) { + return false + } + return !info.IsDir() +} diff --git a/server/handler/registerHandler.go b/server/handler/registerHandler.go index 29104dd..1af5c2d 100644 --- a/server/handler/registerHandler.go +++ b/server/handler/registerHandler.go @@ -22,7 +22,9 @@ func RegisterHandler(w http.ResponseWriter, r *http.Request) { } // Check if user exists if exists, err := db.ExistsByKey(common.UserBucket, user.Username); exists || err != nil { - log.Println(err.Error()) + if err != nil { + log.Println(err.Error()) + } w.WriteHeader(http.StatusUnprocessableEntity) w.Write([]byte(`{"error":"user already exists"}`)) return