Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ DB_PORT=5432
DB_USER=postgres
DB_PASSWORD=mypasswd1234
DB_NAME=myappdb
OC_EMAIL_VALIDATION=true
8 changes: 8 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"log"
"os"
"strings"
"time"

"github.com/jackc/pgx/v5/pgxpool"
Expand All @@ -21,6 +22,13 @@ const (
MaxGamePort = 8000
)

var ValidateEmails = func() bool {
v := os.Getenv("OC_EMAIL_VALIDATION")
if v == "" { return false }
lv := strings.ToLower(v)
return lv == "true"
}()

// Database connection string construction
var dbConnString = func() string {
if v := os.Getenv("DB_CONN"); v != "" {
Expand Down
34 changes: 17 additions & 17 deletions wsm/wsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"golang.org/x/crypto/bcrypt"

"openchamp/server/portmanager"
"openchamp/server/config"
)

type ClientManager struct {
Expand Down Expand Up @@ -423,25 +424,24 @@ func (client *Client) handleRegistration(msg Message) {
return
}

/* === EMAIL CHECKING (TURN ON IN PROD) === */
// Check if email already exists (if provided)
/* === EMAIL CHECKING (controlled by env) === */
// Check if email already exists (if provided) when enabled via config.ValidateEmails
if config.ValidateEmails && registration.Email != "" {
err = client.dbPool.QueryRow(ctx,
"SELECT EXISTS(SELECT 1 FROM users WHERE email = $1)",
registration.Email).Scan(&exists)

// if registration.Email != "" {
// err = client.dbPool.QueryRow(ctx,
// "SELECT EXISTS(SELECT 1 FROM users WHERE email = $1)",
// registration.Email).Scan(&exists)

// if err != nil {
// log.Printf("Database error during registration: %v", err)
// client.sendRegistrationError("Registration failed due to a server error")
// return
// }
if err != nil {
log.Printf("Database error during registration: %v", err)
client.sendRegistrationError("Registration failed due to a server error")
return
}

// if exists {
// client.sendRegistrationError("Email already registered")
// return
// }
// }
if exists {
client.sendRegistrationError("Email already registered")
return
}
}

hashedPw, err := bcrypt.GenerateFromPassword([]byte(registration.Password), bcrypt.DefaultCost)
if err != nil {
Expand Down