Auth: Return error value from CreateOrUpdateUser() #98

This commit is contained in:
Timo Volkmann 2021-09-08 14:35:29 +02:00
parent 5d26325b7d
commit d7aa739d26
2 changed files with 9 additions and 10 deletions

View file

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

View file

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