Refactor and remove sqlite and replace with MySQL
This commit is contained in:
16
internal/helpers/security/admin.go
Normal file
16
internal/helpers/security/admin.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package security
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"log"
|
||||
)
|
||||
|
||||
func IsAdmin(db *sql.DB, userID int) bool {
|
||||
var isAdmin bool
|
||||
err := db.QueryRow(`SELECT is_admin FROM users WHERE id = ?`, userID).Scan(&isAdmin)
|
||||
if err != nil {
|
||||
log.Printf("⚠️ Failed to check is_admin for user %d: %v", userID, err)
|
||||
return false
|
||||
}
|
||||
return isAdmin
|
||||
}
|
||||
13
internal/helpers/security/password.go
Normal file
13
internal/helpers/security/password.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package security
|
||||
|
||||
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(hash, password string) bool {
|
||||
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
|
||||
return err == nil
|
||||
}
|
||||
15
internal/helpers/security/token.go
Normal file
15
internal/helpers/security/token.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package security
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
)
|
||||
|
||||
func GenerateSecureToken() (string, error) {
|
||||
b := make([]byte, 32)
|
||||
_, err := rand.Read(b)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return hex.EncodeToString(b), nil
|
||||
}
|
||||
17
internal/helpers/security/users.go
Normal file
17
internal/helpers/security/users.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package security
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
httpHelpers "synlotto-website/helpers/http"
|
||||
)
|
||||
|
||||
func GetCurrentUserID(r *http.Request) (int, bool) {
|
||||
session, err := httpHelpers.GetSession(nil, r)
|
||||
if err != nil {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
id, ok := session.Values["user_id"].(int)
|
||||
return id, ok
|
||||
}
|
||||
Reference in New Issue
Block a user