Skip to content

Instantly share code, notes, and snippets.

@ejcx
Last active January 2, 2016 20:08
Show Gist options
  • Save ejcx/2faad4e61bf71849a520 to your computer and use it in GitHub Desktop.
Save ejcx/2faad4e61bf71849a520 to your computer and use it in GitHub Desktop.
Go Authentication Design Patterns
package main
import (
"log"
"net/http"
"os"
"github.com/gorilla/sessions"
)
var store = sessions.NewCookieStore([]byte(os.Getenv("SESSION-INTEGRITY")))
func authenticated(w http.ResponseWriter, r *http.Request) {
if session, err := store.Get(r, "SESSIONID"); err == nil {
if _, ok := session.Values["loggedin"]; !ok {
w.WriteHeader(http.StatusUnauthorized)
return
}
w.Write([]byte("Why yes, you are logged in!"))
} else {
w.WriteHeader(http.StatusInternalServerError)
}
}
func main() {
http.HandleFunc("/authenticated", authenticated)
log.Fatal(http.ListenAndServe(":8080", nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment