Skip to content

Instantly share code, notes, and snippets.

@lettergram
Created March 18, 2015 23:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lettergram/bf3217849a0f4ee6a1c4 to your computer and use it in GitHub Desktop.
Save lettergram/bf3217849a0f4ee6a1c4 to your computer and use it in GitHub Desktop.
// 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 {
fmt.Println(err)
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 != "" {
fmt.Println(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