2024-02-19 17:48:04 +00:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AuthClient struct {
|
|
|
|
URL string
|
|
|
|
}
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
Email string `json:"login"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAuthClient(url string) *AuthClient {
|
|
|
|
if url == "" {
|
|
|
|
log.Panicln("url is nil on <NewAuthClient>")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &AuthClient{
|
|
|
|
URL: url,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client *AuthClient) GetUserEmail(userID string) (string, error) {
|
|
|
|
fiberClient := fiber.Client{}
|
|
|
|
|
|
|
|
userURL := fmt.Sprintf("%s/%s", client.URL, userID)
|
|
|
|
|
|
|
|
var user User
|
|
|
|
status, resp, errs := fiberClient.Get(userURL).Struct(&user)
|
|
|
|
|
|
|
|
if len(errs) > 0 {
|
|
|
|
return "", errs[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
if status != fiber.StatusOK {
|
2024-03-13 16:57:12 +00:00
|
|
|
return "", fmt.Errorf("unexpected status code: %d, user: %s, reesponse: %s", status, userID, string(resp))
|
2024-02-19 17:48:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return user.Email, nil
|
|
|
|
}
|