Skip to content

Commit

Permalink
updated .env files
Browse files Browse the repository at this point in the history
  • Loading branch information
afurgapil committed Jun 22, 2024
1 parent aaef749 commit 104dc72
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 59 deletions.
8 changes: 8 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
MAIL_USER=
MAIL_PASSWORD=
MAIL_SERVICE=
DB_HOST=
DB_PORT=
DB_USER=
DB_PASSWORD=
DB_NAME=
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ go.work

/tmp
.DS_store
.env
.env.test
.env.development
29 changes: 22 additions & 7 deletions internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,35 @@ import (
"context"
"fmt"
"log"
"os"
"strconv"

"github.com/jackc/pgx/v4"
"github.com/joho/godotenv"
_ "github.com/lib/pq"
)

const (
host = "localhost"
port = 5432
user = "postgres"
password = "2412"
dbname = "librarymanagementsystem"
)
func init() {
envFile := "../../.env.development"

if err := godotenv.Load(envFile); err != nil {
log.Println("No .env.dev file found")
}
}

func Connect() (*pgx.Conn, error) {

host := os.Getenv("DB_HOST")
portStr := os.Getenv("DB_PORT")
user := os.Getenv("DB_USER")
password := os.Getenv("DB_PASSWORD")
dbname := os.Getenv("DB_NAME")

port, err := strconv.Atoi(portStr)
if err != nil {
return nil, fmt.Errorf("invalid port number: %v", err)
}

connStr := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", host, port, user, password, dbname)
conn, err := pgx.Connect(context.Background(), connStr)
if err != nil {
Expand Down
36 changes: 30 additions & 6 deletions pkg/testUtils/testUtilsMain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,59 @@ package testutils

import (
"context"
"fmt"
"log"
"os"
"strconv"
"testing"

"github.com/jackc/pgx/v4"
"github.com/joho/godotenv"
_ "github.com/lib/pq"
)

var DbConnection *pgx.Conn

func init() {
envFile := "../../.env.test"

if err := godotenv.Load(envFile); err != nil {
log.Println("No .env.test file found")
}
}
func InitializeDatabase() (*pgx.Conn, error) {
var err error
DbConnection, err = pgx.Connect(context.Background(), "postgres://postgres:test1234@localhost:5432/librarymanagementsystem_test")
host := os.Getenv("DB_HOST")
portStr := os.Getenv("DB_PORT")
user := os.Getenv("DB_USER")
password := os.Getenv("DB_PASSWORD")
dbname := os.Getenv("DB_NAME")

port, err := strconv.Atoi(portStr)
if err != nil {
return nil, fmt.Errorf("invalid port number: %v", err)
}

connStr := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", host, port, user, password, dbname)
conn, err := pgx.Connect(context.Background(), connStr)
if err != nil {
return nil, err
}

DbConnection = conn
log.Println("Successfully connected to the database.")
return DbConnection, nil
}

func CloseDatabase() {
if DbConnection != nil {
DbConnection.Close(context.Background())
DbConnection = nil
}
}

func TestMain(m *testing.M) {
var err error
DbConnection, err = InitializeDatabase()
if err != nil {
panic(err)
if _, err := InitializeDatabase(); err != nil {
log.Fatalf("could not initialize database: %v", err)
}
defer CloseDatabase()

Expand Down
91 changes: 46 additions & 45 deletions pkg/utils/resetPassword.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,62 +12,63 @@ import (
var resetPasswordJWTKey = []byte("your_secret_key_for_reset")

type ResetPasswordClaims struct {
StudentID string `json:"student_id"`
jwt.StandardClaims
StudentID string `json:"student_id"`
jwt.StandardClaims
}

func GeneratePasswordResetToken(studentID string) (string, error) {
expirationTime := time.Now().Add(1 * time.Hour)
claims := &ResetPasswordClaims{
StudentID: studentID,
StandardClaims: jwt.StandardClaims{
ExpiresAt: expirationTime.Unix(),
},
}

token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
tokenString, err := token.SignedString(resetPasswordJWTKey)
if err != nil {
return "", err
}

return tokenString, nil
expirationTime := time.Now().Add(1 * time.Hour)
claims := &ResetPasswordClaims{
StudentID: studentID,
StandardClaims: jwt.StandardClaims{
ExpiresAt: expirationTime.Unix(),
},
}

token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
tokenString, err := token.SignedString(resetPasswordJWTKey)
if err != nil {
return "", err
}

return tokenString, nil
}

func ValidatePasswordResetToken(tokenString string) (string, error) {
claims := &ResetPasswordClaims{}
claims := &ResetPasswordClaims{}

token, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) {
return resetPasswordJWTKey, nil
})
token, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) {
return resetPasswordJWTKey, nil
})

if err != nil {
return "", err
}
if err != nil {
return "", err
}

if !token.Valid {
return "", jwt.ErrSignatureInvalid
}
if !token.Valid {
return "", jwt.ErrSignatureInvalid
}

return claims.StudentID, nil
return claims.StudentID, nil
}

func SendPasswordResetEmail(email, token string) error {
err := godotenv.Load()
if err != nil {
panic(err)
}
mail := gomail.NewMessage()
mail.SetHeader("From", os.Getenv("MAIL_USER"))
mail.SetHeader("To", email)
mail.SetHeader("Subject", "Password Reset Request")
mail.SetBody("text/html", "To reset your password, click the following link: <a href=\"https://your-domain.com/reset-password/" + token + "\">Reset Password</a>")

dialer := gomail.NewDialer(os.Getenv("MAIL_SERVICE"), 587, os.Getenv("MAIL_USER"), os.Getenv("MAIL_PASSWORD"))

if err := dialer.DialAndSend(mail); err != nil {
return err
}

return nil
envFile := "../../.env.development"
err := godotenv.Load(envFile)
if err != nil {
panic(err)
}
mail := gomail.NewMessage()
mail.SetHeader("From", os.Getenv("MAIL_USER"))
mail.SetHeader("To", email)
mail.SetHeader("Subject", "Password Reset Request")
mail.SetBody("text/html", "To reset your password, click the following link: <a href=\"https://your-domain.com/reset-password/"+token+"\">Reset Password</a>")

dialer := gomail.NewDialer(os.Getenv("MAIL_SERVICE"), 587, os.Getenv("MAIL_USER"), os.Getenv("MAIL_PASSWORD"))

if err := dialer.DialAndSend(mail); err != nil {
return err
}

return nil
}

0 comments on commit 104dc72

Please sign in to comment.