Skip to content

Commit dd53f68

Browse files
tonglilkarpikpl
andauthored
Fix Authentication token expiration check (#193)
* throw an error if a token does not exist when building Authorization header * Update server/modules/authentication.ts * bump to 2.0.4 --------- Co-authored-by: Piotr Karpala <[email protected]>
1 parent 006d782 commit dd53f68

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "copilot-metrics-viewer",
3-
"version": "2.0.3",
3+
"version": "2.0.4",
44
"private": true,
55
"type": "module",
66
"scripts": {

server/modules/authentication.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,20 @@ export async function authenticateAndGetGitHubHeaders(event: H3Event<EventHandle
3030
const { secure } = await getUserSession(event);
3131

3232
// check if token is expired and get new one
33-
if (secure && secure.expires_at < new Date(Date.now() - 30 * 1000)) {
33+
if (secure?.expires_at && secure.expires_at < new Date(Date.now() - 30 * 1000)) {
3434
// Token is expired or about to expire within 30 seconds
3535
// we could refresh but unlikely dashboard is used for long periods
3636
return buildHeaders('');
3737
}
3838

39-
return buildHeaders(secure?.tokens.access_token || '');
39+
return buildHeaders(secure?.tokens?.access_token || '');
4040
}
4141

4242
function buildHeaders(token: string): Headers {
43+
if (!token) {
44+
throw new Error('GitHub token is required');
45+
}
46+
4347
return new Headers({
4448
Accept: "application/vnd.github+json",
4549
"X-GitHub-Api-Version": "2022-11-28",

0 commit comments

Comments
 (0)