/
c_login.go
67 lines (56 loc) · 1.46 KB
/
c_login.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
package routers
import (
//"io"
//"os"
"github.com/astaxie/beego/context"
//"github.com/beego/i18n"
//"github.com/beego/beeweb/models"
)
// DocsRouter serves about page.
type LoginRouter struct {
baseRouter
}
const (
Secret = "leC3E56ETF8o-RatYNrieyw_BEYbsnr605wt7PQzt31_t"
ConsoleDomain = "http://localhost:8080"
)
func (this *LoginRouter) Get() {
isExit := this.Input().Get("exit") == "true"
if isExit {
this.Ctx.SetCookie("uname", "", -1, "/")
this.Ctx.SetCookie("pwd", "", -1, "/")
this.Redirect("/", 301)
return
}
this.TplNames = "login.html"
}
func (this *LoginRouter) Post() {
uname := this.Input().Get("uname")
pwd := this.Input().Get("pwd")
autologin := this.Input().Get("autologin") == "on"
if "mick" == uname && "mick" == pwd {
maxAge := 0
if autologin {
maxAge = 1<<31 - 1
}
this.Ctx.SetSecureCookie(Secret, "uname", uname, maxAge, "/")
this.Ctx.SetSecureCookie(Secret, "pwd", pwd, maxAge, "/")
this.Redirect(ConsoleDomain+"/dashboard", 301)
return
}
this.Redirect("/login", 301)
return
}
func checkAccount(ctx *context.Context) bool {
ck, ok := ctx.GetSecureCookie(Secret, "uname")
if !ok {
return false
}
uname := ck
ck, ok = ctx.GetSecureCookie(Secret, "pwd")
if !ok {
return false
}
pwd := ck
return "mick" == uname && "mick" == pwd
}