mirror of
https://github.com/eiblog/eiblog.git
synced 2026-02-12 09:22:27 +08:00
chore: rename blog->eiblog
This commit is contained in:
56
pkg/core/eiblog/api.go
Normal file
56
pkg/core/eiblog/api.go
Normal file
@@ -0,0 +1,56 @@
|
||||
// Package eiblog provides ...
|
||||
package eiblog
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-contrib/sessions"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// @title APP Demo API
|
||||
// @version 1.0
|
||||
// @description This is a sample server celler server.
|
||||
|
||||
// @BasePath /api
|
||||
|
||||
// AuthFilter auth filter
|
||||
func AuthFilter(c *gin.Context) {
|
||||
if !IsLogined(c) {
|
||||
c.Abort()
|
||||
c.Status(http.StatusUnauthorized)
|
||||
c.Redirect(http.StatusFound, "/admin/login")
|
||||
return
|
||||
}
|
||||
|
||||
c.Next()
|
||||
}
|
||||
|
||||
// SetLogin login user
|
||||
func SetLogin(c *gin.Context, username string) {
|
||||
session := sessions.Default(c)
|
||||
session.Set("username", username)
|
||||
session.Save()
|
||||
}
|
||||
|
||||
// SetLogout logout user
|
||||
func SetLogout(c *gin.Context) {
|
||||
session := sessions.Default(c)
|
||||
session.Delete("username")
|
||||
session.Save()
|
||||
}
|
||||
|
||||
// IsLogined account logined
|
||||
func IsLogined(c *gin.Context) bool {
|
||||
return GetUsername(c) != ""
|
||||
}
|
||||
|
||||
// GetUsername get logined account
|
||||
func GetUsername(c *gin.Context) string {
|
||||
session := sessions.Default(c)
|
||||
username := session.Get("username")
|
||||
if username == nil {
|
||||
return ""
|
||||
}
|
||||
return username.(string)
|
||||
}
|
||||
Reference in New Issue
Block a user