feat(user): store users in Postgres with password hashes

This commit is contained in:
kk10
2026-10-04 22:49:42 +03:00
parent bbcde1e95e
commit 527c1748f8
4 changed files with 70 additions and 20 deletions
+4 -2
View File
@@ -13,6 +13,7 @@ import (
"mtusi2026-messenger/internal/config" "mtusi2026-messenger/internal/config"
"mtusi2026-messenger/internal/db" "mtusi2026-messenger/internal/db"
"mtusi2026-messenger/internal/logger" "mtusi2026-messenger/internal/logger"
"mtusi2026-messenger/internal/postgres"
"mtusi2026-messenger/internal/service" "mtusi2026-messenger/internal/service"
"mtusi2026-messenger/internal/store" "mtusi2026-messenger/internal/store"
) )
@@ -32,16 +33,17 @@ func Run() error {
return err return err
} }
pool, err := db.New(context.Background(), dbURL) pool, err := postgres.New(context.Background(), dbURL)
if err != nil { if err != nil {
log.Error("connect to database", "error", err) log.Error("connect to database", "error", err)
return err return err
} }
defer pool.Close() defer pool.Close()
queries := db.New(pool)
memory := store.New() memory := store.New()
userService := service.NewUserService(memory) userService := service.NewUserService(queries)
chatService := service.NewChatService(memory) chatService := service.NewChatService(memory)
messageService := service.NewMessageService(memory) messageService := service.NewMessageService(memory)
+8 -1
View File
@@ -14,6 +14,7 @@ var usernamePattern = regexp.MustCompile(`^[a-z0-9_]{3,32}$`)
type CreateUserRequest struct { type CreateUserRequest struct {
Username string `json:"username"` Username string `json:"username"`
DisplayName string `json:"display_name"` DisplayName string `json:"display_name"`
Password string `json:"password"`
} }
type UserResponse struct { type UserResponse struct {
@@ -29,7 +30,13 @@ func (r CreateUserRequest) Validate() bool {
} }
displayNameLength := utf8.RuneCountInString(r.DisplayName) displayNameLength := utf8.RuneCountInString(r.DisplayName)
if strings.TrimSpace(r.DisplayName) == "" || displayNameLength > 100 { if strings.TrimSpace(r.DisplayName) == "" ||
displayNameLength > 100 ||
strings.ContainsRune(r.DisplayName, 0) {
return false
}
if n := utf8.RuneCountInString(r.Password); n < 12 || n > 128 {
return false return false
} }
+7 -2
View File
@@ -37,7 +37,12 @@ func (h *UserHandler) Create(w http.ResponseWriter, r *http.Request) {
return return
} }
user, err := h.service.Create(req.Username, req.DisplayName) user, err := h.service.Create(
r.Context(),
req.Username,
req.DisplayName,
req.Password,
)
if errors.Is(err, service.ErrUsernameTaken) { if errors.Is(err, service.ErrUsernameTaken) {
httpx.WriteError( httpx.WriteError(
w, w,
@@ -71,7 +76,7 @@ func (h *UserHandler) Me(w http.ResponseWriter, r *http.Request) {
requestID := middleware.GetRequestID(r.Context()) requestID := middleware.GetRequestID(r.Context())
userID := middleware.GetUserID(r.Context()) userID := middleware.GetUserID(r.Context())
user, err := h.service.GetByID(userID) user, err := h.service.GetByID(r.Context(), userID)
if errors.Is(err, service.ErrUserNotFound) { if errors.Is(err, service.ErrUserNotFound) {
httpx.WriteError( httpx.WriteError(
w, w,
+51 -15
View File
@@ -1,47 +1,83 @@
package service package service
import ( import (
"context"
"errors" "errors"
"github.com/google/uuid" "github.com/google/uuid"
"mtusi2026-messenger/internal/credentials"
"mtusi2026-messenger/internal/db"
"mtusi2026-messenger/internal/model" "mtusi2026-messenger/internal/model"
"mtusi2026-messenger/internal/store" "mtusi2026-messenger/internal/postgres"
) )
var ( var (
ErrUserNotFound = errors.New("user not found")
ErrUsernameTaken = errors.New("username already taken") ErrUsernameTaken = errors.New("username already taken")
ErrUserNotFound = errors.New("user not found")
) )
type userQueries interface {
CreateUser(ctx context.Context, arg db.CreateUserParams) (db.CreateUserRow, error)
GetUser(ctx context.Context, id uuid.UUID) (db.GetUserRow, error)
}
type UserService struct { type UserService struct {
store *store.Memory q userQueries
} }
func NewUserService(store *store.Memory) *UserService { func NewUserService(q userQueries) *UserService {
return &UserService{store: store} return &UserService{q: q}
} }
func (s *UserService) Create(username, displayName string) (model.User, error) { func (s *UserService) Create(
user, err := s.store.CreateUser(username, displayName) ctx context.Context,
if errors.Is(err, store.ErrConflict) { username, displayName, plain string,
return model.User{}, ErrUsernameTaken ) (model.User, error) {
} hash, err := credentials.HashPassword(plain)
if err != nil { if err != nil {
return model.User{}, err return model.User{}, err
} }
return user, nil id, err := uuid.NewV7()
if err != nil {
return model.User{}, err
}
row, err := s.q.CreateUser(ctx, db.CreateUserParams{
ID: id,
Username: username,
DisplayName: displayName,
PasswordHash: hash,
})
if err != nil {
if postgres.ErrCode(err) == postgres.CodeUniqueViolation {
return model.User{}, ErrUsernameTaken
}
return model.User{}, err
}
return model.User{
ID: row.ID,
Username: row.Username,
DisplayName: row.DisplayName,
CreatedAt: row.CreatedAt.UTC(),
}, nil
} }
func (s *UserService) GetByID(id uuid.UUID) (model.User, error) { func (s *UserService) GetByID(ctx context.Context, id uuid.UUID) (model.User, error) {
user, err := s.store.GetUser(id) row, err := s.q.GetUser(ctx, id)
if errors.Is(err, store.ErrNotFound) { if postgres.IsNotFound(err) {
return model.User{}, ErrUserNotFound return model.User{}, ErrUserNotFound
} }
if err != nil { if err != nil {
return model.User{}, err return model.User{}, err
} }
return user, nil return model.User{
ID: row.ID,
Username: row.Username,
DisplayName: row.DisplayName,
CreatedAt: row.CreatedAt.UTC(),
}, nil
} }