fix: cache JWT secret to avoid token validation failure when JWT_SECRET is not set - #16
Merged
Merged
Conversation
…ET is not set When JWT_SECRET env var is empty, jwtSecret() generates a new random key on every call. This means GenerateToken and ParseToken use different keys, causing all authenticated requests to fail with 'invalid or expired token'. Fixed by caching the secret in a package-level variable so it's only generated once per process lifetime. Fixes #15
LeoninCS
approved these changes
Jul 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题
jwtSecret()在JWT_SECRET环境变量为空时,每次调用都生成新的随机密钥,导致GenerateToken和ParseToken使用不同的密钥,所有鉴权接口返回 401。Fixes #15
修复方案
在包级别缓存密钥,确保每个进程只生成一次:
测试
不设置
JWT_SECRET启动后端,登录后访问鉴权接口正常返回。