getUser is also refresh the user reference in the currentSession#1315
getUser is also refresh the user reference in the currentSession#1315stefanschaller wants to merge 3 commits intosupabase:mainfrom
Conversation
📝 WalkthroughSummary by CodeRabbit
WalkthroughThe Comment |
|
Caution Failed to replace (edit) comment. This is likely due to insufficient permissions or the comment being deleted. Error details |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@packages/gotrue/lib/src/gotrue_client.dart`:
- Around line 720-728: The code emits userUpdated even when there is no local
session because currentSession?.copyWith(...) returns null; fix by
early-returning when _currentSession is null: after constructing userResponse,
if _currentSession is null return userResponse (so no copyWith or
notifyAllSubscribers is called); otherwise update _currentSession with
_currentSession.copyWith(user: userResponse.user) and call
notifyAllSubscribers(AuthChangeEvent.userUpdated). Ensure you reference
userResponse, _currentSession, copyWith, notifyAllSubscribers, and
AuthChangeEvent.userUpdated.
ℹ️ Review info
Configuration used: Central YAML (base), Organization UI (inherited)
Review profile: CHILL
Plan: Pro
Cache: Disabled due to Reviews > Disable Cache setting
Disabled knowledge base sources:
- Linear integration is disabled
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (1)
packages/gotrue/lib/src/gotrue_client.dart
| final userResponse = UserResponse.fromJson(response); | ||
|
|
||
| // np need to update the local user when the user is the same | ||
| if (userResponse.user == _currentSession?.user) return userResponse; | ||
|
|
||
| _currentSession = currentSession?.copyWith(user: userResponse.user); | ||
| notifyAllSubscribers(AuthChangeEvent.userUpdated); | ||
|
|
||
| return userResponse; |
There was a problem hiding this comment.
Bug: userUpdated event emitted incorrectly when no session exists.
When getUser(jwt) is called with a custom JWT but no local session (_currentSession is null), the code reaches line 725 where currentSession?.copyWith(...) evaluates to null, and then emits a userUpdated event with a null session. This is incorrect behavior—no state actually changed.
Guard against this case:
Proposed fix
final userResponse = UserResponse.fromJson(response);
- // np need to update the local user when the user is the same
- if (userResponse.user == _currentSession?.user) return userResponse;
+ // No need to update the local user when there's no session or user is the same
+ if (_currentSession == null || userResponse.user == _currentSession?.user) {
+ return userResponse;
+ }
_currentSession = currentSession?.copyWith(user: userResponse.user);
notifyAllSubscribers(AuthChangeEvent.userUpdated);
return userResponse;🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@packages/gotrue/lib/src/gotrue_client.dart` around lines 720 - 728, The code
emits userUpdated even when there is no local session because
currentSession?.copyWith(...) returns null; fix by early-returning when
_currentSession is null: after constructing userResponse, if _currentSession is
null return userResponse (so no copyWith or notifyAllSubscribers is called);
otherwise update _currentSession with _currentSession.copyWith(user:
userResponse.user) and call notifyAllSubscribers(AuthChangeEvent.userUpdated).
Ensure you reference userResponse, _currentSession, copyWith,
notifyAllSubscribers, and AuthChangeEvent.userUpdated.
There was a problem hiding this comment.
♻️ Duplicate comments (1)
packages/gotrue/lib/src/gotrue_client.dart (1)
720-728:⚠️ Potential issue | 🟠 MajorGuard against emitting
userUpdatedwhen no local session exists.If
getUser(jwt)is called while_currentSessionis null,currentSession?.copyWith(...)yields null and you still emituserUpdatedwith no session. This is incorrect and can mislead subscribers. Please return early when_currentSessionis null.🛠️ Proposed fix
- if (userResponse.user == null) return userResponse; - - // np need to update the local user when the user is the same - if (userResponse.user == _currentSession?.user) return userResponse; + if (userResponse.user == null) return userResponse; + if (_currentSession == null) return userResponse; + + // no need to update the local user when the user is the same + if (userResponse.user == _currentSession?.user) return userResponse; _currentSession = currentSession?.copyWith(user: userResponse.user); notifyAllSubscribers(AuthChangeEvent.userUpdated);🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/gotrue/lib/src/gotrue_client.dart` around lines 720 - 728, The code updates _currentSession and emits notifyAllSubscribers(AuthChangeEvent.userUpdated) even when there is no local session; change the logic in the block after UserResponse.fromJson(response) to return early if _currentSession is null (i.e., check _currentSession == null and return userResponse) before calling currentSession?.copyWith(...) and notifyAllSubscribers so you never emit a userUpdated event without an existing local session.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Duplicate comments:
In `@packages/gotrue/lib/src/gotrue_client.dart`:
- Around line 720-728: The code updates _currentSession and emits
notifyAllSubscribers(AuthChangeEvent.userUpdated) even when there is no local
session; change the logic in the block after UserResponse.fromJson(response) to
return early if _currentSession is null (i.e., check _currentSession == null and
return userResponse) before calling currentSession?.copyWith(...) and
notifyAllSubscribers so you never emit a userUpdated event without an existing
local session.
ℹ️ Review info
Configuration used: Central YAML (base), Organization UI (inherited)
Review profile: CHILL
Plan: Pro
Cache: Disabled due to Reviews > Disable Cache setting
Disabled knowledge base sources:
- Linear integration is disabled
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (1)
packages/gotrue/lib/src/gotrue_client.dart
What kind of change does this PR introduce?
Bug fix/feature -> Depending on who you ask 😅
What is the current behavior?
The
getUsercall only gets the user without doing anything with the userWhat is the new behavior?
The old user object is also updated in the
currentSessionAdditional context
We update the user object in an edgefunction and want to refresh the user afterwards.