This repository has been archived on 2024-03-23. You can view files and clone it, but cannot push or open issues or pull requests.
ambition-legacy/frontend/server/requests.go
Abdulmujeeb Raji a1f6d8fe7f feat(frontend): get login token from server
As of right now, we don't actually do anything with the token. But at
least we're actually able to get it. 😅

Caveats in implementation:
- UI now needs a pointer to player
- .env is now required for server auth (mentioned in README.md)
2023-08-04 14:55:53 +01:00

40 lines
740 B
Go

package server
import (
"os"
"errors"
"bytes"
"io/ioutil"
"encoding/json"
"net/http"
// Needed for server url
_ "github.com/joho/godotenv/autoload"
)
var server_address = os.Getenv("BACKEND_URL")
func GetUserJwtToken(name, password string) (string, error) {
if server_address == "" {
return "", errors.New("no server url provided")
}
post_body, _ := json.Marshal(map[string]string{
"name": name,
"password": password,
})
resp_body := bytes.NewBuffer(post_body)
resp, err := http.Post(server_address + "/user", "application/json", resp_body)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(body), nil
}