-
Notifications
You must be signed in to change notification settings - Fork 19
/
register.go
89 lines (76 loc) · 2.13 KB
/
register.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
package test
import (
"net/http"
"github.com/foxiswho/echo-go/module/auth"
"github.com/foxiswho/echo-go/module/log"
userService "github.com/foxiswho/echo-go/service/example_service"
"github.com/foxiswho/echo-go/router/base"
)
func RegisterHandler(c *base.BaseContext) error {
redirect := c.QueryParam(auth.RedirectParam)
a := c.Auth()
if a.User.IsAuthenticated() {
if redirect == "" {
redirect = "/"
}
c.Redirect(http.StatusMovedPermanently, redirect)
return nil
}
c.Set("tmpl", "example/test/register")
c.Set("data", map[string]interface{}{
"title": "Register",
"redirectParam": auth.RedirectParam,
"redirect": redirect,
})
return nil
}
func RegisterPostHandler(c *base.BaseContext) error {
redirect := c.QueryParam(auth.RedirectParam)
if redirect == "" {
redirect = "/"
}
a := c.Auth()
if a.User.IsAuthenticated() {
c.Redirect(http.StatusMovedPermanently, redirect)
return nil
}
var form LoginForm
if err := c.Bind(&form); err == nil {
u := userService.AddUserWithNicknamePwd(form.Nickname, form.Password)
if u != nil {
session := c.Session()
err := auth.AuthenticateSession(session, u)
if err != nil {
c.JSON(http.StatusBadRequest, err)
}
c.Redirect(http.StatusMovedPermanently, redirect)
return nil
} else {
log.Debugf("Register user_service add error")
s := c.Session()
s.AddFlash("Register user_service add error", "_error")
// registerURL := c.Request().URI()
// c.Redirect(http.StatusMovedPermanently, registerURL)
c.Set("tmpl", "web/register")
c.Set("data", map[string]interface{}{
"title": "Register",
"redirectParam": auth.RedirectParam,
"redirect": redirect,
})
return nil
}
} else {
log.Debugf("Register form bind Error: %v", err)
s := c.Session()
s.AddFlash("Register form bind Error:"+err.Error(), "_error")
// registerURL := c.Request().URI()
// c.Redirect(http.StatusMovedPermanently, registerURL)
c.Set("tmpl", "example/test/register")
c.Set("data", map[string]interface{}{
"title": "Register",
"redirectParam": auth.RedirectParam,
"redirect": redirect,
})
return nil
}
}