From 4e13271dbd3f70cf31dba416e89fd7c76f148eed Mon Sep 17 00:00:00 2001 From: ekzyis Date: Fri, 12 Jul 2024 20:30:34 +0200 Subject: [PATCH] Don't throw if user already exists on signup --- server/router/handler/auth.go | 5 ++++- server/router/handler/lnauth_test.go | 7 +++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/server/router/handler/auth.go b/server/router/handler/auth.go index ad7bad2..5ffcf32 100644 --- a/server/router/handler/auth.go +++ b/server/router/handler/auth.go @@ -111,7 +111,10 @@ func HandleLnAuthCallback(sc context.Context) echo.HandlerFunc { } if query.Action == "register" { - err = tx.QueryRow("INSERT INTO users(ln_pubkey) VALUES ($1) RETURNING id", query.Key).Scan(&userId) + err = tx.QueryRow(""+ + "INSERT INTO users(ln_pubkey) VALUES ($1) "+ + "ON CONFLICT(ln_pubkey) DO UPDATE SET ln_pubkey = $1 "+ + "RETURNING id", query.Key).Scan(&userId) if err != nil { tx.Rollback() pqErr, ok = err.(*pq.Error) diff --git a/server/router/handler/lnauth_test.go b/server/router/handler/lnauth_test.go index c092216..4fc2046 100644 --- a/server/router/handler/lnauth_test.go +++ b/server/router/handler/lnauth_test.go @@ -147,7 +147,7 @@ func TestLnAuthSignupCallbackUserExists(t *testing.T) { key = hex.EncodeToString(pk.SerializeCompressed()) - // create user such that signup must fail + // create user before signup _, err = db.Exec("INSERT INTO users(ln_pubkey) VALUES($1) RETURNING id", key) assert.NoError(err, "error creating user") @@ -158,10 +158,9 @@ func TestLnAuthSignupCallbackUserExists(t *testing.T) { nil) c = e.NewContext(req, rec) - // must throw error because user already exists + // does not throw an error for UX reasons handler.HandleLnAuthCallback(sc)(c) - assert.Equal(http.StatusBadRequest, rec.Code, "wrong status code") - assert.Contains(rec.Body.String(), "\"reason\":\"user already exists\"", "user check failed") + assert.Equal(http.StatusOK, rec.Code, "wrong status code") } func TestLnAuthLogin(t *testing.T) {