Skip to content

Commit f515624

Browse files
Merge pull request #170 from StackOverflowIsBetterThanAnyAI/163-implement-new-auth-system
implement new auth system to remove unnecessary auth calls which flooded the network
2 parents d910807 + 60e1d48 commit f515624

File tree

5 files changed

+74
-7
lines changed

5 files changed

+74
-7
lines changed

src/components/streamFeed/StreamProfilePicture.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ const StreamProfilePicture: FC<StreamProfilePictureProps> = ({
9696
const data = await getProfilePicture(user_id || '')
9797
if (!data) {
9898
throw new Error()
99-
} else setImageUrl(data)
99+
} else {
100+
setImageUrl(data)
101+
}
100102
} catch (error: any) {}
101103
}
102104
fetchImageUrl()

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

src/helper/getTwitchAuthorization.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import axios from 'axios'
22

33
import { AuthorizationProps } from '../types/AuthorizationProps'
4+
import { setItemInStorage } from './setItemInStorage'
45

56
export const getTwitchAuthorization = async (): Promise<AuthorizationProps> => {
67
try {
@@ -9,6 +10,11 @@ export const getTwitchAuthorization = async (): Promise<AuthorizationProps> => {
910
)
1011
if (response.status !== 200 || typeof response.data !== 'object')
1112
throw new Error(`${response.status}`)
13+
14+
setItemInStorage('access_token', response.data.access_token)
15+
setItemInStorage('expires_in', response.data.expires_in)
16+
setItemInStorage('token_type', response.data.token_type)
17+
1218
return response.data
1319
} catch (error: any) {
1420
console.error(

src/helper/getUser.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const getUser = async (): Promise<UserProps | null> => {
3232
}
3333
)
3434

35-
if (response.status !== 200 || typeof response !== 'object')
35+
if (response.status !== 200 || typeof response.data !== 'object')
3636
throw new Error(`${response.status}`)
3737

3838
user = response.data

0 commit comments

Comments
 (0)