diff --git a/internal/api/auth.go b/internal/api/auth.go index 6954c494e..7ff4d44dc 100644 --- a/internal/api/auth.go +++ b/internal/api/auth.go @@ -46,9 +46,8 @@ func AuthEndpoints(router *gin.RouterGroup) { // CallbackError(c, err.Error(), http.StatusInternalServerError) // return //} - user := entity.CreateOrUpdateExternalUser(u) - if user == nil { - e := errors.New("api: server error. Check backend logs") + user, e := entity.CreateOrUpdateExternalUser(u) + if e != nil { c.Error(e) CallbackError(c, e.Error(), http.StatusInternalServerError) return diff --git a/internal/entity/user.go b/internal/entity/user.go index f5f1c51ba..bad8b35fa 100644 --- a/internal/entity/user.go +++ b/internal/entity/user.go @@ -165,7 +165,7 @@ func FirstOrCreateUser(m *User) *User { } // CreateOrUpdateExternalUser Retrieves User by its ExternalID and updates relevant properties. If no User exists, a new one will be inserted into database. -func CreateOrUpdateExternalUser(m *User) *User { +func CreateOrUpdateExternalUser(m *User) (*User, error) { result := User{} if err := Db().Preload("Address").Where("external_id = ?", m.ExternalID).First(&result).Error; err == nil { @@ -182,30 +182,30 @@ func CreateOrUpdateExternalUser(m *User) *User { err := result.Validate() if err != nil { log.Errorf("user: %s", err) - return nil + return nil, err } err = result.Save() if err != nil { log.Errorf("user: %s", err) - return nil + return nil, err } } log.Debugf("user: retrieved %q from db", result.UserName) - return &result + return &result, nil } else { err := m.Validate() if err != nil { log.Errorf("user: %s", err) - return nil + return nil, err } if err := m.Create(); err != nil { log.Debugf("user: %s", err) - return nil + return nil, err } log.Debugf("user: created %q", result.UserName) } - return m + return m, nil } // FindUserByName returns an existing user or nil if not found.