@oxyhq/core 1.11.14 → 1.11.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -168,8 +168,48 @@ export function OxyServicesPopupAuthMixin(Base) {
168
168
  document.body.appendChild(iframe);
169
169
  try {
170
170
  const session = await this.waitForIframeAuth(iframe, timeout, clientId);
171
- if (session && session.accessToken) {
172
- this.httpService.setTokens(session.accessToken);
171
+ // Bail early on incomplete responses. The iframe contract requires
172
+ // both an access token and a session id; anything less is unusable.
173
+ // Returning `null` here (without installing the token) prevents a
174
+ // stale credential from being committed to HttpService when the
175
+ // user is actually signed out — that pattern caused a `getCurrentUser`
176
+ // -> 401 -> token-clear loop in consumer apps because callers gated
177
+ // on `session?.user` and never installed the user via
178
+ // `handleAuthSuccess`, while HttpService quietly held the token.
179
+ const accessToken = session ? session.accessToken : undefined;
180
+ if (!session || !accessToken || !session.sessionId) {
181
+ return null;
182
+ }
183
+ // Snapshot the previous token so we can roll back if the user
184
+ // lookup below fails — this avoids leaving a half-committed session
185
+ // (token installed, user missing) which would let the next
186
+ // authenticated request 401 with no way to recover.
187
+ const previousAccessToken = this.httpService.getAccessToken();
188
+ this.httpService.setTokens(accessToken);
189
+ // The iframe typically returns `{ sessionId, accessToken }` without
190
+ // user data. Fetch the user explicitly so callers receive a
191
+ // fully-formed session and never need a second `/users/me` round
192
+ // trip. If this fails the session is unusable — revert the token
193
+ // and return null so the caller treats this exactly like a
194
+ // missing-session response.
195
+ if (!session.user) {
196
+ try {
197
+ const userData = await this.makeRequest('GET', `/session/user/${session.sessionId}`, undefined, { cache: false, retry: false });
198
+ if (!userData) {
199
+ throw new Error('Empty user response');
200
+ }
201
+ session.user = userData;
202
+ }
203
+ catch (userError) {
204
+ debug.warn('silentSignIn: failed to fetch user data, rolling back token', userError);
205
+ if (previousAccessToken) {
206
+ this.httpService.setTokens(previousAccessToken);
207
+ }
208
+ else {
209
+ this.httpService.clearTokens();
210
+ }
211
+ return null;
212
+ }
173
213
  }
174
214
  return session;
175
215
  }