Login

The Login utility handles user authentication by verifying their credentials and returning a session token if the credentials are valid.

Note that while the backend previously used the username field to provide the email address, this has been deprecated in favor of the email field.

API Reference

Examples

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"
	"time"
)

const BaseURI = "https://hyperhuman.deemos.com/api"

var client = &http.Client{Timeout: 10 * time.Second}

type CommonError struct {
	Error *string `json:"error,omitempty"`
}

type UserBasicInformation struct {
	UserUUID  string `json:"user_uuid,omitempty"`
	Username  string `json:"username,omitempty"`
	AvatarUrl string `json:"avatar_url,omitempty"`
	Email     string `json:"email,omitempty"`
}

// LoginResponse represents the response from the login endpoint.
type LoginResponse struct {
	CommonError
	UserBasicInformation
	Token string `json:"token,omitempty"`
}

// Login performs JWT-based authentication.
func Login(email, password string) (string, *UserBasicInformation, error) {
	payload := map[string]string{"password": password, "email": email}

	jsonData, err := json.Marshal(payload)
	if err != nil {
		return "", nil, err
	}

	req, err := http.NewRequest("POST", fmt.Sprintf("%s/user/login", BaseURI), bytes.NewBuffer(jsonData))
	if err != nil {
		return "", nil, err
	}

	req.Header.Set("Content-Type", "application/json")

	resp, err := client.Do(req)
	if err != nil {
		return "", nil, err
	}
	defer resp.Body.Close()

	var responseData LoginResponse
	err = json.NewDecoder(resp.Body).Decode(&responseData)
	if err != nil {
		return "", nil, err
	}

	if responseData.Error != nil {
		return "", nil, fmt.Errorf("error: %s", *responseData.Error)
	}

	return responseData.Token, &responseData.UserBasicInformation, nil
}

func main() {
	email := "example@example.com"
	password := "<your password>"
	token, info, _ := Login(email, password)
	fmt.Println("Logged in successfully. Token:", token, "BasicInformation:", *info)
}

Last updated