Skip to content

Commit

Permalink
Merged PR 31938: Detecteer verlopen city-pass access token
Browse files Browse the repository at this point in the history
detect expired city-pass token

Related work items: #124396
  • Loading branch information
RikSchefferAmsterdam committed Aug 29, 2024
2 parents 946684d + 231106d commit 76cb552
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/modules/city-pass/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@ import {
BudgetTransactionsParams,
DiscountTransactionsResponse,
TransactionsParams,
CityPassApiError,
CityPassError401Codes,
} from '@/modules/city-pass/types'
import {logout} from '@/modules/city-pass/utils/logout'
import {refreshTokens} from '@/modules/city-pass/utils/refreshTokens'
import {ModuleSlug} from '@/modules/slugs'
import {baseApi} from '@/services/baseApi'
import {AfterBaseQueryErrorFn} from '@/services/types'

type ApiError = {
detail: string
}

/**
* Removes a token that causes the backend to return a 403 forbidden error
*/
Expand All @@ -25,11 +23,10 @@ const afterError: AfterBaseQueryErrorFn = async (
{dispatch},
failRetry,
) => {
if (error?.status === 403) {
const {detail} = error.data as ApiError
if (error?.status === 401) {
const {code} = error.data as CityPassApiError

// TODO change this conditional to check for an enum and not match text
if (detail === 'Access token has expired') {
if (code === CityPassError401Codes.tokenExpired) {
return refreshTokens(dispatch, failRetry)
} else {
await logout(dispatch)
Expand Down
10 changes: 10 additions & 0 deletions src/modules/city-pass/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {SvgIconName} from '@/components/ui/media/svgIcons'
import {RedirectKey} from '@/modules/redirects/types'
import {ApiError} from '@/types/api'

export enum CityPassEndpointName {
getAccessToken = 'getAccessToken',
Expand Down Expand Up @@ -119,3 +120,12 @@ export enum LoginResult {
failed = 'mislukt',
success = 'gelukt',
}

export enum CityPassError401Codes {
apiKeyInvalid = 'API_KEY_INVALID',
tokenExpired = 'TOKEN_EXPIRED',
tokenInvalid = 'TOKEN_INVALID',
tokenNotReady = 'TOKEN_NOT_READY',
}

export type CityPassApiError = ApiError<CityPassError401Codes>
5 changes: 5 additions & 0 deletions src/types/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,8 @@ export enum TimeOutDuration {
medium = 15000,
long = 30000,
}

export type ApiError<CodesEnum = unknown> = {
code: CodesEnum
detail: string
}

0 comments on commit 76cb552

Please sign in to comment.