This commit is contained in:
r8zavetr8v 2024-10-27 01:07:27 -04:00
parent f8f10b7c15
commit 55415fb5e2
6 changed files with 21 additions and 20 deletions

2
Makefile Normal file
View File

@ -0,0 +1,2 @@
run:
go run cmd/main.go

View File

@ -10,11 +10,12 @@ import (
func main() { func main() {
ctx := context.Background() 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)
} }

View File

@ -5,9 +5,9 @@ services:
ports: ports:
- 5432:5432 - 5432:5432
environment: environment:
POSTGRES_USERNAME: draincloud - POSTGRES_USER=draincloud
POSTGRES_DB: draincloud - POSTGRES_PASSWORD=draincloud
POSTGRES_PASSWORD: draincloud_secret - POSTGRES_DB=draincloud
volumes: volumes:
- draincloud-db-data:/var/lib/postgresql/data - draincloud-db-data:/var/lib/postgresql/data

View File

@ -82,16 +82,16 @@ func (d *DrainCloud) register(ctx *gin.Context, req *domain.RegisterRequest) (*r
return nil, fmt.Errorf("invalid creds: %w", err) 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 { if err != nil {
logger.Error(ctx, "[register] failed to generate password", logger.Err(err)) logger.Error(ctx, "[register] failed to generate password", logger.Err(err))
return nil, fmt.Errorf("failed to generate password: %w", err) return nil, fmt.Errorf("failed to generate password: %w", err)
} }
// _, err = d.datadase.AddUser(ctx, req.Login, req.Login, passwordHash) _, err = d.datadase.AddUser(ctx, req.Login, req.Login, passwordHash)
// if err != nil { if err != nil {
// return nil, fmt.Errorf("failed to add new user: %w", err) return nil, fmt.Errorf("failed to add new user: %w", err)
// } }
sessionToken, err := generateSessionToken(100) sessionToken, err := generateSessionToken(100)
if err != nil { 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) ctx.SetCookie("__Csrf_token", csrfToken, int((time.Hour * 24).Seconds()), "_path", "_domain", true, false)
// TODO save session into database
return &registerResult{}, nil return &registerResult{}, nil
} }
func validateLoginAndPassword(login, password string) error { func validateLoginAndPassword(login, password string) error {
if len(login) < 8 { if len(login) < 4 {
return fmt.Errorf("login must be longer than 8 chars") 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") return fmt.Errorf("password must be longer than 8 chars")
} }

View File

@ -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) { func addUser(ctx context.Context, conn dbtx, login string, username string, passwordHash []byte) (uint64, error) {
const stmt = `INSERT INTO users (login,username,password) 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) row := conn.QueryRow(ctx, stmt, login, username, passwordHash)

View File

@ -8,10 +8,6 @@ create table if not exists users (
updated_at timestamptz default current_timestamp 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_login on users (login);
create index idx_users_username on users (username); create index idx_users_username on users (username);