Skip to content

Commit

Permalink
Auth fix (#7460)
Browse files Browse the repository at this point in the history
* auth success only if online

* fix reconnect

* lint/format

Co-authored-by: Roshan Padaki <[email protected]>
  • Loading branch information
Ming and Roshan Padaki authored Oct 20, 2022
1 parent 44da68c commit e140568
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 18 deletions.
34 changes: 21 additions & 13 deletions browser/hybrid/components/cloud_tabs_extension/src/@core-ts/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,35 @@ import stringify from "json-stringify-safe"
const httpConfig =
(method: string) =>
async (args: { body: object; url: string; accessToken?: string }) => {
const response = await fetch(args.url, {
method,
headers: {
"Content-Type": "application/json",
...(args.accessToken !== undefined && {
Authorization: `Bearer ${args.accessToken}`,
}),
},
body: stringify(args.body),
})
let response = null

try {
const json = await response.json()
response = await fetch(args.url, {
method,
headers: {
"Content-Type": "application/json",
...(args.accessToken !== undefined && {
Authorization: `Bearer ${args.accessToken}`,
}),
},
body: stringify(args.body),
})
} catch (err) {
return {
status: 500,
json: {},
}
}

try {
const json = await response.json()
return {
status: response.status,
status: response?.status ?? 500,
json,
}
} catch (err) {
return {
status: response.status,
status: response?.status ?? 500,
json: {},
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { filter, share, switchMap, take, throttle, map } from "rxjs/operators"
import { NodeEventHandler } from "rxjs/internal/observable/fromEvent"

import { webpageLoaded } from "@app/worker/events/webRequest"
import { activatedFromSleep } from "@app/worker/events/idle"
import { activatedFromSleep, networkOnline } from "@app/worker/events/idle"
import {
authSuccess as _authSuccess,
authFailure as _authFailure,
Expand Down Expand Up @@ -43,9 +43,11 @@ const authInfo = merge(
merge(
webpageLoaded.pipe(take(1)),
activatedFromSleep,
networkOnline,
timer(0, DAY_MS / 2)
).pipe(
// We throttle by 10s so there's no race condition of two simultaneous auth refresh requests
filter(() => navigator.onLine),
throttle(() => interval(SEC_MS * 10)),
switchMap(() => from(initWhistAuth()))
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@ const activatedFromSleep = fromEventPattern(
(newState: chrome.idle.IdleState) => newState
).pipe(filter((newState: chrome.idle.IdleState) => newState === "active"))

export { activatedFromSleep }
const networkOnline = fromEventPattern(
(handler: any) => window.addEventListener("online", handler),
(handler: any) => window.removeEventListener("online", handler),
(args: any) => args
)

export { activatedFromSleep, networkOnline }
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { from, of } from "rxjs"
import { from, of, merge } from "rxjs"
import { switchMap, map, filter, share } from "rxjs/operators"

import { getStorage } from "@app/worker/utils/storage"
Expand All @@ -7,15 +7,20 @@ import {
mandelboxCreateSuccess,
} from "@app/worker/utils/mandelbox"
import { stateDidChange, whistState } from "@app/worker/utils/state"
import { authSuccess } from "./auth"

import { MandelboxState, Storage } from "@app/constants/storage"
import { config } from "@app/constants/app"
import { AsyncReturnType } from "@app/@types/api"
import { AWSRegion, AWSRegionOrdering } from "@app/constants/location"
import { AuthInfo } from "@app/@types/payload"

const mandelboxNeeded = stateDidChange("waitingCloudTabs").pipe(
filter((change: any) => change?.applyData?.name === "push"),
const mandelboxNeeded = merge(
stateDidChange("waitingCloudTabs").pipe(
filter((change: any) => change?.applyData?.name === "push")
),
authSuccess.pipe(filter(() => whistState.waitingCloudTabs.length > 0))
).pipe(
filter(
() =>
whistState.mandelboxState === MandelboxState.MANDELBOX_NONEXISTENT &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,7 @@ const socketReconnectFailed = merge(socket, socketConnected).pipe(
)
)

// I'm not sure why this fixes things but it does
socket.subscribe()

export { socket, socketConnected, socketDisconnected, socketReconnectFailed }

0 comments on commit e140568

Please sign in to comment.