From 14ca4920e1abe1abf36d998bb92918b361dc67b8 Mon Sep 17 00:00:00 2001 From: AKP Date: Wed, 31 Aug 2022 12:51:29 +0100 Subject: Replace faulty error assertion mattn/go-sqlite3 returns errors as `sqlite3.Error`, not `*sqlite3.Error` as was in use. This commit switches references of the latter with the former in user registration logic. This commit also removes some duplicated code from the user registration process. Fixes #2 Signed-off-by: AKP --- walrss/internal/core/users.go | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/walrss/internal/core/users.go b/walrss/internal/core/users.go index dfd2c86..5e7c80c 100644 --- a/walrss/internal/core/users.go +++ b/walrss/internal/core/users.go @@ -33,12 +33,7 @@ func RegisterUser(st *state.State, email, password string) (*db.User, error) { u.Password = hash - if _, err := st.Data.NewInsert().Model(u).Exec(context.Background()); err != nil { - if e, ok := err.(*sqlite3.Error); ok { - if e.Code == sqlite3.ErrConstraint { - return nil, NewUserError("email address in use") - } - } + if err := coreRegisterUser(st, u); err != nil { return nil, err } @@ -51,16 +46,23 @@ func RegisterUserOIDC(st *state.State, email string) (*db.User, error) { Email: email, } + if err := coreRegisterUser(st, u); err != nil { + return nil, err + } + + return u, nil +} + +func coreRegisterUser(st *state.State, u *db.User) error { if _, err := st.Data.NewInsert().Model(u).Exec(context.Background()); err != nil { - if e, ok := err.(*sqlite3.Error); ok { + if e, ok := err.(sqlite3.Error); ok { if e.Code == sqlite3.ErrConstraint { - return nil, NewUserError("email address in use") + return NewUserError("email address in use") } } - return nil, err + return err } - - return u, nil + return nil } func AreUserCredentialsCorrect(st *state.State, email, password string) (bool, error) { -- cgit v1.2.3-70-g09d2