-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.go
146 lines (141 loc) · 3.31 KB
/
account.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package heron
import (
"encoding/json"
"net/http"
"github.com/gorilla/sessions"
"github.com/shonenada/heron/models"
"github.com/zenazn/goji/web"
)
func AccountController(c web.C, w http.ResponseWriter, r *http.Request) {
r.ParseForm()
db := GetDatabase()
if r.Method == "GET" {
username := c.URLParams["username"]
user := models.Account{}
db.Where("username = ?", username).First(&user)
if user.Username == "" {
raw_data := map[string]string{"message": "user not found"}
data, err := json.Marshal(raw_data)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
http.Error(w, string(data), 404)
return
} else {
RenderJson(w, user)
return
}
}
if r.Method == "POST" {
username := r.PostForm.Get("username")
password := r.PostForm.Get("password")
repeatPassword := r.PostForm.Get("repeat-password")
if username == "" {
RenderJson(w, map[string]interface{}{
"success": false,
"message": "username cannot be empty",
})
return
}
if password == "" {
RenderJson(w, map[string]interface{}{
"success": false,
"message": "password cannot be empty",
})
return
}
if repeatPassword == "" {
RenderJson(w, map[string]interface{}{
"success": false,
"message": "repeat password cannot be empty",
})
return
}
if password != repeatPassword {
RenderJson(w, map[string]interface{}{
"success": false,
"message": "repeat password is not match with passwrord",
})
return
}
account := models.Account{}
db.Where("username = ?", username).First(&account)
if account.Username != "" {
RenderJson(w, map[string]interface{}{
"success": false,
"message": "username exists",
})
return
}
account.Username = username
account.Password = password
account.Actived = true
if db.NewRecord(account) {
db.Create(&account)
RenderJson(w, map[string]interface{}{
"success": true,
})
return
} else {
RenderJson(w, map[string]interface{}{
"success": false,
"message": "record exist",
})
return
}
}
}
func AccountSignController(c web.C, w http.ResponseWriter, r *http.Request) {
r.ParseForm()
db := GetDatabase()
store := sessions.NewCookieStore([]byte(Config.SecretKey))
if r.Method == "POST" {
username := r.PostForm.Get("username")
password := r.PostForm.Get("password")
if username == "" {
RenderJson(w, map[string]interface{}{
"success": false,
"message": "usernmae cannot be empty",
})
return
}
if password == "" {
RenderJson(w, map[string]interface{}{
"succcess": false,
"message": "password cannot be empty",
})
return
}
user := models.Account{}
db.Where("username = ? AND password = ?", username, password).First(&user)
if user.Username == username {
session, err := store.Get(r, "user")
if err != nil {
http.Error(w, err.Error(), 500)
return
}
session.Values["username"] = user.Username
session.Save(r, w)
RenderJson(w, map[string]interface{}{
"success": true,
})
return
} else {
RenderJson(w, map[string]interface{}{
"success": false,
"message": "username or password is incorrect",
})
return
}
}
if r.Method == "DELETE" {
session, err := store.Get(r, "user")
if err != nil {
http.Error(w, err.Error(), 500)
return
}
session.Values["username"] = ""
session.Save(r, w)
}
}