Skip to content

Commit e35fe71

Browse files
committed
better handling if account is partially signed up
1 parent 68a4bc8 commit e35fe71

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

controller.go

+3
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,9 @@ func signup(w http.ResponseWriter, r *http.Request) {
317317
}
318318
}
319319

320+
//check if user exists than was not activated yet. In that case, resend the email and don't try to insert
321+
//the user, as this would fail due to constraints
322+
320323
err = insertUser(cred.Email, calcPw, emailToken, refreshToken, flowType, timeNow())
321324
if err != nil {
322325
writeErr(w, http.StatusBadRequest, "invalid_request", "blocked", "ERR-signup-07, insert user failed: %v", err)

db.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,11 @@ func insertUser(email string, pwRaw []byte, emailToken string, refreshToken stri
5555
s1 := base32.StdEncoding.EncodeToString(pwRaw)
5656
pw = &s1
5757
}
58-
stmt, err := db.Prepare(`INSERT INTO auth (email, password, email_token, refresh_token, flow_type, created_at)
59-
VALUES ($1, $2, $3, $4, $5, $6)`)
58+
stmt, err := db.Prepare(`INSERT INTO auth as a (email, password, email_token, refresh_token, flow_type, created_at)
59+
VALUES ($1, $2, $3, $4, $5, $6)
60+
ON CONFLICT (email) DO
61+
UPDATE SET password=$2, email_token = $3
62+
WHERE a.email_token IS NOT NULL`)
6063
if err != nil {
6164
return fmt.Errorf("prepare INSERT INTO auth for %v statement failed: %v", email, err)
6265
}

main_test.go

+5-8
Original file line numberDiff line numberDiff line change
@@ -65,25 +65,22 @@ func TestSignupWrongEmail(t *testing.T) {
6565
shutdown()
6666
}
6767

68-
func TestSignupTwice(t *testing.T) {
68+
func TestSignupTwiceWorking(t *testing.T) {
6969
shutdown := mainTest(testParams...)
7070
resp := doSignup("[email protected]", "testtest")
71-
resp.Body.Close()
7271
resp = doSignup("[email protected]", "testtest")
7372

74-
bodyBytes, _ := ioutil.ReadAll(resp.Body)
75-
bodyString := string(bodyBytes)
76-
77-
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
78-
assert.True(t, strings.Index(bodyString, "ERR-signup-07") > 0)
73+
assert.Equal(t, http.StatusOK, resp.StatusCode)
7974

8075
resp.Body.Close()
8176
shutdown()
8277
}
8378

84-
func TestSignupWrong(t *testing.T) {
79+
func TestSignupTwiceNotWorking(t *testing.T) {
8580
shutdown := mainTest(testParams...)
8681
resp := doSignup("[email protected]", "testtest")
82+
token := token("[email protected]")
83+
resp = doConfirm("[email protected]", token)
8784
resp = doSignup("[email protected]", "testtest")
8885

8986
bodyBytes, _ := ioutil.ReadAll(resp.Body)

0 commit comments

Comments
 (0)