fix: make an attempt to look for the token before adding to db

This commit is contained in:
Derrick Hammer 2023-08-04 12:54:45 -04:00
parent a7ac5a5b72
commit f11b285d4e
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 7 additions and 0 deletions

View File

@ -62,6 +62,13 @@ func generateAndSaveLoginToken(accountID uint, maxAge time.Duration) (string, er
Expiration: claim.ExpiresAt(),
}
existingSession := model.LoginSession{}
err = db.Get().Where("token = ?", token).First(&existingSession).Error
if err == nil {
return token, nil
}
if err := db.Get().Create(&session).Error; err != nil {
if strings.Contains(err.Error(), "Duplicate entry") {
return token, nil