Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/state/epics/auth.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
import { combineEpics, ofType } from 'redux-observable'
import { concatMap } from 'rxjs/operators'
import { of } from 'rxjs'
import { concatMap, filter } from 'rxjs/operators'
import { getUserInfo } from '~/apis/authsrv'
import config from '~/config'
import { clearFormCache } from '~/hooks/funnels/form'
import type { AppState } from '~/state'
import type { AnyAppAction, GetAction } from '~/state/actions'
import {
InitiateLogin,
LoadUserInfo,
LookupUserInfo,
} from '~/state/actions/auth'
import { catchAppError } from './operators/catch-app-error'
import { of } from 'rxjs'
import { loadAutosave, removeAutosave } from '~/state/models/autosave'
import { clearFormCache } from '~/hooks/funnels/form'
import { load, save } from '~/util/local-storage'
import { catchAppError } from './operators/catch-app-error'
import { justDo } from './operators/just-do'

const LAST_LOGIN_ATTEMPT_KEY = 'last-login-attempt'
const LOGIN_THROTTLE_MS = 10_000 // 10 seconds

const shouldAllowLoginRedirect = (): boolean => {
const lastAttempt = load<number>(LAST_LOGIN_ATTEMPT_KEY)
if (lastAttempt === null) {
return true
}

const timeSinceLastAttempt = Date.now() - lastAttempt
return timeSinceLastAttempt >= LOGIN_THROTTLE_MS
}

export default combineEpics<
GetAction<AnyAppAction>,
GetAction<AnyAppAction>,
Expand All @@ -23,8 +37,9 @@ export default combineEpics<
(action$) =>
action$.pipe(
ofType(InitiateLogin.type),
filter(() => shouldAllowLoginRedirect()),
justDo(() => {
// eslint-disable-next-line no-process-env
save(LAST_LOGIN_ATTEMPT_KEY, Date.now())
location.href = `${config.apis.authsrv.url}/auth?app_name=${config.apis.authsrv.appName}${process.env.NODE_ENV === 'development' ? '' : `&dropoff_url=${location.href}`}`
})
),
Expand Down