From a3c913b501d4dbe44f3302583e2cd4bae7b82018 Mon Sep 17 00:00:00 2001 From: Cody Krist <137073012+cmkrist@users.noreply.github.com> Date: Thu, 30 Oct 2025 11:41:45 +0000 Subject: [PATCH] added new env variable for email validation toggle --- .env.example | 1 + config/config.go | 8 ++++++++ wsm/wsm.go | 34 +++++++++++++++++----------------- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/.env.example b/.env.example index da7a661..614e1ef 100644 --- a/.env.example +++ b/.env.example @@ -4,3 +4,4 @@ DB_PORT=5432 DB_USER=postgres DB_PASSWORD=mypasswd1234 DB_NAME=myappdb +OC_EMAIL_VALIDATION=true \ No newline at end of file diff --git a/config/config.go b/config/config.go index 7d3383a..adfa7c1 100644 --- a/config/config.go +++ b/config/config.go @@ -7,6 +7,7 @@ import ( "fmt" "log" "os" + "strings" "time" "github.com/jackc/pgx/v5/pgxpool" @@ -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 != "" { diff --git a/wsm/wsm.go b/wsm/wsm.go index b4c6a2a..d5de1eb 100644 --- a/wsm/wsm.go +++ b/wsm/wsm.go @@ -19,6 +19,7 @@ import ( "golang.org/x/crypto/bcrypt" "openchamp/server/portmanager" + "openchamp/server/config" ) type ClientManager struct { @@ -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 {