Skip to content
This repository has been archived by the owner on Nov 28, 2023. It is now read-only.

AletheiaWareLLC/authgo

Repository files navigation

authgo

authgo is an authentication library that makes it easy to add authentication to your webserver.

authgo is simple to setup and offers complete control of the HTML templates and stylesheets so your website can match your existing style and brand.

Getting Started

  1. Get the library
go get aletheiaware.com/authgo
  1. Create the Database.
// In a test environment use an In-Memory Database.
db := database.NewInMemoryDatabase()

// In production implement the Database interface to connect to your own database.
db := NewSqlDatabase()
  1. Create the Email Validator.
// In a test environment use a mock verifier (code is always authtest.TEST_CHALLENGE)
ev := authtest.NewEmailVerifier()

// In production use an SMTP service to send the verification code.
ev := email.NewSmtpEmailVerifier("smtp-relay.gmail.com:25", "example.com", "noreply@example.com", templates.Lookup("email-verification.go.html"))
  1. Create the Authenticator.
auth := authgo.NewAuthenticator(db, ev)
  1. Attach the HTTP Handlers with the HTML templates.
handler.AttachAuthenticationHandlers(mux, auth, templates)
  1. Add Authentication Checks to your HTTP Handlers.
mux.Handle("/greeter", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    account := auth.CurrentAccount(w, r)
    if account == nil {
        redirect.SignIn(w, r, r.URL.String())
        return
    }
    // Request is authorized, greet the user
    fmt.Fprintf(w, "Hello %s!", account.Username)
}))