1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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)
}
|