A simple admin ui without any functionalities
Signed-off-by: Yuan Si <do4suki@gmail.com>
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-contrib/sessions"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func WebAuth(ctx *gin.Context) {
|
||||
session := sessions.Default(ctx)
|
||||
requestUrl := strings.Split(ctx.Request.URL.String(), "?")[0] // 过滤 GET 参数
|
||||
_, ok := session.Get("username").(string)
|
||||
if ok {
|
||||
if requestUrl == "/admin/login" {
|
||||
ctx.Redirect(http.StatusFound, "/admin/index")
|
||||
ctx.Abort()
|
||||
}
|
||||
} else {
|
||||
if requestUrl != "/admin/login" {
|
||||
ctx.Redirect(http.StatusFound, "/admin/login")
|
||||
ctx.Abort()
|
||||
}
|
||||
}
|
||||
|
||||
ctx.Next()
|
||||
}
|
||||
Reference in New Issue
Block a user