Skip to content

Instantly share code, notes, and snippets.

@as27
Created March 11, 2017 19:04
Show Gist options
  • Save as27/fd01d74b56844a5afde87e1bb271824b to your computer and use it in GitHub Desktop.
Save as27/fd01d74b56844a5afde87e1bb271824b to your computer and use it in GitHub Desktop.
Buffalo Auth Tutorial: Checking if a user is logged in
// CheckAuth is the middlewar to check if a user is logged on.
func CheckAuth(next buffalo.Handler) buffalo.Handler {
return func(c buffalo.Context) error {
// Read the userID out of the session
userID := c.Session().Get("userID")
// If there is no userID redirect to the login page
if userID == nil {
err := c.Redirect(http.StatusTemporaryRedirect, "/login")
return err
}
// If not, call the next handler
err := next(c)
return err
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment