From c6260f34b931bdda82d3dab256f4420caa1a45aa Mon Sep 17 00:00:00 2001 From: Horus3 Date: Fri, 13 Feb 2015 17:32:07 +0100 Subject: Add Login() and Register(). --- app/controllers/app.go | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) (limited to 'app/controllers/app.go') diff --git a/app/controllers/app.go b/app/controllers/app.go index e76d76b..87c8bd1 100644 --- a/app/controllers/app.go +++ b/app/controllers/app.go @@ -9,3 +9,70 @@ type App struct { func (c App) Index() revel.Result { return c.Render() } + +func (c App) Account() revel.Result { + return c.Render() +} + +func (c App) PrintLogin() revel.Result { + return c.Render() +} + +func (c App) Login(login, password string) revel.Result { + + c.Validation.Required(login).Message("Please provide a user name or email.") + c.Validation.Required(password).Message("Please enter a password.") + + if c.Validation.HasErrors() { + c.Validation.Keep() + c.FlashParams() + return c.Redirect(App.PrintLogin) + } + + u := User{} + db.Where("name = ?", login).Or("email = ?", login).Find(&u) + + c.Validation.Required(VerifyPassword(u.Password, password)).Message("User / Password combination did not matched.") + if c.Validation.HasErrors() { + c.Validation.Keep() + c.FlashParams() + return c.Redirect(App.PrintLogin) + } + + c.Session["login"] = "true" + c.Session["uid"] = string(u.Id) + + return c.Redirect(App.Account) +} + +func (c App) Register(email, confirmEmail, user, password, confirmPassword string) revel.Result { + c.Validation.Required(email).Message("Please provide a mail adress.") + c.Validation.Required(email == confirmEmail).Message("The mail adresses do not match.") + c.Validation.Required(user).Message("Please provide a user name.") + + if password != "" { + c.Validation.Required(password == confirmPassword).Message("Passwords do not match.") + } + + if c.Validation.HasErrors() { + c.Validation.Keep() + c.FlashParams() + return c.Redirect(App.PrintLogin) + } + + p, _ := HashPassword(password) + + user := User{ + Name: user, + Email: email, + Password: p, + } + + db.NewRecord(u) + db.Create(&u) + db.Save(&u) + + c.Flash.Success("Registration completed.") + + return c.Redirect(App.Account) +} -- cgit v1.2.3