Created
March 18, 2015 23:59
-
-
Save lettergram/bf3217849a0f4ee6a1c4 to your computer and use it in GitHub Desktop.
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
// 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