diff --git a/datasrc.go b/datasrc.go index e554215e..295a4cac 100644 --- a/datasrc.go +++ b/datasrc.go @@ -655,7 +655,7 @@ func CreateWarriorPrivate(WarriorName string) (*Warrior, error) { } // CreateWarriorCorporal adds a new warrior corporal (registered) to the db -func CreateWarriorCorporal(WarriorName string, WarriorEmail string, WarriorPassword string) (NewWarrior *Warrior, VerifyID string, RegisterErr error) { +func CreateWarriorCorporal(WarriorName string, WarriorEmail string, WarriorPassword string, ActiveWarriorID string) (NewWarrior *Warrior, VerifyID string, RegisterErr error) { hashedPassword, hashErr := HashAndSalt([]byte(WarriorPassword)) if hashErr != nil { return nil, "", hashErr @@ -665,16 +665,31 @@ func CreateWarriorCorporal(WarriorName string, WarriorEmail string, WarriorPassw var verifyID string WarriorRank := "CORPORAL" - e := db.QueryRow( - `SELECT warriorId, verifyId FROM register_warrior($1, $2, $3, $4);`, - WarriorName, - WarriorEmail, - hashedPassword, - WarriorRank, - ).Scan(&WarriorID, &verifyID) - if e != nil { - log.Println(e) - return nil, "", errors.New("a warrior with that email already exists") + if ActiveWarriorID != "" { + e := db.QueryRow( + `SELECT warriorId, verifyId FROM register_existing_warrior($1, $2, $3, $4, $5);`, + ActiveWarriorID, + WarriorName, + WarriorEmail, + hashedPassword, + WarriorRank, + ).Scan(&WarriorID, &verifyID) + if e != nil { + log.Println(e) + return nil, "", errors.New("a warrior with that email already exists") + } + } else { + e := db.QueryRow( + `SELECT warriorId, verifyId FROM register_warrior($1, $2, $3, $4);`, + WarriorName, + WarriorEmail, + hashedPassword, + WarriorRank, + ).Scan(&WarriorID, &verifyID) + if e != nil { + log.Println(e) + return nil, "", errors.New("a warrior with that email already exists") + } } return &Warrior{WarriorID: WarriorID, WarriorName: WarriorName, WarriorEmail: WarriorEmail, WarriorRank: WarriorRank}, verifyID, nil diff --git a/handlers.go b/handlers.go index 287cd96b..a0bbab29 100644 --- a/handlers.go +++ b/handlers.go @@ -143,6 +143,8 @@ func EnlistWarriorHandler(w http.ResponseWriter, r *http.Request) { return } + ActiveWarriorID, _ := ValidateWarriorCookie(w, r) + WarriorName, WarriorEmail, WarriorPassword, accountErr := ValidateWarriorAccount( keyVal["warriorName"], keyVal["warriorEmail"], @@ -155,7 +157,7 @@ func EnlistWarriorHandler(w http.ResponseWriter, r *http.Request) { return } - newWarrior, VerifyID, err := CreateWarriorCorporal(WarriorName, WarriorEmail, WarriorPassword) + newWarrior, VerifyID, err := CreateWarriorCorporal(WarriorName, WarriorEmail, WarriorPassword, ActiveWarriorID) if err != nil { w.WriteHeader(http.StatusInternalServerError) return diff --git a/schema.sql b/schema.sql index a52d7ae0..ff12b27b 100644 --- a/schema.sql +++ b/schema.sql @@ -88,7 +88,7 @@ CREATE TYPE WarriorsVote AS CREATE OR REPLACE PROCEDURE deactivate_all_warriors() LANGUAGE plpgsql AS $$ BEGIN - UPDATE battles_warriors SET active = false WHERE active = true; + UPDATE battles_warriors SET active = false, last_active = NOW() WHERE active = true; END; $$; @@ -253,7 +253,7 @@ BEGIN RAISE 'Valid Reset ID not found'; END IF; - UPDATE warriors SET password = warriorPassword WHERE id = matchedWarriorId; + UPDATE warriors SET password = warriorPassword, last_active = NOW() WHERE id = matchedWarriorId; DELETE FROM warrior_reset WHERE reset_id = resetId; COMMIT; @@ -264,7 +264,7 @@ $$; CREATE OR REPLACE PROCEDURE update_warrior_password(warriorId UUID, warriorPassword TEXT) LANGUAGE plpgsql AS $$ BEGIN - UPDATE warriors SET password = warriorPassword WHERE id = warriorId; + UPDATE warriors SET password = warriorPassword, last_active = NOW() WHERE id = warriorId; COMMIT; END; @@ -288,7 +288,7 @@ BEGIN RAISE 'Valid Verify ID not found'; END IF; - UPDATE warriors SET verified = 'TRUE' WHERE id = matchedWarriorId; + UPDATE warriors SET verified = 'TRUE', last_active = NOW() WHERE id = matchedWarriorId; DELETE FROM warrior_verify WHERE verify_id = verifyId; COMMIT; @@ -369,3 +369,30 @@ BEGIN INSERT INTO warrior_verify (warrior_id) VALUES (warriorId) RETURNING verify_id INTO verifyId; END; $$ LANGUAGE plpgsql; + +-- Register a new warrior from existing private +DROP FUNCTION IF EXISTS register_existing_warrior(UUID, VARCHAR, VARCHAR, TEXT, VARCHAR); +CREATE FUNCTION register_existing_warrior( + IN activeWarriorId UUID, + IN warriorName VARCHAR(64), + IN warriorEmail VARCHAR(320), + IN hashedPassword TEXT, + IN warriorRank VARCHAR(128), + OUT warriorId UUID, + OUT verifyId UUID +) +AS $$ +BEGIN + UPDATE warriors + SET + name = warriorName, + email = warriorEmail, + password = hashedPassword, + rank = warriorRank, + last_active = NOW() + WHERE id = activeWarriorId + RETURNING id INTO warriorId; + + INSERT INTO warrior_verify (warrior_id) VALUES (warriorId) RETURNING verify_id INTO verifyId; +END; +$$ LANGUAGE plpgsql; diff --git a/src/pages/Register.svelte b/src/pages/Register.svelte index c1efa527..b6724370 100644 --- a/src/pages/Register.svelte +++ b/src/pages/Register.svelte @@ -8,7 +8,7 @@ export let notifications export let battleId - let warriorName = '' + let warriorName = $warrior.name || '' let warriorEmail = '' let warriorPassword1 = '' let warriorPassword2 = '' @@ -137,7 +137,8 @@