-
Notifications
You must be signed in to change notification settings - Fork 9
/
auth.go
49 lines (39 loc) · 1.34 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
package middleware
import (
"github.com/spf13/cast"
"github.com/gofiber/fiber/v2"
"github.com/deatil/doak-cms/pkg/db"
"github.com/deatil/doak-cms/pkg/config"
"github.com/deatil/doak-cms/pkg/cookie"
"github.com/deatil/doak-cms/pkg/session"
"github.com/deatil/doak-cms/app/url"
"github.com/deatil/doak-cms/app/model"
"github.com/deatil/doak-cms/app/response"
)
// 登录检测
func NewAuth() fiber.Handler {
return func(ctx *fiber.Ctx) error {
userid := session.Get(ctx, "userid")
if userid == nil {
cookieKey := config.Section("cookie").Key("key").MustString("doak")
userid = cookie.Get(ctx, cookieKey)
if userid == "" {
return response.AdminErrorRender(ctx, "请先登录", url.AdminUrl("login"))
}
}
// 账号信息
var user model.User
_, err := db.Engine().
Where("id = ?", cast.ToInt64(userid)).
Get(&user)
if err != nil || user.Status != 1 {
session.Delete(ctx, "userid")
return response.AdminErrorRender(ctx, "账号不存在或者被禁用")
}
ctx.Locals("userid", userid)
ctx.Locals("user", user)
return ctx.Next()
// 无数据发送[204]
// return ctx.SendStatus(fiber.StatusNoContent)
}
}