Skip to content

fix(nestjs): guard crashes when I18nContext.current() returns undefined on logout (#236) - #240

Open
Huauauaa wants to merge 1 commit into
opentiny:devfrom
Huauauaa:Huauauaa/nestjs
Open

Huauauaa wants to merge 1 commit into
opentiny:devfrom
Huauauaa:Huauauaa/nestjs

Conversation

@Huauauaa

@Huauauaa Huauauaa commented Sep 24, 2026 •

Copy link
Copy Markdown

PR

PR Checklist

Please check if your PR fulfills the following requirements:

  • The commit message follows our Commit Message Guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • Other... Please describe:

What is the current behavior?

Issue Number: fixes #236

What is the new behavior?

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Summary by CodeRabbit

  • Bug Fixes
    • Authentication errors now display translated messages when no localization context is available, using English as a fallback.
    • Token-related errors continue to use the active language when a localization context is present, including messages for missing or expired tokens.

@github-actions github-actions Bot added bug Something isn't working unit-test Unit test labels Sep 24, 2026
@coderabbitai

coderabbitai Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: a470df80-7e72-4f52-b934-e1045685b0b8

📥 Commits

Reviewing files that changed from the base of the PR and between 4622f33 and 99ea8d9.

📒 Files selected for processing (2)
  • template/nestJs/src/auth/__tests__/auth.guard.spec.ts
  • template/nestJs/src/auth/auth.guard.ts

Included review availability: Your plan provides up to 1 included review per hour; 0 remain after this review.


Walkthrough

AuthGuard now uses I18nService with the enUS language when no current I18nContext exists. New tests cover the missing-token and token-verification-failure paths without an I18nContext.

Changes

Auth guard translation fallback

Layer / File(s) Summary
Guard translation fallback and coverage
template/nestJs/src/auth/auth.guard.ts, template/nestJs/src/auth/__tests__/auth.guard.spec.ts
AuthGuard injects I18nService and uses it with lang: 'enUS' when I18nContext is absent. Token error messages use the translation helper. Tests cover missing-token and token-verification-failure paths without a current context.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: ⚪ Minimal · up to 99ea8

The change addresses the reported logout crash, and no actionable merge risk is established beyond normal validation.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main bug fix: preventing the NestJS guard from crashing when I18nContext.current() returns undefined during logout.
Linked Issues check ✅ Passed Issue #236 requires logout to complete without the Cannot read properties of undefined (reading 't') error. AuthGuard now checks I18nContext.current() before translation and uses `I18nService.t(…
Out of Scope Changes check ✅ Passed The changes are limited to AuthGuard translation fallback logic and its automated tests. These changes directly support issue #236 and do not show unrelated behavior or files.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 2…
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

A rabbit checks the token gate,
No context? Translations still await.
enUS lends a helping hand,
While guarded paths proceed as planned.
The rabbit hops, tests green and bright,
And leaves the guard in good form tonight.

Comment @coderabbitai help to get the list of available commands.

@GaoNeng-wWw

Copy link
Copy Markdown
Collaborator
image 单元测试可能需要修复一下

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working unit-test Unit test

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 [Bug]: 退出时,后台错误导致页面卡住

2 participants