Skip to content

Commit 60e1d48

Browse files
call api with data from storage if available, if credentials have expired, renew them
1 parent 0369d33 commit 60e1d48

File tree

2 files changed

+64
-5
lines changed

2 files changed

+64
-5
lines changed

src/helper/getProfilePicture.tsx

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
import axios from 'axios'
22
import { AuthorizationProps } from '../types/AuthorizationProps'
3+
import { getItemFromStorage } from './getItemFromStorage'
34
import { getTwitchAuthorization } from './getTwitchAuthorization'
45

56
export const getProfilePicture = async (
67
user_id: string
78
): Promise<string | undefined> => {
9+
const parsedData = getItemFromStorage()
810
const authorizationObject: AuthorizationProps =
9-
await getTwitchAuthorization()
11+
parsedData.access_token &&
12+
parsedData.expires_in &&
13+
parsedData.token_type
14+
? {
15+
access_token: parsedData.access_token,
16+
expires_in: parsedData.expires_in,
17+
token_type: parsedData.token_type,
18+
}
19+
: await getTwitchAuthorization()
1020
let { access_token, token_type } = authorizationObject
11-
if (!access_token || !token_type) return undefined
21+
if (!access_token || !token_type) {
22+
return undefined
23+
}
1224

1325
token_type =
1426
token_type.substring(0, 1).toUpperCase() +
@@ -30,11 +42,29 @@ export const getProfilePicture = async (
3042

3143
return response.data.imageUrl
3244
} catch (error) {
45+
if (axios.isAxiosError(error) && error.response?.status === 401) {
46+
console.warn('Token expired, refreshing...')
47+
48+
const retryAuth: AuthorizationProps = await getTwitchAuthorization()
49+
if (retryAuth.access_token) {
50+
const retryResponse = await axios.post(
51+
'https://twitch-backend.vercel.app/api/profile-picture',
52+
{
53+
access_token: retryAuth.access_token,
54+
token_type: retryAuth.token_type,
55+
user_id,
56+
}
57+
)
58+
return retryResponse.data.imageUrl
59+
}
60+
}
61+
3362
console.error(
3463
'The following error occurred while fetching a user profile picture for the user:',
3564
user_id,
3665
error
3766
)
67+
3868
return undefined
3969
}
4070
}

src/helper/getStreams.tsx

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
11
import axios from 'axios'
22
import { AuthorizationProps } from '../types/AuthorizationProps'
3+
import { StreamProps } from '../types/StreamProps'
4+
import { getItemFromStorage } from './getItemFromStorage'
35
import { getTwitchAuthorization } from './getTwitchAuthorization'
4-
import type { StreamProps } from '../types/StreamProps'
56

67
export const getStreams = async (
78
url: string
89
): Promise<StreamProps | undefined | { error: 'login' }> => {
10+
const parsedData = getItemFromStorage()
911
const authorizationObject: AuthorizationProps =
10-
await getTwitchAuthorization()
12+
parsedData.access_token &&
13+
parsedData.expires_in &&
14+
parsedData.token_type
15+
? {
16+
access_token: parsedData.access_token,
17+
expires_in: parsedData.expires_in,
18+
token_type: parsedData.token_type,
19+
}
20+
: await getTwitchAuthorization()
1121
let { access_token, token_type } = authorizationObject
12-
if (!access_token || !token_type) return { error: 'login' }
22+
if (!access_token || !token_type) {
23+
return { error: 'login' }
24+
}
1325

1426
token_type =
1527
token_type.substring(0, 1).toUpperCase() +
@@ -36,6 +48,23 @@ export const getStreams = async (
3648

3749
return response.data
3850
} catch (error: any) {
51+
if (axios.isAxiosError(error) && error.response?.status === 401) {
52+
console.warn('Token expired, refreshing...')
53+
54+
const retryAuth: AuthorizationProps = await getTwitchAuthorization()
55+
if (retryAuth.access_token) {
56+
const retryResponse = await axios.post(
57+
'https://twitch-backend.vercel.app/api/streams',
58+
{
59+
access_token: retryAuth.access_token,
60+
token_type: retryAuth.token_type,
61+
url,
62+
}
63+
)
64+
return retryResponse.data
65+
}
66+
}
67+
3968
console.error(
4069
'The following error occured while fetching the current Livestreams:',
4170
error

0 commit comments

Comments
 (0)