first commit
This commit is contained in:
12
pkg/utis/colors.go
Normal file
12
pkg/utis/colors.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package utils
|
||||
|
||||
const (
|
||||
ColorReset = "\033[0m"
|
||||
ColorRed = "\033[31m"
|
||||
ColorGreen = "\033[32m"
|
||||
ColorYellow = "\033[33m"
|
||||
ColorBlue = "\033[34m"
|
||||
ColorPurple = "\033[35m"
|
||||
ColorCyan = "\033[36m"
|
||||
ColorWhite = "\033[37m"
|
||||
)
|
||||
58
pkg/utis/email.go
Normal file
58
pkg/utis/email.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
config "goGin/config"
|
||||
"net/smtp"
|
||||
)
|
||||
|
||||
func SendVerificationEmail(toEmail, token string) error {
|
||||
// Get config
|
||||
host := config.AppConfig.EmailHost
|
||||
port := config.AppConfig.EmailPort
|
||||
from := config.AppConfig.EmailFrom
|
||||
if from == "" {
|
||||
from = "noreply@gogin.local"
|
||||
}
|
||||
|
||||
// Construct verification link
|
||||
// Assuming frontend handles verification at /verify-email?token=...
|
||||
// Or backend endpoint directly: /api/v1/auth/verify-email?token=...
|
||||
// Let's use APP_URL from config
|
||||
// ApiAppURL
|
||||
// http://localhost:3000/auth/verify-email?token=TURNSITE_TOKEN_BURAYA
|
||||
// http://localhost:3000/auth/verify-email?token=e4dab20a06260dabee0b90f8a6e6e2f2b5d0aa45e36f04bbc41ba4943ced2fb8
|
||||
verifyLink := fmt.Sprintf("%s/auth/verify-email?token=%s", config.AppConfig.ApiAppURL, token)
|
||||
|
||||
// Email content
|
||||
subject := "Subject: Verify your email address\n"
|
||||
mime := "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n"
|
||||
body := fmt.Sprintf(`
|
||||
<html>
|
||||
<body>
|
||||
<h2>Welcome to GAuth-Central!</h2>
|
||||
<p>Please click the link below to verify your email address:</p>
|
||||
<p><a href="%s">Verify Email</a></p>
|
||||
<p>Or copy and paste this link: %s</p>
|
||||
</body>
|
||||
</html>
|
||||
`, verifyLink, verifyLink)
|
||||
|
||||
msg := []byte(subject + mime + body)
|
||||
|
||||
// Address
|
||||
addr := fmt.Sprintf("%s:%s", host, port)
|
||||
|
||||
// Auth (if needed)
|
||||
var auth smtp.Auth
|
||||
if config.AppConfig.EmailHostUser != "" && config.AppConfig.EmailHostPassword != "" {
|
||||
auth = smtp.PlainAuth("", config.AppConfig.EmailHostUser, config.AppConfig.EmailHostPassword, host)
|
||||
}
|
||||
|
||||
// Send email
|
||||
if err := smtp.SendMail(addr, auth, from, []string{toEmail}, msg); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
15
pkg/utis/password.go
Normal file
15
pkg/utis/password.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
func HashPassword(password string) (string, error) {
|
||||
bytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||
return string(bytes), err
|
||||
}
|
||||
|
||||
func CheckPasswordHash(password, hash string) bool {
|
||||
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
|
||||
return err == nil
|
||||
}
|
||||
15
pkg/utis/token.go
Normal file
15
pkg/utis/token.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
)
|
||||
|
||||
// GenerateSecureToken returns a cryptographically random hex string (e.g. for email verification).
|
||||
func GenerateSecureToken(byteLength int) (string, error) {
|
||||
b := make([]byte, byteLength)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return hex.EncodeToString(b), nil
|
||||
}
|
||||
Reference in New Issue
Block a user