-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroute-handler.go
113 lines (96 loc) · 2.55 KB
/
route-handler.go
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
108
109
110
111
112
113
package main
import (
"github.com/satori/go.uuid"
"golang.org/x/crypto/bcrypt"
"log"
"net/http"
)
func login(w http.ResponseWriter, req *http.Request) {
var message string
pass := true
if alreadyLoggedIn(req) {
http.Redirect(w, req, "/home", http.StatusSeeOther)
} else {
if req.Method == http.MethodPost {
u := User{
Email: req.FormValue("email"),
Password: req.FormValue("password"),
}
message = validatePayload(u, true)
if len(message) > 0 {
pass = false
} else if !isValidLogin(u) {
pass = false
message = INVALID_CREDENTIALS
}
if pass {
//create session
sessionId, _ := uuid.NewV4()
http.SetCookie(w, &http.Cookie{
Name: "uuid",
Value: sessionId.String(),
})
db.Debug().Delete(Session{}, "email = ?", u.Email)
db.Create(&Session{
Email: u.Email,
SessionId: sessionId.String(),
})
http.Redirect(w, req, "/home", http.StatusSeeOther)
}
}
}
_ = tpl.ExecuteTemplate(w, "index.html", Response{pass, message})
}
func signup(w http.ResponseWriter, req *http.Request) {
if alreadyLoggedIn(req) {
http.Redirect(w, req, "/home", http.StatusSeeOther)
}
var pass bool
var message string
if req.Method == http.MethodPost {
u := User{
Email: req.FormValue("email"),
Password: req.FormValue("password"),
Name: req.FormValue("name"),
Mobile: req.FormValue("mobile"),
}
message = validatePayload(u, false)
if len(message) == 0 { // request has all the necessary data
bs, err := bcrypt.GenerateFromPassword([]byte(u.Password), bcrypt.MinCost)
if err != nil {
log.Fatalln("Unable to encrypt password! ")
}
u.Password = string(bs)
rowsAffected := db.Create(&u).RowsAffected
if rowsAffected > 0 { // if insert is successful
pass = true
message = SUCCESSFULL_REGISTRATION
} else { // email already present
pass = false
message = EMAIL_ALREADY_TAKEN
}
} else {
pass = false
message = COULD_NOT_REGISTER + ":" + message
}
}
_ = tpl.ExecuteTemplate(w, "signup.html", Response{pass, message})
}
func home(w http.ResponseWriter, req *http.Request) {
_ = tpl.ExecuteTemplate(w, "home.html", nil)
}
func logout(w http.ResponseWriter, req *http.Request) {
if !alreadyLoggedIn(req) {
http.Redirect(w, req, "/", http.StatusSeeOther)
return
}
c, _ := req.Cookie("uuid")
c = &http.Cookie{ // remove the cookie
Name: "uuid",
Value: "",
MaxAge: -1,
}
db.Debug().Unscoped().Where("session_id = ?", c.Value).Delete(&Session{})
http.SetCookie(w, c)
http.Redirect(w, req, "/", http.StatusSeeOther)
}