summaryrefslogtreecommitdiff
path: root/handler.go
blob: e708a20774db5922e625102dd93d949edf8dbfa7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main

import (
	"log"
	"net/http"
)

func IndexHandler(w http.ResponseWriter, r *http.Request) {
	session, err := store.Get(r, "_SID")
	if err != nil {
		log.Println(err)
	}

	flash := Flash{}
	flash.Error = session.Flashes("error")
	flash.Success = session.Flashes("success")

	index := mainTempl.Lookup("index.html")

	err = index.ExecuteTemplate(w, "index.html", flash)
	if err != nil {
		log.Println(err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	session.Save(r, w)
}

func CreateNewEntryHandler(w http.ResponseWriter, r *http.Request) {
	err := r.ParseForm()
	if err != nil {
		log.Panic(err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	req := Request{}
	err = decoder.Decode(&req, r.PostForm)
	if err != nil {
		log.Panic(err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	session, err := store.Get(r, "_SID")
	if err != nil {
		log.Println(err)
	}

	if !CompareStrings(req.Email, req.ConfirmEmail) {
		session.AddFlash("E-Mail don't match.", "error")
		session.Save(r, w)
		http.Redirect(w, r, "/", 302)
		return
	}

	if !CompareStrings(req.Password, req.ConfirmPassword) {
		session.AddFlash("Passwords don't match.", "error")
		session.Save(r, w)
		http.Redirect(w, r, "/", 302)
		return
	}

	req.Password = Md5Hash(req.Password)

	err = CreateNewEntry(req.Email, req.Password)
	if err != nil {
		session.AddFlash(err, "error")
		session.Save(r, w)
		http.Redirect(w, r, "/", 302)
		return
	}

	session.AddFlash("Success! You can login now with your new mail account.")
	session.Save(r, w)
	http.Redirect(w, r, "/", 302)
}