Skip to content

Commit

Permalink
RJS-2789: Surface errors when linking credentials fails (#6588)
Browse files Browse the repository at this point in the history
* Adding an error_code check for "InvalidSession" before refreshing access tokens

* "build" got renamed to "bundle"

* Fixing tsconfig.json

* Comitting changes made by npm 10.5.0

* Adding a changelog header

* Adding a note to the changelog

* Fixing unit tests

* Fixed handling invalid token signatures

* Update packages/realm-web/package.json
  • Loading branch information
kraenhansen authored Apr 4, 2024
1 parent 86eb923 commit 4e99e63
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 74 deletions.
71 changes: 13 additions & 58 deletions package-lock.json

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions packages/realm-web/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
## vNext (TBD)

### Deprecations
* None

### Enhancements
* None

### Fixed
* Fixed an endless loop of requests that would happen if linking credentials failed due to an authentication failure. ([#6588](https://github.com/realm/realm-js/pull/6588), since v0.6.0)

### Internal
* None

2.0.0 Release notes (2022-10-18)
=============================================================

Expand Down
3 changes: 1 addition & 2 deletions packages/realm-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
"./dist/bundle.es.js": "./dist/bundle.dom.es.js"
},
"scripts": {
"prepack": "npm run build",
"bundle": "wireit",
"start": "npm run build -- --watch",
"start": "npm run bundle -- --watch",
"lint": "eslint --ext .js,.ts .",
"test": "mocha",
"postversion": "ts-node --project scripts/tsconfig.json scripts/postversion.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/realm-web/src/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,6 @@ export class App<
*/
private hydrate() {
const userIds = this.storage.getUserIds();
this.users = userIds.map((id) => new User({ app: this, id }));
this.users = userIds.map((id) => new User<FunctionsFactoryType, CustomDataType>({ app: this, id }));
}
}
30 changes: 19 additions & 11 deletions packages/realm-web/src/Fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,20 +237,28 @@ export class Fetcher implements LocationUrlContext {

if (response.ok) {
return response;
} else if (user && response.status === 401 && tokenType === "access") {
// If the access token has expired, it would help refreshing it
await user.refreshAccessToken();
// Retry with the specific user, since the currentUser might have changed.
return this.fetch({ ...request, user });
} else {
if (user && response.status === 401 && tokenType === "refresh") {
// A 401 error while using the refresh token indicates the token has an issue.
// Reset the tokens to prevent a lock.
user.accessToken = null;
user.refreshToken = null;
const error = await MongoDBRealmError.fromRequestAndResponse(url, request, response);
if (
user &&
response.status === 401 &&
(error.errorCode === "InvalidSession" || // Expired token
error.error === "unauthorized") // Entirely invalid signature
) {
if (tokenType === "access") {
// If the access token has expired, it would help refreshing it
await user.refreshAccessToken();
// Retry with the specific user, since the currentUser might have changed.
return this.fetch({ ...request, user });
} else if (tokenType === "refresh") {
// A 401 error while using the refresh token indicates the token has an issue.
// Reset the tokens to prevent a lock.
user.accessToken = null;
user.refreshToken = null;
}
}
// Throw an error with a message extracted from the body
throw await MongoDBRealmError.fromRequestAndResponse(url, request, response);
throw error;
}
} else {
throw new Error("Expected either 'url' or 'path'");
Expand Down
2 changes: 1 addition & 1 deletion packages/realm-web/src/tests/utils/MockFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export function createMockFetch(responses: unknown[]): MockFetch {
url: response.url,
json: async () => ({
error: response.error,
errorCode: response.errorCode,
error_code: response.errorCode,
link: response.link,
}),
headers: {
Expand Down
1 change: 1 addition & 0 deletions packages/realm-web/src/tests/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export const INVALID_SESSION_ERROR = new MongoDBRealmError(
401,
"",
"invalid session",
"InvalidSession",
);

export * from "./MockApp";
Expand Down
1 change: 1 addition & 0 deletions packages/realm-web/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"typeRoots": [
"./types",
"./node_modules/@types",
"../../node_modules",
"../../node_modules/@types",
],
"types": [
Expand Down
2 changes: 1 addition & 1 deletion packages/realm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -377,4 +377,4 @@
6
]
}
}
}

0 comments on commit 4e99e63

Please sign in to comment.