Skip to content

Commit

Permalink
Keep active warrior ID when registering
Browse files Browse the repository at this point in the history
Closes #55 by keeping active warrior ID when registering form private to corporal.
  • Loading branch information
StevenWeathers committed Apr 3, 2020
1 parent 25de458 commit 7319af9
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 21 deletions.
37 changes: 26 additions & 11 deletions datasrc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand All @@ -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
Expand Down
35 changes: 31 additions & 4 deletions schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;
$$;

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
12 changes: 7 additions & 5 deletions src/pages/Register.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
export let notifications
export let battleId
let warriorName = ''
let warriorName = $warrior.name || ''
let warriorEmail = ''
let warriorPassword1 = ''
let warriorPassword2 = ''
Expand Down Expand Up @@ -137,15 +137,16 @@
<div class="text-center px-2 mb-4">
<h1 class="text-3xl md:text-4xl font-bold">Enlist to Battle</h1>
</div>
<div class="flex flex-wrap">
<div class="flex flex-wrap justify-center">
{#if !$warrior.id}
<div class="w-full md:w-1/2 px-4">
<form
on:submit="{createWarriorPrivate}"
class="bg-white shadow-lg rounded p-4 md:p-6 mb-4"
name="registerGuest">
<h2
class="font-bold text-xl md:text-2xl b-4 mb-2 md:mb-6
md:leading-tight">
md:leading-tight text-center">
Register as Guest
</h2>

Expand Down Expand Up @@ -177,6 +178,7 @@
</div>
</form>
</div>
{/if}

<div class="w-full md:w-1/2 px-4">
<form
Expand All @@ -185,8 +187,8 @@
name="createAccount">
<h2
class="font-bold text-xl md:text-2xl mb-2 md:mb-6
md:leading-tight">
Create an Account (optional)
md:leading-tight text-center">
Create an Account <span class="text-gray-500">(optional)</span>
</h2>

<div class="mb-4">
Expand Down

0 comments on commit 7319af9

Please sign in to comment.