Repository navigation
Quick start guide
In this guide we add login and a small authenticated response. Every return route runs the same authentication setup.
Use Go 1.21 or later:
go get github.com/Authwave/go-clientCreate a deployment in the provider and configure its client return URI. Set the complete client key in AUTHWAVE_CLIENT_KEY. The final underscore separates deployment ID from the raw 32-byte secret. Keep the value unchanged on the server, outside public files and version control.
For this example configure http://localhost:3000/ as the client return URI.
Create main.go:
package main
import (
"encoding/json"
authwave "github.com/Authwave/go-client"
"log"
"net/http"
"os"
)
func main() {
key := os.Getenv("AUTHWAVE_CLIENT_KEY")
if key == "" {
log.Fatal("Set AUTHWAVE_CLIENT_KEY")
}
handler, err := authwave.NewHTTPHandler(authwave.HTTPOptions{
ClientKey: key, Store: authwave.NewMemorySessionStore(), InsecureCookie: true, // Local HTTP only.
}, func(auth *authwave.Authenticator, r *http.Request) (*authwave.Response, error) {
switch r.URL.Path {
case "/login":
auth.SetReturnURI("/")
if err := auth.Login(nil); err != nil {
return nil, err
}
case "/logout":
destination := "/"
if err := auth.Logout(&destination, nil); err != nil {
return nil, err
}
}
result := map[string]string{"login": "/login"}
if auth.IsLoggedIn() {
user, err := auth.GetAuthUser()
if err != nil {
return nil, err
}
result = map[string]string{"id": user.ID(), "email": user.Email()}
}
body, err := json.Marshal(result)
if err != nil {
return nil, err
}
return &authwave.Response{Header: http.Header{"Content-Type": []string{"application/json"}}, Body: body}, nil
})
if err != nil {
log.Fatal(err)
}
log.Fatal(http.ListenAndServe("127.0.0.1:3000", handler))
}Run go run ., then visit http://localhost:3000/.
The example uses a development memory store and local HTTP cookies. Production applications use HTTPS and a shared persistent store. Cookies are Secure unless InsecureCookie is enabled.
The /login route requests / as the return destination and starts authentication. The return route uses the same store and authentication setup, allowing it to process the provider's response before rendering content.
Return authentication errors to the adapter: a RedirectError tells it to save the session and send a redirect. An already logged-in login call returns normally.
The home route returns the authenticated ID and email, or the login route when signed out. JSON keeps these examples independent of HTML rendering. Escape user values when rendering HTML.
Visit /login, complete authentication, and follow the provider return and cleanup redirect. Visit /logout to clear local authentication and the provider session.
The handler works with http.ServeMux and other routing libraries by dispatching inside the callback. Keep every callback return route inside the same authentication wrapper.
Next, see Working with the authenticated user.
Authwave’s Go client connects your application to an Authwave provider.