Skip to content

Instantly share code, notes, and snippets.

Created March 18, 2015 23:59
What would you like to do?
// Check if the user is logged in, true if they do
func IsLoggedIn(r *http.Request) bool {
// Obtains cookie from users http.Request
cookie, err := r.Cookie("SessionID")
if err != nil {
return false
// Obtain sessionID from cookie we obtained earlier
sessionID := cookie.Value
// Split the sessionID to Username and ID (username+random)
z := strings.Split(sessionID, ":")
email := z[0]
sessionID = z[1]
// Returns the expectedSessionID from the database
expectedSessionID, errz := lookupSessionID(email)
if errz != "" {
return false
// If SessionID matches the expected SessionID, it is Good
if sessionID == expectedSessionID {
// If you want to be really secure check IP
return true
return false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment