Skip to content

Commit

Permalink
refactors to solve for supertokens/supertokens-web-js#62
Browse files Browse the repository at this point in the history
  • Loading branch information
rishabhpoddar committed Jan 18, 2023
1 parent 662ee62 commit 8a7a8d0
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 43 deletions.
2 changes: 1 addition & 1 deletion bundle/bundle.js

Large diffs are not rendered by default.

11 changes: 2 additions & 9 deletions lib/build/fetch.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 7 additions & 10 deletions lib/build/utils/cookieHandler/defaultImplementation.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 2 additions & 11 deletions lib/ts/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,6 @@ import WindowHandlerReference from "./utils/windowHandler";
import LockFactoryReference from "./utils/lockFactory";
import { logDebugMessage } from "./logger";

function getWindowOrThrow(): Window {
if (typeof window === "undefined") {
throw Error(
"If you are using this package with server-side rendering, please make sure that you are checking if the window object is defined."
);
}

return window;
}

export class AntiCsrfToken {
private static tokenInfo:
| undefined
Expand Down Expand Up @@ -163,7 +153,8 @@ export default class AuthHttpRequest {
logDebugMessage("init: Input sessionExpiredStatusCode: " + config.sessionExpiredStatusCode);
logDebugMessage("init: Input sessionScope: " + config.sessionScope);

AuthHttpRequest.env = getWindowOrThrow().fetch === undefined ? global : getWindowOrThrow();
const fetchedWindow = WindowHandlerReference.getReferenceOrThrow().windowHandler.getWindowUnsafe();

This comment has been minimized.

Copy link
@sublimator

sublimator Jan 18, 2023

Contributor

If we are to override this and return something that's not a window (i.e. self inside a ServiceWorker, which also has a fetch property), maybe it should be called something more generic ?

AuthHttpRequest.env = fetchedWindow === undefined || fetchedWindow.fetch === undefined ? global : fetchedWindow;

AuthHttpRequest.refreshTokenUrl = config.apiDomain + config.apiBasePath + "/session/refresh";
AuthHttpRequest.signOutUrl = config.apiDomain + config.apiBasePath + "/signout";
Expand Down
15 changes: 3 additions & 12 deletions lib/ts/utils/cookieHandler/defaultImplementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,13 @@
* under the License.
*/
import { CookieHandlerInterface } from "./types";

function getWindowOrThrow(): Window {
if (typeof window === "undefined") {
throw Error(
"If you are using this package with server-side rendering, please make sure that you are checking if the window object is defined."
);
}

return window;
}
import WindowHandlerReference from "../windowHandler";

export const defaultCookieHandlerImplementation: CookieHandlerInterface = {
getCookie: async function () {
return getWindowOrThrow().document.cookie;
return WindowHandlerReference.getReferenceOrThrow().windowHandler.getWindowUnsafe().document.cookie;

This comment has been minimized.

Copy link
@sublimator

sublimator Jan 18, 2023

Contributor

Oh, I see it's used here

},
setCookie: async function (cookieString: string) {
getWindowOrThrow().document.cookie = cookieString;
WindowHandlerReference.getReferenceOrThrow().windowHandler.getWindowUnsafe().document.cookie = cookieString;
}
};

0 comments on commit 8a7a8d0

Please sign in to comment.