feat: 微信分享钩子全页面接入 + 订阅信息结构化映射 + JWT 错误处理加强
- client: 全部页面接入 onShareAppMessage/onShareTimeline 分享钩子 - client: login 页增强、profile 订阅展示优化 - client/stores: 订阅字段扁平结构映射为 subInfo(plan/expiresAt/startDate) - server: auth 路由 JWT 无效/过期统一返回 401 未授权响应 - server: models/tests 同步更新
This commit is contained in:
@@ -96,32 +96,23 @@ const userAuthMiddleware = async (req, res, next) => {
|
||||
|
||||
const token = authHeader.replace('Bearer ', '')
|
||||
|
||||
// 添加调试日志
|
||||
console.log('[Auth] Token received:', token.substring(0, 20) + '...')
|
||||
|
||||
const decoded = jwt.verify(token, process.env.JWT_SECRET)
|
||||
console.log('[Auth] Token decoded:', decoded)
|
||||
|
||||
// 兼容两种token格式:包含userId或openid
|
||||
let user = null
|
||||
if (decoded.userId) {
|
||||
console.log('[Auth] Looking up user by userId:', decoded.userId)
|
||||
user = await User.findById(decoded.userId)
|
||||
} else if (decoded.openid) {
|
||||
console.log('[Auth] Looking up user by openid:', decoded.openid)
|
||||
user = await User.findOne({ openid: decoded.openid })
|
||||
}
|
||||
|
||||
if (!user) {
|
||||
console.log('[Auth] User not found')
|
||||
return res.status(401).json({
|
||||
success: false,
|
||||
error: '用户不存在或令牌无效'
|
||||
})
|
||||
}
|
||||
|
||||
console.log('[Auth] User found:', user._id)
|
||||
console.log('[Auth] User openid:', user.openid)
|
||||
req.user = user
|
||||
// 优先使用 openid,如果没有则使用 userId 作为标识
|
||||
req.openid = user.openid || user._id.toString()
|
||||
|
||||
Reference in New Issue
Block a user