mirror of
https://github.com/snowykami/neo-blog.git
synced 2025-09-04 00:06:22 +00:00
refactor: change import paths for DeviceContext and GravatarAvatar components fix: adjust login form API call and update UI text for clarity feat: add post API for listing posts with pagination and filtering options feat: implement BlogCard component for displaying blog posts with enhanced UI feat: create Badge component for consistent styling of labels and indicators refactor: reintroduce DeviceContext with improved functionality for theme and language management feat: define Label and Post models for better type safety and structure
33 lines
711 B
Go
33 lines
711 B
Go
package ctxutils
|
|
|
|
import (
|
|
"context"
|
|
"github.com/snowykami/neo-blog/internal/model"
|
|
"github.com/snowykami/neo-blog/internal/repo"
|
|
"github.com/snowykami/neo-blog/pkg/constant"
|
|
)
|
|
|
|
// GetCurrentUser 从上下文中获取当前用户
|
|
func GetCurrentUser(ctx context.Context) (*model.User, bool) {
|
|
val := ctx.Value(constant.ContextKeyUserID)
|
|
if val == nil {
|
|
return nil, false
|
|
}
|
|
user, err := repo.User.GetUserByID(val.(uint))
|
|
if err != nil {
|
|
return nil, false
|
|
}
|
|
|
|
return user, true
|
|
}
|
|
|
|
// GetCurrentUserID 从上下文中获取当前用户ID
|
|
func GetCurrentUserID(ctx context.Context) (uint, bool) {
|
|
user, ok := GetCurrentUser(ctx)
|
|
if !ok || user == nil {
|
|
return 0, false
|
|
}
|
|
|
|
return user.ID, true
|
|
}
|