admin core func done
This commit is contained in:
@@ -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(¤tUserName)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return "", err
|
||||
}
|
||||
|
||||
if !token.Valid {
|
||||
return nil, fmt.Errorf("invalid token")
|
||||
}
|
||||
|
||||
return claims, nil
|
||||
}
|
||||
return currentUserName, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user