first commit
This commit is contained in:
53
services/email_service.go
Normal file
53
services/email_service.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/smtp"
|
||||
"strings"
|
||||
|
||||
configs "goFiber/config"
|
||||
)
|
||||
|
||||
type EmailService struct{}
|
||||
|
||||
func NewEmailService() *EmailService {
|
||||
return &EmailService{}
|
||||
}
|
||||
|
||||
func (s *EmailService) Send(to, subject, body string) error {
|
||||
host := strings.TrimSpace(configs.AppConfig.EmailHost)
|
||||
port := strings.TrimSpace(configs.AppConfig.EmailPort)
|
||||
from := strings.TrimSpace(configs.AppConfig.EmailFrom)
|
||||
|
||||
if host == "" || port == "" || from == "" {
|
||||
return fmt.Errorf("email configuration is incomplete")
|
||||
}
|
||||
|
||||
addr := host + ":" + port
|
||||
username := strings.TrimSpace(configs.AppConfig.EmailHostUser)
|
||||
password := strings.TrimSpace(configs.AppConfig.EmailHostPassword)
|
||||
|
||||
var auth smtp.Auth
|
||||
if username != "" && password != "" {
|
||||
auth = smtp.PlainAuth("", username, password, host)
|
||||
}
|
||||
|
||||
message := "From: " + from + "\r\n" +
|
||||
"To: " + to + "\r\n" +
|
||||
"Subject: " + subject + "\r\n" +
|
||||
"MIME-Version: 1.0\r\n" +
|
||||
"Content-Type: text/plain; charset=UTF-8\r\n\r\n" +
|
||||
body
|
||||
|
||||
return smtp.SendMail(addr, auth, from, []string{to}, []byte(message))
|
||||
}
|
||||
|
||||
func (s *EmailService) SendVerificationEmail(to, firstName, verifyURL string) error {
|
||||
subject := "Email verification"
|
||||
body := fmt.Sprintf(
|
||||
"Hi %s,\n\nPlease verify your email by opening this link:\n%s\n\nIf you did not create this account, you can ignore this email.",
|
||||
firstName,
|
||||
verifyURL,
|
||||
)
|
||||
return s.Send(to, subject, body)
|
||||
}
|
||||
121
services/jwt_service.go
Normal file
121
services/jwt_service.go
Normal file
@@ -0,0 +1,121 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
configs "goFiber/config"
|
||||
"log"
|
||||
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
)
|
||||
|
||||
const (
|
||||
TokenTypeAccess = "access"
|
||||
TokenTypeRefresh = "refresh"
|
||||
)
|
||||
|
||||
type JWTClaim struct {
|
||||
UserID uint `json:"user_id"`
|
||||
Email string `json:"email"`
|
||||
IsAdmin bool `json:"is_admin"`
|
||||
FirstName string `json:"first_name"`
|
||||
LastName string `json:"last_name"`
|
||||
TokenType string `json:"token_type"`
|
||||
jwt.RegisteredClaims
|
||||
}
|
||||
|
||||
type JWTService struct{}
|
||||
|
||||
func NewJWTService() *JWTService {
|
||||
return &JWTService{}
|
||||
}
|
||||
|
||||
func (s *JWTService) GenerateToken(
|
||||
userID uint,
|
||||
email string,
|
||||
isAdmin bool,
|
||||
firstName string,
|
||||
lastName string,
|
||||
tokenType string,
|
||||
expiration time.Duration,
|
||||
) (string, error) {
|
||||
now := time.Now()
|
||||
claims := &JWTClaim{
|
||||
UserID: userID,
|
||||
Email: email,
|
||||
IsAdmin: isAdmin,
|
||||
FirstName: firstName,
|
||||
LastName: lastName,
|
||||
TokenType: tokenType,
|
||||
RegisteredClaims: jwt.RegisteredClaims{
|
||||
Subject: strconv.FormatUint(uint64(userID), 10),
|
||||
ExpiresAt: jwt.NewNumericDate(now.Add(expiration)),
|
||||
IssuedAt: jwt.NewNumericDate(now),
|
||||
},
|
||||
}
|
||||
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||
return token.SignedString([]byte(configs.AppConfig.JWTSecret))
|
||||
}
|
||||
|
||||
func (s *JWTService) GenerateTokenPair(
|
||||
userID uint,
|
||||
email string,
|
||||
isAdmin bool,
|
||||
firstName string,
|
||||
lastName string,
|
||||
) (string, string, error) {
|
||||
access, err := s.GenerateToken(
|
||||
userID,
|
||||
email,
|
||||
isAdmin,
|
||||
firstName,
|
||||
lastName,
|
||||
TokenTypeAccess,
|
||||
time.Duration(configs.AppConfig.AccessTokenExpireMinutes)*time.Minute,
|
||||
)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
refresh, err := s.GenerateToken(
|
||||
userID,
|
||||
email,
|
||||
isAdmin,
|
||||
firstName,
|
||||
lastName,
|
||||
TokenTypeRefresh,
|
||||
time.Duration(configs.AppConfig.RefreshTokenExpireDays)*24*time.Hour,
|
||||
)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
// Log generated tokens (access + refresh)
|
||||
log.Printf("Generated token pair for user=%d email=%s access_exp=%dm refresh_exp=%dd", userID, email, configs.AppConfig.AccessTokenExpireMinutes, configs.AppConfig.RefreshTokenExpireDays)
|
||||
log.Printf("access: %s", access)
|
||||
log.Printf("refresh: %s", refresh)
|
||||
|
||||
return access, refresh, nil
|
||||
}
|
||||
|
||||
func (s *JWTService) ValidateToken(signedToken string) (*JWTClaim, error) {
|
||||
token, err := jwt.ParseWithClaims(signedToken, &JWTClaim{}, func(token *jwt.Token) (interface{}, error) {
|
||||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
return nil, errors.New("unexpected signing method")
|
||||
}
|
||||
return []byte(configs.AppConfig.JWTSecret), nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
claims, ok := token.Claims.(*JWTClaim)
|
||||
if !ok || !token.Valid {
|
||||
return nil, errors.New("invalid token claims")
|
||||
}
|
||||
|
||||
return claims, nil
|
||||
}
|
||||
Reference in New Issue
Block a user