// Process login action func (self *AuthController) ProcessLogin(w http.ResponseWriter, req *http.Request, r render.Render, di *di.DI) { email := req.FormValue("Email") user := di.UserManager().FindActiveByEmail(email) if user == nil { // not authenticated // message: Invalid credentials r.Redirect("/auth/") return } password := req.FormValue("Password") if di.UserManager().CheckPassword(user, password) { // message: Hello, {userName} authManager := di.AuthManager() token := authManager.GenerateToken(user) // set cookie cookie := http.Cookie{Name: "gousertoken", Value: token, Path: "/"} http.SetCookie(w, &cookie) r.Redirect("/users/") } else { // not authorized // message: Invalid credentials r.Redirect("/auth/") } }
// Show login template func (self *AuthController) Login(r render.Render, di *di.DI, templateVars utils.TemplateVars) { authManager := di.AuthManager() if authManager.IsAuthenticated() { r.HTML(403, "error/403", templateVars) return } r.HTML(200, "auth/login", templateVars) }
// Register Action, show register template form func (self *RegisterController) Register(params martini.Params, r render.Render, di *di.DI, templateVars utils.TemplateVars) { authManager := di.AuthManager() if authManager.IsAuthenticated() { r.HTML(403, "error/403", templateVars) return } r.HTML(200, "register/register", templateVars) }
/** * Show edit template * Route /users/edit/profile/ */ func (u *UserController) Edit(r render.Render, di *di.DI, templateVars utils.TemplateVars) { authManager := di.AuthManager() if !authManager.IsAuthenticated() { r.HTML(403, "error/403", templateVars) return } currentUser := authManager.CurrentUser() templateVars.SetData(currentUser) r.HTML(200, "user/edit", templateVars) }
// Show login template func (self *AuthController) LogOut(w http.ResponseWriter, params martini.Params, r render.Render, di *di.DI, templateVars utils.TemplateVars) { authManager := di.AuthManager() if !authManager.IsAuthenticated() { r.HTML(403, "error/403", templateVars) return } authManager.Logout() cookie := http.Cookie{Name: "gousertoken", Value: "", Path: "/", MaxAge: -1} http.SetCookie(w, &cookie) r.Redirect("/users/") }
// Show login template func (self *UpdateLogController) List(params martini.Params, r render.Render, di *di.DI, templateVars utils.TemplateVars) { authManager := di.AuthManager() if !authManager.IsAuthenticated() { r.HTML(403, "error/403", templateVars) return } currentUser := authManager.CurrentUser() userId := currentUser.Id logs := di.UpdateLogManager().FindAll(userId) templateVars.SetData(logs) r.HTML(200, "updateLog/list", templateVars) }
/** * Save user * Route /users/save/profile/ */ func (u *UserController) Save(req *http.Request, r render.Render, di *di.DI, templateVars utils.TemplateVars) { authManager := di.AuthManager() if !authManager.IsAuthenticated() { r.HTML(403, "error/403", templateVars) return } userManager := di.UserManager() currentUser := authManager.CurrentUser() // clone user newUser := *currentUser newUser.FullName = req.FormValue("FullName") newUser.Address = req.FormValue("Address") newUser.Phone = req.FormValue("Phone") userManager.Update(&newUser) // store user updates to log di.UpdateLogManager().StoreChanges(currentUser, &newUser) // redirect strId := strconv.FormatInt(currentUser.Id, 10) r.Redirect("/users/" + strId + "/view/") }