forked from kataras/iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
125 lines (108 loc) 路 2.78 KB
/
controller.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
package user
const (
pathMyProfile = "/user/me"
pathRegister = "/user/register"
)
// Controller is responsible to handle the following requests:
// GET /user/register
// POST /user/register
// GET /user/login
// POST /user/login
// GET /user/me
// GET /user/{id:long} | long is a new param type, it's the int64.
// All HTTP Methods /user/logout
type Controller struct {
AuthController
}
// GetRegister handles GET:/user/register.
func (c *Controller) GetRegister() {
if c.isLoggedIn() {
c.logout()
return
}
c.Data["Title"] = "User Registration"
c.Tmpl = pathRegister + ".html"
}
// PostRegister handles POST:/user/register.
func (c *Controller) PostRegister() {
// we can either use the `c.Ctx.ReadForm` or read values one by one.
var (
firstname = c.Ctx.FormValue("firstname")
username = c.Ctx.FormValue("username")
password = c.Ctx.FormValue("password")
)
user, err := c.createOrUpdate(firstname, username, password)
if err != nil {
c.fireError(err)
return
}
// setting a session value was never easier.
c.Session.Set(sessionIDKey, user.ID)
// succeed, nothing more to do here, just redirect to the /user/me.
c.Path = pathMyProfile
}
// GetLogin handles GET:/user/login.
func (c *Controller) GetLogin() {
if c.isLoggedIn() {
c.logout()
return
}
c.Data["Title"] = "User Login"
c.Tmpl = PathLogin + ".html"
}
// PostLogin handles POST:/user/login.
func (c *Controller) PostLogin() {
var (
username = c.Ctx.FormValue("username")
password = c.Ctx.FormValue("password")
)
user, err := c.verify(username, password)
if err != nil {
c.fireError(err)
return
}
c.Session.Set(sessionIDKey, user.ID)
c.Path = pathMyProfile
}
// AnyLogout handles any method on path /user/logout.
func (c *Controller) AnyLogout() {
c.logout()
}
// GetMe handles GET:/user/me.
func (c *Controller) GetMe() {
id, err := c.Session.GetInt64(sessionIDKey)
if err != nil || id <= 0 {
// when not already logged in.
c.Path = PathLogin
return
}
u, found := c.Source.GetByID(id)
if !found {
// if the session exists but for some reason the user doesn't exist in the "database"
// then logout him and redirect to the register page.
c.logout()
return
}
// set the model and render the view template.
c.User = u
c.Data["Title"] = "Profile of " + u.Username
c.Tmpl = pathMyProfile + ".html"
}
func (c *Controller) renderNotFound(id int64) {
c.Status = 404
c.Data["Title"] = "User Not Found"
c.Data["ID"] = id
c.Tmpl = "user/notfound.html"
}
// GetBy handles GET:/user/{id:long},
// i.e http://localhost:8080/user/1
func (c *Controller) GetBy(userID int64) {
// we have /user/{id}
// fetch and render user json.
if user, found := c.Source.GetByID(userID); !found {
// not user found with that ID.
c.renderNotFound(userID)
} else {
c.Ctx.JSON(user)
}
}