Login

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

API Reference

Login with given credentials.

post

Maybe Recaptcha required. Return session token if success.

Authorizations
Body
usernamestring · max: 80Optional
emailstring · emailOptional
passwordstringRequired
Responses
201Success
application/json
post
POST /api/user/login HTTP/1.1
Host: 
Authorization: Bearer JWT
Content-Type: application/json
Accept: */*
Content-Length: 62

{
  "username": "text",
  "email": "[email protected]",
  "password": "text"
}
201Success
{
  "error": "ALREADY_LOGGEDIN",
  "token": "text",
  "username": "text",
  "user_uuid": "text",
  "avatar_url": "text"
}

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 := "[email protected]"
	password := "<your password>"
	token, info, _ := Login(email, password)
	fmt.Println("Logged in successfully. Token:", token, "BasicInformation:", *info)
}

Last updated