Created
March 19, 2015 00:02
-
-
Save lettergram/49c765f82a47d8a2cadb 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
// Handles the users login and gives them a cookie for doing so | |
func loginHandler(w http.ResponseWriter, r *http.Request) { | |
// Generate a new User Struct | |
usr := new(user.User) | |
// Read the email value from the form | |
usr.Email = r.FormValue("email") | |
// Read the password value from the form | |
pass := r.FormValue("pwd") | |
// Find the user profile in the user database | |
userProfile := user.FindUser(usr.Email) | |
if len(pass) > 0 { | |
// Encode the password for check - Late blogpost will review this | |
usr.Password = codify.SHA(pass) | |
// Check to see if the credentials match the database | |
ok := user.CheckCredentials(usr.Email, usr.Password) | |
if ok { | |
usr = userProfile | |
// Create a custom user page, to be displayed | |
user.CreateUserFile(usr.Email) | |
// Generates a cookie for the user (so they don't have to login again) | |
cookie := cookies.LoginCookie(usr.Email) | |
// Sends Cookie | |
http.SetCookie(w, &cookie) | |
// Gives the user a session id | |
usr.SessionID = cookie.Value | |
// Updates user data in database, w/ session ID | |
_ = user.UpdateUser(usr) | |
// Redirects the user to /login-succeeded | |
http.Redirect(w, r, "/login-succeeded", http.StatusFound) | |
} else { | |
http.Redirect(w, r, "/login-failed", http.StatusFound) | |
} | |
} else { | |
viewHandler(w, r) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment