feat(portal): agregar flujo de recuperación de contraseña
- Nuevo modelo PortalPasswordResetToken con token seguro (32 bytes, 1h vigencia) - AutoMigrate del nuevo modelo en main.go - SendPortalPasswordResetEmail con diseño consistente al app - Handlers: PortalForgotPasswordPage/Post y PortalResetPasswordPost/Page - Rutas públicas GET/POST /portal/forgot-password y /portal/reset-password - Vistas forgot_password.html y reset_password.html con validación JS - Enlace ¿Olvidaste tu contraseña? en login.html + soporte mensaje success Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
co-authored by
Copilot
parent
b5e6a9285f
commit
6e27032ac6
@@ -1,7 +1,10 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/sujit-baniya/fiber-boilerplate/app"
|
||||
"gorm.io/gorm"
|
||||
@@ -196,6 +199,58 @@ func GetClienteIDsForPortalUser(u *PortalUser) []uint {
|
||||
return []uint{}
|
||||
}
|
||||
|
||||
// ─── PortalPasswordResetToken ────────────────────────────────────────────────
|
||||
|
||||
type PortalPasswordResetToken struct {
|
||||
ID uint `gorm:"primaryKey;autoIncrement"`
|
||||
PortalUserID uint `gorm:"column:portal_user_id;index;not null"`
|
||||
Token string `gorm:"column:token;uniqueIndex;not null"`
|
||||
ExpiresAt time.Time `gorm:"column:expires_at;not null"`
|
||||
Used bool `gorm:"column:used;default:false"`
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
func (PortalPasswordResetToken) TableName() string { return "portal_password_reset_tokens" }
|
||||
|
||||
// CreatePortalResetToken genera un token aleatorio seguro de 32 bytes, lo persiste y lo retorna.
|
||||
func CreatePortalResetToken(portalUserID uint) (string, error) {
|
||||
b := make([]byte, 32)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
return "", err
|
||||
}
|
||||
token := hex.EncodeToString(b)
|
||||
record := &PortalPasswordResetToken{
|
||||
PortalUserID: portalUserID,
|
||||
Token: token,
|
||||
ExpiresAt: time.Now().Add(1 * time.Hour),
|
||||
}
|
||||
if err := app.Http.Database.DB.Create(record).Error; err != nil {
|
||||
return "", err
|
||||
}
|
||||
return token, nil
|
||||
}
|
||||
|
||||
// GetValidPortalResetToken busca el token, verifica que no esté usado ni vencido.
|
||||
func GetValidPortalResetToken(token string) (*PortalPasswordResetToken, error) {
|
||||
var record PortalPasswordResetToken
|
||||
err := app.Http.Database.DB.Where("token = ?", token).First(&record).Error
|
||||
if err != nil {
|
||||
return nil, errors.New("token inválido")
|
||||
}
|
||||
if record.Used {
|
||||
return nil, errors.New("este enlace ya fue utilizado")
|
||||
}
|
||||
if time.Now().After(record.ExpiresAt) {
|
||||
return nil, errors.New("el enlace ha expirado, solicita uno nuevo")
|
||||
}
|
||||
return &record, nil
|
||||
}
|
||||
|
||||
// MarkPortalResetTokenUsed marca el token como utilizado.
|
||||
func MarkPortalResetTokenUsed(id uint) error {
|
||||
return app.Http.Database.DB.Model(&PortalPasswordResetToken{}).Where("id = ?", id).Update("used", true).Error
|
||||
}
|
||||
|
||||
// CheckPortalLogin valida credenciales y retorna el portal user.
|
||||
func CheckPortalLogin(email, password string) (*PortalUser, error) {
|
||||
u, err := GetPortalUserByEmail(email)
|
||||
|
||||
Reference in New Issue
Block a user