Don't throw if user already exists on signup

This commit is contained in:
ekzyis 2024-07-12 20:30:34 +02:00
parent 0f3866866a
commit 4e13271dbd
2 changed files with 7 additions and 5 deletions

View File

@ -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)

View File

@ -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) {