Cloudreve/middleware/auth.go

39 lines
742 B
Go
Raw Normal View History

2019-11-12 02:34:54 -05:00
package middleware
import (
2019-11-16 03:11:37 -05:00
"github.com/HFO4/cloudreve/models"
"github.com/HFO4/cloudreve/pkg/serializer"
2019-11-12 02:34:54 -05:00
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
)
// CurrentUser 获取登录用户
func CurrentUser() gin.HandlerFunc {
return func(c *gin.Context) {
session := sessions.Default(c)
uid := session.Get("user_id")
if uid != nil {
user, err := model.GetUserByID(uid)
if err == nil {
c.Set("user", &user)
}
}
c.Next()
}
}
// AuthRequired 需要登录
func AuthRequired() gin.HandlerFunc {
return func(c *gin.Context) {
if user, _ := c.Get("user"); user != nil {
if _, ok := user.(*model.User); ok {
c.Next()
return
}
}
c.JSON(200, serializer.CheckLogin())
c.Abort()
}
}