summaryrefslogtreecommitdiff
path: root/register.go
diff options
context:
space:
mode:
authorKevin Chabowski <kevin@kch42.de>2013-08-29 22:37:05 +0200
committerKevin Chabowski <kevin@kch42.de>2013-08-29 22:37:05 +0200
commit61f137d2cc8ae0199c99493701023b4d862a34ad (patch)
tree5a77a1beb16cfd508486fabf6419f37fc348fc34 /register.go
parent8ecfe7a2fc61caf890e319e7a2f298b71dc90826 (diff)
downloadmailremind-61f137d2cc8ae0199c99493701023b4d862a34ad.tar.gz
mailremind-61f137d2cc8ae0199c99493701023b4d862a34ad.tar.bz2
mailremind-61f137d2cc8ae0199c99493701023b4d862a34ad.zip
Registering accounts is working
Diffstat (limited to 'register.go')
-rw-r--r--register.go101
1 files changed, 101 insertions, 0 deletions
diff --git a/register.go b/register.go
new file mode 100644
index 0000000..936f540
--- /dev/null
+++ b/register.go
@@ -0,0 +1,101 @@
+package main
+
+import (
+ "code.google.com/p/go.crypto/bcrypt"
+ "kch42.de/gostuff/mailremind/model"
+ "log"
+ "math/rand"
+ "net/http"
+)
+
+type registerData struct {
+ Error, Success string
+ Timezones *[]string
+}
+
+type registerFormdata struct {
+ Mail EMail
+ Password, RetypePassword string
+ Timezone timelocForm
+}
+
+var acCodeAlphabet = []rune("qwertzuiopasdfghjklyxcvbnmQWERTZUIOPASDFGHJKLYXCVBNM1234567890")
+
+func genAcCode() string {
+ const codelen = 10
+ alphalen := len(acCodeAlphabet)
+
+ code := make([]rune, codelen)
+ for i := 0; i < codelen; i++ {
+ code[i] = acCodeAlphabet[rand.Intn(alphalen)]
+ }
+
+ return string(code)
+}
+
+func register(rw http.ResponseWriter, req *http.Request) {
+ 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."
+ }
+}