feat: add resolver for token verification

Resolves #6
This commit is contained in:
Lakhan Samani
2021-07-14 01:36:11 +05:30
parent 04a522c947
commit eeb8f7d097
9 changed files with 454 additions and 59 deletions

View File

@@ -16,7 +16,10 @@ type Manager interface {
AddUser(user User) (User, error)
GetUsers() ([]User, error)
GetUserByEmail(email string) (User, error)
UpdateVerificationTime(verifiedAt int64, email string) error
AddVerification(verification Verification) (Verification, error)
GetVerificationByToken(token string) (Verification, error)
DeleteToken(email string) error
}
type manager struct {

View File

@@ -60,3 +60,13 @@ func (mgr *manager) GetUserByEmail(email string) (User, error) {
return user, nil
}
func (mgr *manager) UpdateVerificationTime(verifiedAt int64, email string) error {
result := mgr.db.Model(&User{}).Where("email = ?", email).Update("email_verified_at", verifiedAt)
if result.Error != nil {
return result.Error
}
return nil
}

View File

@@ -7,8 +7,8 @@ import (
)
type Verification struct {
ID uint `gorm:"primaryKey"`
Token string
ID uint `gorm:"primaryKey"`
Token string `gorm:"index"`
Identifier string
ExpiresAt int64
CreatedAt int64 `gorm:"autoCreateTime"`
@@ -28,3 +28,27 @@ func (mgr *manager) AddVerification(verification Verification) (Verification, er
}
return verification, nil
}
func (mgr *manager) GetVerificationByToken(token string) (Verification, error) {
var verification Verification
result := mgr.db.Where("token = ?", token).First(&verification)
if result.Error != nil {
log.Println(`Error getting verification token:`, result.Error)
return verification, result.Error
}
return verification, nil
}
func (mgr *manager) DeleteToken(email string) error {
var verification Verification
result := mgr.db.Where("email = ?", email).Delete(&verification)
if result.Error != nil {
log.Println(`Error deleting token:`, result.Error)
return result.Error
}
return nil
}