summaryrefslogtreecommitdiff
path: root/register.go
diff options
context:
space:
mode:
authorKevin Chabowski <kevin@kch42.de>2013-08-30 15:11:50 +0200
committerKevin Chabowski <kevin@kch42.de>2013-08-30 15:11:50 +0200
commit3a501d85a560b2e2c5c5ded47cc431e4f1315d21 (patch)
tree4a339c9c992645d69b22e592b12ccb3e051a0d40 /register.go
parentbf1207d7f3a4e95124c5f1498f96fad298e496a6 (diff)
downloadmailremind-3a501d85a560b2e2c5c5ded47cc431e4f1315d21.tar.gz
mailremind-3a501d85a560b2e2c5c5ded47cc431e4f1315d21.tar.bz2
mailremind-3a501d85a560b2e2c5c5ded47cc431e4f1315d21.zip
Defined own handler type to reduce repetive jobs.
Diffstat (limited to 'register.go')
-rw-r--r--register.go130
1 files changed, 67 insertions, 63 deletions
diff --git a/register.go b/register.go
index 936f540..38f7b97 100644
--- a/register.go
+++ b/register.go
@@ -2,6 +2,7 @@ package main
import (
"code.google.com/p/go.crypto/bcrypt"
+ "github.com/gorilla/sessions"
"kch42.de/gostuff/mailremind/model"
"log"
"math/rand"
@@ -33,69 +34,72 @@ func genAcCode() string {
return string(code)
}
-func register(rw http.ResponseWriter, req *http.Request) {
+func register(user model.User, sess *sessions.Session, req *http.Request) interface{} {
outdata := &registerData{Timezones: &timeLocs}
- defer func() {
- if err := tplRegister.Execute(rw, outdata); err != nil {
- log.Printf("Exec tplRegister: %s", err)
- }
- }()
-
- if req.Method == "POST" {
- if err := req.ParseForm(); err != nil {
- outdata.Error = "Data of form could not be understand. If this happens again, please contact support!"
- return
- }
-
- indata := new(registerFormdata)
- if err := formdec.Decode(indata, req.Form); (err != nil) || (indata.Mail == "") || (indata.Timezone.Loc == nil) {
- outdata.Error = "Input data wrong or missing. Please fill in all values and make sure to provide a valid E-Mail address."
- return
- }
-
- if indata.Password == "" {
- outdata.Error = "Empty passwords are not allowed."
- return
- }
-
- if indata.Password != indata.RetypePassword {
- outdata.Error = "Passwords are not identical."
- return
- }
-
- mail := string(indata.Mail)
-
- switch _, err := dbcon.UserByMail(mail); err {
- case nil:
- outdata.Error = "This E-Mail address is already used."
- return
- case model.NotFound:
- default:
- log.Printf("Error while checking, if mail is used: %s", err)
- outdata.Error = "Internal error, sorry. If this happens again, please contact support!"
- return
- }
-
- acCode := genAcCode()
- pwhash, err := bcrypt.GenerateFromPassword([]byte(indata.Password), bcrypt.DefaultCost)
- if err != nil {
- log.Printf("Error while hashing password: %s", err)
- outdata.Error = "Internal error, sorry. If this happens again, please contact support!"
- return
- }
-
- user, err := dbcon.AddUser(mail, pwhash, indata.Timezone.Loc, false, acCode)
- if err != nil {
- log.Printf("Could not create user (%s): %s", indata.Mail, err)
- outdata.Error = "Internal error, sorry. If this happens again, please contact support!"
- return
- }
-
- if !SendActivationcode(mail, acCode, user.ID()) {
- outdata.Error = "We could not send you a mail with your confirmation code."
- return
- }
-
- outdata.Success = "Account created successfully! We sent you an E-Mail that contains a link to activate your account."
+
+ if user != nil {
+ outdata.Success = "You are already logged in. To register a new account, first log out."
+ return outdata
+ }
+
+ if req.Method != "POST" {
+ return outdata
+ }
+
+ if err := req.ParseForm(); err != nil {
+ outdata.Error = "Data of form could not be understand. If this happens again, please contact support!"
+ return outdata
+ }
+
+ indata := new(registerFormdata)
+ if err := formdec.Decode(indata, req.Form); (err != nil) || (indata.Mail == "") || (indata.Timezone.Loc == nil) {
+ outdata.Error = "Input data wrong or missing. Please fill in all values and make sure to provide a valid E-Mail address."
+ return outdata
+ }
+
+ if indata.Password == "" {
+ outdata.Error = "Empty passwords are not allowed."
+ return outdata
+ }
+
+ if indata.Password != indata.RetypePassword {
+ outdata.Error = "Passwords are not identical."
+ return outdata
+ }
+
+ mail := string(indata.Mail)
+
+ switch _, err := dbcon.UserByMail(mail); err {
+ case nil:
+ outdata.Error = "This E-Mail address is already used."
+ return outdata
+ case model.NotFound:
+ default:
+ log.Printf("Error while checking, if mail is used: %s", err)
+ outdata.Error = "Internal error, sorry. If this happens again, please contact support!"
+ return outdata
+ }
+
+ acCode := genAcCode()
+ pwhash, err := bcrypt.GenerateFromPassword([]byte(indata.Password), bcrypt.DefaultCost)
+ if err != nil {
+ log.Printf("Error while hashing password: %s", err)
+ outdata.Error = "Internal error, sorry. If this happens again, please contact support!"
+ return outdata
}
+
+ user, err = dbcon.AddUser(mail, pwhash, indata.Timezone.Loc, false, acCode)
+ if err != nil {
+ log.Printf("Could not create user (%s): %s", indata.Mail, err)
+ outdata.Error = "Internal error, sorry. If this happens again, please contact support!"
+ return outdata
+ }
+
+ if !SendActivationcode(mail, acCode, user.ID()) {
+ outdata.Error = "We could not send you a mail with your confirmation code."
+ return outdata
+ }
+
+ outdata.Success = "Account created successfully! We sent you an E-Mail that contains a link to activate your account."
+ return outdata
}