From 55415fb5e20f9fe9e8d03e911bb449a4183ef9b3 Mon Sep 17 00:00:00 2001 From: optclblast Date: Sun, 27 Oct 2024 01:07:27 -0400 Subject: [PATCH] reg +- --- Makefile | 2 ++ cmd/main.go | 11 ++++++----- docker-compose.yaml | 6 +++--- internal/app/app.go | 16 +++++++++------- internal/storage/postgres/database.go | 2 +- schema.sql | 4 ---- 6 files changed, 21 insertions(+), 20 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..6f6e29f --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +run: + go run cmd/main.go \ No newline at end of file diff --git a/cmd/main.go b/cmd/main.go index b7ab511..1b21ba4 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -10,11 +10,12 @@ import ( func main() { ctx := context.Background() - ps := plugin.NewPluginStore() - pl := plugin.MustNewPluginLoader(ctx, 8081, ps) - pl.Run(ctx) - pg := postgres.New(ctx, "postgres://draincloud:draincloud_secret@localhost:5432/draincloud?sslmode=disable") + plugin.MustNewPluginLoader(ctx, 8081, plugin.NewPluginStore()). + Run(ctx) - app.New(pg).Run(context.TODO()) + pg := postgres.New(ctx, "postgres://draincloud:draincloud@localhost:5432/draincloud?sslmode=disable") + + app.New(pg). + Run(ctx) } diff --git a/docker-compose.yaml b/docker-compose.yaml index d926488..0425d10 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -5,9 +5,9 @@ services: ports: - 5432:5432 environment: - POSTGRES_USERNAME: draincloud - POSTGRES_DB: draincloud - POSTGRES_PASSWORD: draincloud_secret + - POSTGRES_USER=draincloud + - POSTGRES_PASSWORD=draincloud + - POSTGRES_DB=draincloud volumes: - draincloud-db-data:/var/lib/postgresql/data diff --git a/internal/app/app.go b/internal/app/app.go index 7e98211..5245350 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -82,16 +82,16 @@ func (d *DrainCloud) register(ctx *gin.Context, req *domain.RegisterRequest) (*r return nil, fmt.Errorf("invalid creds: %w", err) } - _, err := bcrypt.GenerateFromPassword([]byte(req.Password), 10) + passwordHash, err := bcrypt.GenerateFromPassword([]byte(req.Password), 10) if err != nil { logger.Error(ctx, "[register] failed to generate password", logger.Err(err)) return nil, fmt.Errorf("failed to generate password: %w", err) } - // _, err = d.datadase.AddUser(ctx, req.Login, req.Login, passwordHash) - // if err != nil { - // return nil, fmt.Errorf("failed to add new user: %w", err) - // } + _, err = d.datadase.AddUser(ctx, req.Login, req.Login, passwordHash) + if err != nil { + return nil, fmt.Errorf("failed to add new user: %w", err) + } sessionToken, err := generateSessionToken(100) if err != nil { @@ -107,15 +107,17 @@ func (d *DrainCloud) register(ctx *gin.Context, req *domain.RegisterRequest) (*r ctx.SetCookie("__Csrf_token", csrfToken, int((time.Hour * 24).Seconds()), "_path", "_domain", true, false) + // TODO save session into database + return ®isterResult{}, nil } func validateLoginAndPassword(login, password string) error { - if len(login) < 8 { + if len(login) < 4 { return fmt.Errorf("login must be longer than 8 chars") } - if len(password) < 8 { + if len(password) < 6 { return fmt.Errorf("password must be longer than 8 chars") } diff --git a/internal/storage/postgres/database.go b/internal/storage/postgres/database.go index c3ab7c8..d3bbf03 100644 --- a/internal/storage/postgres/database.go +++ b/internal/storage/postgres/database.go @@ -51,7 +51,7 @@ func (d *Database) GetUserByLogin(ctx context.Context, login string) (*models.Us func addUser(ctx context.Context, conn dbtx, login string, username string, passwordHash []byte) (uint64, error) { const stmt = `INSERT INTO users (login,username,password) - VALUES ($1,$2,$3,$4) RETURNING id` + VALUES ($1,$2,$3) RETURNING id` row := conn.QueryRow(ctx, stmt, login, username, passwordHash) diff --git a/schema.sql b/schema.sql index 9837dc8..2b91e67 100644 --- a/schema.sql +++ b/schema.sql @@ -8,10 +8,6 @@ create table if not exists users ( updated_at timestamptz default current_timestamp ); -alter table users add constraint users_username_len check(length(username) > 250) not valid; - -alter table users add constraint users_login_len check(length(username) > 250) not valid; - create index idx_users_login on users (login); create index idx_users_username on users (username);