added password reset system
This commit is contained in:
10
internal/repositories/user_permissions.go
Normal file
10
internal/repositories/user_permissions.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"GoMembership/internal/database"
|
||||
"GoMembership/internal/models"
|
||||
)
|
||||
|
||||
func (r *UserRepository) SetUserStatus(id uint, status uint) error {
|
||||
return database.DB.Model(&models.User{}).Where("id = ?", id).Update("status", status).Error
|
||||
}
|
||||
@@ -3,7 +3,6 @@ package repositories
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
|
||||
"GoMembership/internal/constants"
|
||||
"GoMembership/internal/database"
|
||||
|
||||
"gorm.io/gorm/clause"
|
||||
@@ -18,10 +17,12 @@ type UserRepositoryInterface interface {
|
||||
UpdateUser(user *models.User) (*models.User, error)
|
||||
GetUsers(where map[string]interface{}) (*[]models.User, error)
|
||||
GetUserByEmail(email string) (*models.User, error)
|
||||
SetVerificationToken(verification *models.Verification) (uint, error)
|
||||
IsVerified(userID *uint) (bool, error)
|
||||
GetVerificationOfToken(token *string) (*models.Verification, error)
|
||||
GetVerificationOfToken(token *string, verificationType *string) (*models.Verification, error)
|
||||
SetVerificationToken(verification *models.Verification) (token string, err error)
|
||||
DeleteVerification(id uint, verificationType string) error
|
||||
DeleteUser(id uint) error
|
||||
SetUserStatus(id uint, status uint) error
|
||||
}
|
||||
|
||||
type UserRepository struct{}
|
||||
@@ -156,42 +157,3 @@ func (ur *UserRepository) GetUserByEmail(email string) (*models.User, error) {
|
||||
}
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
func (ur *UserRepository) IsVerified(userID *uint) (bool, error) {
|
||||
var user models.User
|
||||
result := database.DB.Select("status").First(&user, userID)
|
||||
if result.Error != nil {
|
||||
if result.Error == gorm.ErrRecordNotFound {
|
||||
return false, gorm.ErrRecordNotFound
|
||||
}
|
||||
return false, result.Error
|
||||
}
|
||||
return user.Status != constants.UnverifiedStatus, nil
|
||||
}
|
||||
|
||||
func (ur *UserRepository) GetVerificationOfToken(token *string) (*models.Verification, error) {
|
||||
|
||||
var emailVerification models.Verification
|
||||
result := database.DB.Where("verification_token = ?", token).First(&emailVerification)
|
||||
if result.Error != nil {
|
||||
if result.Error == gorm.ErrRecordNotFound {
|
||||
return nil, gorm.ErrRecordNotFound
|
||||
}
|
||||
return nil, result.Error
|
||||
}
|
||||
return &emailVerification, nil
|
||||
}
|
||||
|
||||
func (ur *UserRepository) SetVerificationToken(verification *models.Verification) (uint, error) {
|
||||
// Use GORM to insert or update the Verification record
|
||||
result := database.DB.Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "user_id"}},
|
||||
DoUpdates: clause.AssignmentColumns([]string{"verification_token", "created_at"}),
|
||||
}).Create(&verification)
|
||||
|
||||
if result.Error != nil {
|
||||
return 0, result.Error
|
||||
}
|
||||
|
||||
return verification.ID, nil
|
||||
}
|
||||
|
||||
57
internal/repositories/user_verification.go
Normal file
57
internal/repositories/user_verification.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"GoMembership/internal/constants"
|
||||
"GoMembership/internal/database"
|
||||
"GoMembership/internal/models"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
func (ur *UserRepository) IsVerified(userID *uint) (bool, error) {
|
||||
var user models.User
|
||||
result := database.DB.Select("status").First(&user, userID)
|
||||
if result.Error != nil {
|
||||
if result.Error == gorm.ErrRecordNotFound {
|
||||
return false, gorm.ErrRecordNotFound
|
||||
}
|
||||
return false, result.Error
|
||||
}
|
||||
return user.Status > constants.DisabledStatus, nil
|
||||
}
|
||||
|
||||
func (ur *UserRepository) GetVerificationOfToken(token *string, verificationType *string) (*models.Verification, error) {
|
||||
|
||||
var emailVerification models.Verification
|
||||
result := database.DB.Where("verification_token = ? AND type = ?", token, verificationType).First(&emailVerification)
|
||||
if result.Error != nil {
|
||||
if result.Error == gorm.ErrRecordNotFound {
|
||||
return nil, gorm.ErrRecordNotFound
|
||||
}
|
||||
return nil, result.Error
|
||||
}
|
||||
return &emailVerification, nil
|
||||
}
|
||||
|
||||
func (ur *UserRepository) SetVerificationToken(verification *models.Verification) (token string, err error) {
|
||||
|
||||
result := database.DB.Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "user_id"}},
|
||||
DoUpdates: clause.AssignmentColumns([]string{"verification_token", "created_at", "type"}),
|
||||
}).Create(&verification)
|
||||
|
||||
if result.Error != nil {
|
||||
return "", result.Error
|
||||
}
|
||||
|
||||
return verification.VerificationToken, nil
|
||||
}
|
||||
|
||||
func (ur *UserRepository) DeleteVerification(id uint, verificationType string) error {
|
||||
result := database.DB.Where("user_id = ? AND type = ?", id, verificationType).Delete(&models.Verification{})
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user