forked from GoAdminGroup/go-admin
/
auth.go
56 lines (44 loc) · 1.19 KB
/
auth.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
package controller
import (
"bytes"
"fmt"
"github.com/chenhg5/go-admin/context"
"github.com/chenhg5/go-admin/modules/auth"
"github.com/chenhg5/go-admin/modules/menu"
"github.com/chenhg5/go-admin/template"
"net/http"
)
func Auth(ctx *context.Context) {
password := ctx.Request.FormValue("password")
username := ctx.Request.FormValue("username")
if user, ok := auth.Check(password, username); ok {
auth.SetCookie(ctx, user)
menu.Unlock()
ctx.Json(http.StatusOK, map[string]interface{}{
"code": 200,
"msg": "登录成功",
"url": Config.PREFIX + Config.INDEX,
})
return
}
ctx.Json(http.StatusBadRequest, map[string]interface{}{
"code": 400,
"msg": "登录失败",
})
return
}
func Logout(ctx *context.Context) {
auth.DelCookie(ctx)
ctx.Response.Header.Add("Location", Config.PREFIX+"/login")
ctx.SetStatusCode(302)
}
func ShowLogin(ctx *context.Context) {
defer GlobalDeferHandler(ctx)
tmpl, name := template.GetComp("login").GetTemplate()
buf := new(bytes.Buffer)
fmt.Println(tmpl.ExecuteTemplate(buf, name, struct {
AssertRootUrl string
}{Config.PREFIX}))
ctx.WriteString(buf.String())
ctx.Response.Header.Add("Content-Type", "text/html; charset=utf-8")
}