34 lines
745 B
Go
34 lines
745 B
Go
package models
|
|
|
|
import (
|
|
"fmt"
|
|
"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 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")
|
|
}
|
|
|
|
var currentUserName string
|
|
err := DB.QueryRow(`
|
|
SELECT first_name || ' ' || last_name
|
|
FROM users
|
|
WHERE user_id = $1
|
|
`, currentUserID).Scan(¤tUserName)
|
|
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return currentUserName, nil
|
|
}
|