updated for using .env

This commit is contained in:
Mann Patel
2025-08-27 17:54:45 -06:00
parent 09093aa70a
commit 9f260c3821
11 changed files with 83 additions and 32 deletions

View File

@@ -4,18 +4,18 @@ import (
"database/sql"
"log"
"net/http"
"os"
"strconv"
"time"
"github.com/golang-jwt/jwt/v5"
"github.com/joho/godotenv"
"golang.org/x/crypto/bcrypt"
"github.com/patel-mann/poll-system/app/internal/models"
"github.com/patel-mann/poll-system/app/internal/utils"
)
var jwtKey = []byte("your-secret-key") //TODO: Move to env/config
// Helper function to get redirect URL based on role
func getDefaultRedirectURL(role int) string {
switch role {
@@ -49,6 +49,18 @@ func renderRegisterError(w http.ResponseWriter, errorMsg string) {
// Helper function to create and sign JWT token
func createJWTToken(userID, role int) (string, time.Time, error) {
err := godotenv.Load() // or specify path: godotenv.Load("/path/to/.env")
if err != nil {
log.Fatalf("Error loading .env file: %v", err)
}
// Get individual components from environment variables
jwtSecret := os.Getenv("JWT_SECRET")
var jwtKey = []byte(jwtSecret) //TODO: Move to env/config
expirationTime := time.Now().Add(12 * time.Hour)
claims := &models.Claims{
UserID: userID,