Created
March 11, 2017 19:04
-
-
Save as27/fd01d74b56844a5afde87e1bb271824b to your computer and use it in GitHub Desktop.
Buffalo Auth Tutorial: Checking if a user is logged in
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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