Files
Poll-system/app/internal/models/token.go

32 lines
658 B
Go
Raw Normal View History

2025-08-26 14:13:09 -06:00
package models
import (
"fmt"
2025-08-27 13:21:11 -06:00
"net/http"
2025-08-26 14:13:09 -06:00
)
2025-08-27 13:21:11 -06:00
func GetCurrentUserID(w http.ResponseWriter, r *http.Request)(int){
currentUserID := r.Context().Value("user_id").(int)
return currentUserID
}
2025-08-26 14:13:09 -06:00
2025-08-27 13:21:11 -06:00
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")
}
2025-08-26 14:13:09 -06:00
2025-08-27 13:21:11 -06:00
var currentUserName string
err := DB.QueryRow(`
2025-08-28 17:09:23 -06:00
SELECT first_name
2025-08-27 13:21:11 -06:00
FROM users
WHERE user_id = $1
`, currentUserID).Scan(&currentUserName)
2025-08-26 14:13:09 -06:00
if err != nil {
2025-08-27 13:21:11 -06:00
return "", err
2025-08-26 14:13:09 -06:00
}
2025-08-27 13:21:11 -06:00
return currentUserName, nil
}