-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move twitch login to backend API call, rather than exposing access to…
…ken in URL in browser Fixes #311
- Loading branch information
Showing
36 changed files
with
466 additions
and
355 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import type { TwitchAccess } from '@/types'; | ||
import { userManager } from '@/utilities/auth'; | ||
import type { | ||
TwitchAuthRequest, | ||
TwitchCallbackRequest, | ||
} from 'glowing-telegram-types/src/types'; | ||
|
||
const { VITE_API_URL: baseApiUrl } = import.meta.env; | ||
|
||
export async function authenticatedFetch( | ||
url: string, | ||
options: RequestInit = {}, | ||
): Promise<Response> { | ||
const user = await userManager.getUser(); | ||
|
||
if (!user) { | ||
throw new Error('User not found'); | ||
} | ||
|
||
const token = user.id_token; | ||
|
||
if (token === undefined) { | ||
throw new Error('User not authenticated'); | ||
} | ||
|
||
return fetch(url, { | ||
...options, | ||
headers: { | ||
Authorization: token, | ||
Accept: 'application/json', | ||
...options.headers, | ||
}, | ||
}); | ||
} | ||
|
||
export async function fetchTwitchAccessToken(): Promise<TwitchAccess> { | ||
const url = new URL('auth/twitch/token', baseApiUrl); | ||
|
||
try { | ||
const res = await authenticatedFetch(url.toString()); | ||
|
||
const data = await res.json(); | ||
|
||
return { | ||
valid: true, | ||
id: data.broadcaster_id, | ||
accessToken: data.access_token, | ||
}; | ||
} catch (error) { | ||
return { | ||
id: 'twitchToken', | ||
valid: false, | ||
}; | ||
} | ||
} | ||
|
||
export async function generateAuthorizeUri( | ||
provider: 'twitch', | ||
scopes: string[], | ||
): Promise<string> { | ||
const url = new URL(`auth/${provider}/url`, baseApiUrl); | ||
|
||
const body: TwitchAuthRequest = { | ||
scopes, | ||
redirect_uri: window.location.href, | ||
}; | ||
|
||
const res = await authenticatedFetch(url.toString(), { | ||
method: 'POST', | ||
body: JSON.stringify(body), | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
|
||
const data = await res.json(); | ||
|
||
return data.url; | ||
} | ||
|
||
export async function handleOAuthCallback( | ||
provider: 'twitch', | ||
code: string, | ||
state: string, | ||
): Promise<string> { | ||
const body: TwitchCallbackRequest = { | ||
code, | ||
state, | ||
scope: [], | ||
}; | ||
|
||
const res = await authenticatedFetch( | ||
new URL(`auth/${provider}/callback`, baseApiUrl).toString(), | ||
{ | ||
method: 'POST', | ||
body: JSON.stringify(body), | ||
redirect: 'manual', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}, | ||
); | ||
|
||
const data = await res.json(); | ||
|
||
return data.url; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.