FusionAuth Go Client
Go Client Library
The Go client library allows you to integrate FusionAuth with your Go application.
Source Code:
Installation
go get github.com/FusionAuth/go-client/pkg/fusionauth
Example Usage
package example
import (
"encoding/json"
"net/http"
"net/url"
"time"
"github.com/FusionAuth/go-client/pkg/fusionauth"
)
const host = "http://localhost:9011"
var apiKey = "YOUR_FUSIONAUTH_API_KEY"
var httpClient = &http.Client{
Timeout: time.Second * 10,
}
var baseURL, _ = url.Parse(host)
// Construct a new FusionAuth Client
var auth = fusionauth.NewClient(httpClient, baseURL, apiKey)
// Login logs in the user using the FusionAuth Go client library
func Login() http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Read response body
var credentials fusionauth.LoginRequest
defer r.Body.Close()
json.NewDecoder(r.Body).Decode(&credentials)
// Use FusionAuth Go client to log in the user
authResponse, errors, err := auth.Login(credentials)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Write the response from the FusionAuth client as JSON
var responseJSON []byte
if errors != nil {
responseJSON, err = json.Marshal(errors)
} else {
responseJSON, err = json.Marshal(authResponse)
}
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(responseJSON)
})
}
Related Posts
Example apps
- Golang jwt - JWT creation and decoding examples with the jwt-go library
- Golang jwt api gateway - Protecting a microservice with a JWT middleware
- Golang oauth - Login with the Authorization Code grant
- Golang device grant - A golang client using the device grant