Migrate shared UI and backend components
This commit is contained in:
1 parent
64d22d0aac
commit
ac08be0248
16 files changed
+236
-395
No files matched your search
+5
-5
@@ -11,7 +11,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
sharedauth "repos.gfranz.ch/Gabriel20xx/SharedCode/backend/auth"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -238,7 +238,7 @@ func (a *AuthService) handleSetup(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
hash, err := bcrypt.GenerateFromPassword([]byte(request.Password), bcrypt.DefaultCost)
|
||||
hash, err := sharedauth.HashPassword(request.Password)
|
||||
if err != nil {
|
||||
writeJSONError(w, http.StatusInternalServerError, "Internal server error")
|
||||
return
|
||||
@@ -283,7 +283,7 @@ func (a *AuthService) handleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSONError(w, http.StatusInternalServerError, "Internal server error")
|
||||
return
|
||||
}
|
||||
if user == nil || bcrypt.CompareHashAndPassword([]byte(user.PasswordHash), []byte(request.Password)) != nil {
|
||||
if user == nil || !sharedauth.VerifyPassword(user.PasswordHash, request.Password) {
|
||||
writeJSONError(w, http.StatusUnauthorized, errInvalidCredentials.Error())
|
||||
return
|
||||
}
|
||||
@@ -318,7 +318,7 @@ func (a *AuthService) handleUpdateCredentials(w http.ResponseWriter, r *http.Req
|
||||
writeJSONError(w, http.StatusBadRequest, "Invalid JSON body")
|
||||
return
|
||||
}
|
||||
if request.CurrentPassword != nil && *request.CurrentPassword != "" && bcrypt.CompareHashAndPassword([]byte(user.PasswordHash), []byte(*request.CurrentPassword)) != nil {
|
||||
if request.CurrentPassword != nil && *request.CurrentPassword != "" && !sharedauth.VerifyPassword(user.PasswordHash, *request.CurrentPassword) {
|
||||
writeJSONError(w, http.StatusBadRequest, errCurrentPasswordBad.Error())
|
||||
return
|
||||
}
|
||||
@@ -347,7 +347,7 @@ func (a *AuthService) handleUpdateCredentials(w http.ResponseWriter, r *http.Req
|
||||
writeJSONError(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
hash, err := bcrypt.GenerateFromPassword([]byte(*request.NewPassword), bcrypt.DefaultCost)
|
||||
hash, err := sharedauth.HashPassword(*request.NewPassword)
|
||||
if err != nil {
|
||||
writeJSONError(w, http.StatusInternalServerError, "Internal server error")
|
||||
return
|
||||
|
||||
+6
-22
@@ -7,8 +7,9 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
sharedpostgres "repos.gfranz.ch/Gabriel20xx/SharedCode/backend/postgres"
|
||||
)
|
||||
|
||||
// DB owns the PostgreSQL pool. The schema deliberately uses the same physical
|
||||
@@ -19,13 +20,9 @@ type DB struct {
|
||||
}
|
||||
|
||||
func connectDB(ctx context.Context, connectionString string) (*DB, error) {
|
||||
pool, err := pgxpool.New(ctx, connectionString)
|
||||
pool, err := sharedpostgres.Open(ctx, sharedpostgres.DefaultConfig(connectionString))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create pool: %w", err)
|
||||
}
|
||||
if err := pool.Ping(ctx); err != nil {
|
||||
pool.Close()
|
||||
return nil, fmt.Errorf("ping database: %w", err)
|
||||
return nil, err
|
||||
}
|
||||
return &DB{pool: pool}, nil
|
||||
}
|
||||
@@ -35,19 +32,7 @@ func (d *DB) close() {
|
||||
}
|
||||
|
||||
func (d *DB) withTx(ctx context.Context, fn func(pgx.Tx) error) error {
|
||||
tx, err := d.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("begin transaction: %w", err)
|
||||
}
|
||||
defer tx.Rollback(ctx) // harmless after a successful commit
|
||||
|
||||
if err := fn(tx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return fmt.Errorf("commit transaction: %w", err)
|
||||
}
|
||||
return nil
|
||||
return sharedpostgres.WithTx(ctx, d.pool, fn)
|
||||
}
|
||||
|
||||
// initSchema is idempotent so an existing CollabTable PostgreSQL database can
|
||||
@@ -211,8 +196,7 @@ func (d *DB) createUser(ctx context.Context, username, email, passwordHash strin
|
||||
RETURNING id, username, email
|
||||
`, username, email, passwordHash).Scan(&user.ID, &user.Username, &user.Email)
|
||||
if err != nil {
|
||||
var pgErr *pgconn.PgError
|
||||
if errors.As(err, &pgErr) && pgErr.Code == "23505" {
|
||||
if sharedpostgres.IsUniqueViolation(err) {
|
||||
return nil, ErrSingletonUserExists
|
||||
}
|
||||
return nil, fmt.Errorf("create user: %w", err)
|
||||
|
||||
+5
-1
@@ -5,13 +5,17 @@ go 1.26.5
|
||||
require (
|
||||
github.com/jackc/pgx/v5 v5.10.0
|
||||
github.com/joho/godotenv v1.5.1
|
||||
golang.org/x/crypto v0.54.0
|
||||
)
|
||||
|
||||
require golang.org/x/crypto v0.54.0 // indirect
|
||||
|
||||
require (
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
||||
github.com/jackc/puddle/v2 v2.2.2 // indirect
|
||||
golang.org/x/sync v0.22.0 // indirect
|
||||
golang.org/x/text v0.40.0 // indirect
|
||||
repos.gfranz.ch/Gabriel20xx/SharedCode/backend v0.0.0
|
||||
)
|
||||
|
||||
replace repos.gfranz.ch/Gabriel20xx/SharedCode/backend => ../SharedCode/backend
|
||||
Reference in new issue
Block a user