48 lines
786 B
Go
48 lines
786 B
Go
package client
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
|
|
"crispbyte.dev/sig-auth/server"
|
|
)
|
|
|
|
func RegisterKey(baseUrl *url.URL, key string, userId string) (string, error) {
|
|
request := server.RegisterRequest{
|
|
UserId: userId,
|
|
Key: key,
|
|
}
|
|
|
|
json_data, _ := json.Marshal(request)
|
|
|
|
registerUrl := baseUrl.JoinPath("register")
|
|
|
|
resp, err := http.DefaultClient.Post(
|
|
registerUrl.String(),
|
|
"application/json",
|
|
bytes.NewBuffer(json_data))
|
|
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
out, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
bodyContent := string(out[:])
|
|
|
|
if resp.StatusCode >= 300 {
|
|
return "", fmt.Errorf("bad status: %d - %s", resp.StatusCode, bodyContent)
|
|
}
|
|
|
|
return bodyContent, nil
|
|
}
|