admin core func done

This commit is contained in:
Mann Patel
2025-08-27 13:21:11 -06:00
parent 9148f011ad
commit 6edd4ee030
29 changed files with 2152 additions and 1139 deletions

View File

@@ -2,27 +2,32 @@ package models
import (
"fmt"
"github.com/golang-jwt/jwt/v5"
"net/http"
)
var jwtKey = []byte("your-secret-key") //TODO: Move to env/config
func GetCurrentUserID(w http.ResponseWriter, r *http.Request)(int){
currentUserID := r.Context().Value("user_id").(int)
return currentUserID
}
func ExtractClaims(tokenStr string) (*Claims, error) {
claims := &Claims{}
func GetCurrentUserName(r *http.Request) (string, error) {
currentUserID, ok := r.Context().Value("user_id").(int)
if !ok {
return "", fmt.Errorf("user_id not found in context")
}
token, err := jwt.ParseWithClaims(tokenStr, claims, func(token *jwt.Token) (interface{}, error) {
return jwtKey, nil
})
var currentUserName string
err := DB.QueryRow(`
SELECT first_name || ' ' || last_name
FROM users
WHERE user_id = $1
`, currentUserID).Scan(&currentUserName)
if err != nil {
return nil, err
return "", err
}
if !token.Valid {
return nil, fmt.Errorf("invalid token")
}
return claims, nil
}
return currentUserName, nil
}