新增 OIDC/SSO、邮件验证码与 Gitea 项目同步,并强化 Feed 与管理后台。
作为 OIDC Provider 对接 Gitea;注册支持邮件验证码/验证码;侧栏同步公开仓库;Feed 分页、文章大纲、标签云与站点品牌设置。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -25,15 +25,21 @@ func NewAuthMiddleware(auth *service.AuthService) *AuthMiddleware {
|
||||
return &AuthMiddleware{auth: auth}
|
||||
}
|
||||
|
||||
// OptionalAuth 可选鉴权:有 token 则解析,无 token 不拦截
|
||||
// OptionalAuth 可选鉴权:有 token 则解析,无 token 不拦截。
|
||||
// 用户已删除或不存在时清除失效 cookie,避免前端误显示为已登录。
|
||||
func (m *AuthMiddleware) OptionalAuth() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
token := extractToken(c)
|
||||
if token != "" {
|
||||
if claims, err := m.auth.ParseToken(token); err == nil {
|
||||
c.Set(CtxUserID, claims.UserID)
|
||||
c.Set(CtxUsername, claims.Username)
|
||||
c.Set(CtxRole, claims.Role)
|
||||
var user model.User
|
||||
if err := model.DB.Select("id", "username", "role").First(&user, claims.UserID).Error; err != nil {
|
||||
c.SetCookie(CookieName, "", -1, "/", "", false, true)
|
||||
} else {
|
||||
c.Set(CtxUserID, user.ID)
|
||||
c.Set(CtxUsername, user.Username)
|
||||
c.Set(CtxRole, user.Role)
|
||||
}
|
||||
}
|
||||
}
|
||||
c.Next()
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"git.iioio.com/freefire/jiang13-forum/service"
|
||||
)
|
||||
|
||||
const VisitorCookieName = "j13_vid"
|
||||
|
||||
// PresenceMiddleware 记录当前请求的浏览活跃(会员或游客)
|
||||
func PresenceMiddleware(online *service.OnlineService) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
if uid, ok := c.Get(CtxUserID); ok {
|
||||
if id, ok := uid.(uint); ok && id > 0 {
|
||||
online.Ping(id)
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
}
|
||||
vid, err := c.Cookie(VisitorCookieName)
|
||||
if err != nil || vid == "" {
|
||||
vid = newVisitorID()
|
||||
c.SetCookie(VisitorCookieName, vid, 86400, "/", "", false, true)
|
||||
}
|
||||
online.PingGuest(vid)
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
func newVisitorID() string {
|
||||
b := make([]byte, 16)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
return hex.EncodeToString([]byte("fallback-visitor-id"))
|
||||
}
|
||||
return hex.EncodeToString(b)
|
||||
}
|
||||
Reference in New Issue
Block a user