summaryrefslogtreecommitdiff
path: root/handler.go
blob: 8fab6e835662c5d7d0e25055228dc89c4f6c7f5e (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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")
	session.Save(r, w)

	/*
		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
		}
	*/

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

	session.Save(r, w)
}

func RegisterHandler(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")
	session.Save(r, w)

	err = ExecTemplate("register.html", w, flash)
	if err != nil {
		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(), "error")
		session.Save(r, w)
		http.Redirect(w, r, "/", 302)
		return
	}

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