fix(cli): use project client for Users service in JwtManager#1396
fix(cli): use project client for Users service in JwtManager#1396ChiragAgg5k merged 2 commits intomasterfrom
Conversation
The CLI refactor (943c732) changed JwtManager.setup() to use sdkForConsole() for both Users and Projects services. However, the Users API needs to target the actual project (via sdkForProject()), not the console project. This caused user impersonation (--user-id) to fail on self-hosted instances, as the user lookup and JWT creation were sent to the wrong project context.
📝 WalkthroughWalkthroughUpdated Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
Greptile SummaryThis PR fixes a regression introduced in the CLI refactor ( Confidence Score: 5/5Safe to merge — the change is a minimal, targeted fix that restores pre-regression behavior with no side-effects The fix is one logical line change that correctly reverts the client used for the Users service back to sdkForProject(), matching the documented v12.0.0 behavior. No new logic is introduced, error handling is unchanged, and the only remaining note is a non-blocking style suggestion about lazy initialization. No files require special attention Important Files Changed
Reviews (1): Last reviewed commit: "fix(cli): use project client for Users s..." | Re-trigger Greptile |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
templates/cli/lib/emulation/utils.ts (1)
140-151: ResetuserJwtwhen nouserIdis provided to avoid stale state.If
setup()is called multiple times in one process, a prior JWT can persist whenuserIdis omitted. Clearing first is safer.Suggested patch
+ this.userJwt = null; if (userId) { const projectClient = await sdkForProject(); const usersClient = new Users(projectClient); await usersClient.get({ userId, }); const userResponse = await usersClient.createJWT({ userId, duration: 60 * 60, }); this.userJwt = userResponse.jwt; }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@templates/cli/lib/emulation/utils.ts` around lines 140 - 151, The setup method can leave a stale JWT when called without a userId; modify setup (around the sdkForProject()/Users logic) to explicitly clear this.userJwt when no userId is provided—either set this.userJwt = undefined (or null) before the if (userId) block or add an else branch that sets this.userJwt = undefined so prior JWTs are not reused; reference symbols: setup, this.userJwt, sdkForProject, Users, usersClient.createJWT.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@templates/cli/lib/emulation/utils.ts`:
- Around line 140-151: The setup method can leave a stale JWT when called
without a userId; modify setup (around the sdkForProject()/Users logic) to
explicitly clear this.userJwt when no userId is provided—either set this.userJwt
= undefined (or null) before the if (userId) block or add an else branch that
sets this.userJwt = undefined so prior JWTs are not reused; reference symbols:
setup, this.userJwt, sdkForProject, Users, usersClient.createJWT.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: d78a4403-6b97-4f89-8059-7aaeffa67b08
📒 Files selected for processing (1)
templates/cli/lib/emulation/utils.ts
Summary
--user-id) failing on self-hosted instances943c73277) changedJwtManager.setup()to usesdkForConsole()for bothUsersandProjectsservices, but theUsersAPI (/users/{userId},/users/{userId}/jwts) needs to target the actual project viasdkForProject()— not the"console"projectusersClient.get()to fail before either JWT could be generated, resulting in emptyx-appwrite-keyandx-appwrite-user-jwtheadersUsersclientProjectsclientsdkForProject()sdkForConsole()sdkForConsole()sdkForConsole()sdkForProject()sdkForConsole()Test plan
appwrite run functions --function-id <ID> --user-id <USER_ID>on a self-hosted instancehasUserJwt: trueandhasApiKey: true--user-idstill works correctlySummary by CodeRabbit