I am batman
This commit is contained in:
85
internal/utils/emailUtils.go
Normal file
85
internal/utils/emailUtils.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/smtp"
|
||||
"os"
|
||||
|
||||
_ "github.com/joho/godotenv/autoload"
|
||||
)
|
||||
|
||||
func SendVerificationEmail(to string, token string) error {
|
||||
from := os.Getenv("EMAIL_FROM")
|
||||
password := os.Getenv("EMAIL_PASSWORD")
|
||||
smtpHost := os.Getenv("SMTP_HOST")
|
||||
smtpPort := os.Getenv("SMTP_PORT")
|
||||
|
||||
verificationLink := fmt.Sprintf("%s/api/v1/verify/%s", os.Getenv("APP_URL"), token)
|
||||
|
||||
subject := "Verify Your Email"
|
||||
body := fmt.Sprintf(`
|
||||
<html>
|
||||
<body>
|
||||
<h2>Welcome to OpsMastery!</h2>
|
||||
<p>Please verify your email address by clicking the link below:</p>
|
||||
<a href="%s">Verify Email</a>
|
||||
<p>If you didn't create this account, please ignore this email.</p>
|
||||
</body>
|
||||
</html>
|
||||
`, verificationLink)
|
||||
|
||||
message := fmt.Sprintf("To: %s\r\n"+
|
||||
"Subject: %s\r\n"+
|
||||
"MIME-Version: 1.0\r\n"+
|
||||
"Content-Type: text/html; charset=UTF-8\r\n"+
|
||||
"\r\n"+
|
||||
"%s\r\n", to, subject, body)
|
||||
|
||||
auth := smtp.PlainAuth("", from, password, smtpHost)
|
||||
addr := fmt.Sprintf("%s:%s", smtpHost, smtpPort)
|
||||
|
||||
err := smtp.SendMail(addr, auth, from, []string{to}, []byte(message))
|
||||
if err != nil {
|
||||
log.Printf("SMTP error: %v", err) // Add this line for more details
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func SendPasswordResetEmail(to string, token string) error {
|
||||
from := os.Getenv("EMAIL_FROM")
|
||||
password := os.Getenv("EMAIL_PASSWORD")
|
||||
smtpHost := os.Getenv("SMTP_HOST")
|
||||
smtpPort := os.Getenv("SMTP_PORT")
|
||||
|
||||
//resetLink := fmt.Sprintf("%s/auth/reset-password?reset_token=%s", os.Getenv("FRONTEND_URL"), token)
|
||||
|
||||
resetLink := fmt.Sprintf("http://localhost:3000/auth/reset-password?reset_token=%s", token)
|
||||
|
||||
log.Printf("Generated reset link: %s", resetLink)
|
||||
|
||||
subject := "Reset Your Password"
|
||||
body := fmt.Sprintf(`
|
||||
<html>
|
||||
<body>
|
||||
<h2>Password Reset Request</h2>
|
||||
<p>Click the link below to reset your password:</p>
|
||||
<a href="%s">Reset Password</a>
|
||||
<p>If you didn't request this, please ignore this email.</p>
|
||||
<p>This link will expire in 1 hour.</p>
|
||||
</body>
|
||||
</html>
|
||||
`, resetLink)
|
||||
|
||||
message := fmt.Sprintf("To: %s\r\n"+
|
||||
"Subject: %s\r\n"+
|
||||
"MIME-Version: 1.0\r\n"+
|
||||
"Content-Type: text/html; charset=UTF-8\r\n"+
|
||||
"\r\n"+
|
||||
"%s\r\n", to, subject, body)
|
||||
|
||||
auth := smtp.PlainAuth("", from, password, smtpHost)
|
||||
addr := fmt.Sprintf("%s:%s", smtpHost, smtpPort)
|
||||
|
||||
return smtp.SendMail(addr, auth, from, []string{to}, []byte(message))
|
||||
}
|
||||
63
internal/utils/jwtUtils.go
Normal file
63
internal/utils/jwtUtils.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"OpsMastery.v5/internal/models"
|
||||
"github.com/golang-jwt/jwt/v4"
|
||||
)
|
||||
|
||||
var (
|
||||
accessTokenSecret = []byte(os.Getenv("JWT_ACCESS_SECRET"))
|
||||
refreshTokenSecret = []byte(os.Getenv("JWT_REFRESH_SECRET"))
|
||||
)
|
||||
|
||||
func GenerateJWT(user models.User) (string, string, error) {
|
||||
accessClaims := jwt.MapClaims{
|
||||
"sub": user.ID,
|
||||
"email": user.Email,
|
||||
"role": user.Role,
|
||||
"exp": time.Now().Add(time.Minute * 15).Unix(),
|
||||
}
|
||||
|
||||
accessToken := jwt.NewWithClaims(jwt.SigningMethodHS256, accessClaims)
|
||||
signedAccessToken, err := accessToken.SignedString(accessTokenSecret)
|
||||
if err != nil {
|
||||
log.Println("Error generating access token:", err)
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
refreshClaims := jwt.MapClaims{
|
||||
"sub": user.ID,
|
||||
"exp": time.Now().Add(time.Hour * 24 * 7).Unix(),
|
||||
}
|
||||
|
||||
refreshToken := jwt.NewWithClaims(jwt.SigningMethodHS256, refreshClaims)
|
||||
signedRefreshToken, err := refreshToken.SignedString(refreshTokenSecret)
|
||||
if err != nil {
|
||||
log.Println("Error generating refresh token:", err)
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
return signedAccessToken, signedRefreshToken, nil
|
||||
}
|
||||
|
||||
func ValidateJWT(tokenString string, isRefreshToken bool) (jwt.MapClaims, error) {
|
||||
secret := accessTokenSecret
|
||||
if isRefreshToken {
|
||||
secret = refreshTokenSecret
|
||||
}
|
||||
|
||||
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
|
||||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
return nil, jwt.NewValidationError("invalid signing method", jwt.ValidationErrorClaimsInvalid)
|
||||
}
|
||||
return secret, nil
|
||||
})
|
||||
if err != nil || !token.Valid {
|
||||
return nil, err
|
||||
}
|
||||
return token.Claims.(jwt.MapClaims), nil
|
||||
}
|
||||
15
internal/utils/tokenUtils.go
Normal file
15
internal/utils/tokenUtils.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
)
|
||||
|
||||
func GenerateRandomToken() string {
|
||||
bytes := make([]byte, 16) // 16 bytes = 128 bits
|
||||
_, err := rand.Read(bytes)
|
||||
if err != nil {
|
||||
panic("Failed to generate random token")
|
||||
}
|
||||
return hex.EncodeToString(bytes)
|
||||
}
|
||||
Reference in New Issue
Block a user