diff options
| author | nsfisis <nsfisis@gmail.com> | 2025-10-27 01:53:31 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2025-10-27 01:53:31 +0900 |
| commit | c2465b4733e888a3817f717312f5e332a72e3ba5 (patch) | |
| tree | 25d736c09c173eb2701d7d84fa87936475d302a0 /backend/cmd_createuser.go | |
| parent | af87dd1de0c1dd0e6284170aef807224d5c8ccb5 (diff) | |
| download | feedaka-c2465b4733e888a3817f717312f5e332a72e3ba5.tar.gz feedaka-c2465b4733e888a3817f717312f5e332a72e3ba5.tar.zst feedaka-c2465b4733e888a3817f717312f5e332a72e3ba5.zip | |
refactor(backend): split main.gov0.3.4
Diffstat (limited to 'backend/cmd_createuser.go')
| -rw-r--r-- | backend/cmd_createuser.go | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/backend/cmd_createuser.go b/backend/cmd_createuser.go new file mode 100644 index 0000000..f953bf0 --- /dev/null +++ b/backend/cmd_createuser.go @@ -0,0 +1,62 @@ +package main + +import ( + "bufio" + "context" + "database/sql" + "fmt" + "log" + "os" + "strings" + + "golang.org/x/crypto/bcrypt" + + "undef.ninja/x/feedaka/db" +) + +func runCreateUser(database *sql.DB) { + queries := db.New(database) + reader := bufio.NewReader(os.Stdin) + + // Read username + fmt.Print("Enter username: ") + username, err := reader.ReadString('\n') + if err != nil { + log.Fatalf("Failed to read username: %v", err) + } + username = strings.TrimSpace(username) + if username == "" { + log.Fatal("Username cannot be empty") + } + + // Read password + fmt.Print("Enter password: ") + password, err := reader.ReadString('\n') + if err != nil { + log.Fatalf("Failed to read password: %v", err) + } + password = strings.TrimSpace(password) + + // Validate password length + if len(password) < 15 { + log.Fatalf("Password must be at least 15 characters long (got %d characters)", len(password)) + } + + // Hash password + hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) + if err != nil { + log.Fatalf("Failed to hash password: %v", err) + } + + // Create user + ctx := context.Background() + user, err := queries.CreateUser(ctx, db.CreateUserParams{ + Username: username, + PasswordHash: string(hashedPassword), + }) + if err != nil { + log.Fatalf("Failed to create user: %v", err) + } + + log.Printf("User created successfully: ID=%d, Username=%s", user.ID, user.Username) +} |
