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.
- Get the library
go get aletheiaware.com/authgo
- 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()
- 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"))
- Create the Authenticator.
auth := authgo.NewAuthenticator(db, ev)
- Attach the HTTP Handlers with the HTML templates.
handler.AttachAuthenticationHandlers(mux, auth, templates)
- 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)
}))