summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHorus32015-02-13 17:32:07 +0100
committerHorus32015-02-13 17:32:07 +0100
commitc6260f34b931bdda82d3dab256f4420caa1a45aa (patch)
treeb92bc5e06670653d58321a21fe764a1f03caaf98
parentd6ab45e8ec5f26004b4efc07383245051d546bf3 (diff)
downloadstatuspage-c6260f34b931bdda82d3dab256f4420caa1a45aa.tar.gz
Add Login() and Register().
-rw-r--r--app/controllers/app.go67
-rw-r--r--app/controllers/utilities.go7
2 files changed, 71 insertions, 3 deletions
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)
+}
diff --git a/app/controllers/utilities.go b/app/controllers/utilities.go
index 99720b8..2d07bdf 100644
--- a/app/controllers/utilities.go
+++ b/app/controllers/utilities.go
@@ -87,10 +87,11 @@ func HashPassword(password string) (string, error) {
}
// Verify password and hash
-func VerifyPassword(password, hash string) (bool, error) {
+func VerifyPassword(password, hash string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
if err != nil {
- return false, err
+ revel.ERROR.Printf("%s \n", err)
+ return false
}
- return true, nil
+ return true
}