Skip to content

Commit

Permalink
Address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
gitkrakel committed Mar 1, 2024
1 parent 816c5b1 commit 09b099c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/domUtils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
export const createAnchor = (href: string, target?: string, callback?: () => void) => {
export const createAnchor = (href: string, target?: string, onclick?: () => void) => {
const a = document.createElement('a');
a.href = href;
if (target) {
a.target = target;
}
if (callback) {
a.addEventListener('click', callback);
if (onclick) {
a.addEventListener('click', onclick);
}

return a;
Expand Down
19 changes: 8 additions & 11 deletions src/gkApi.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { cookies } from 'webextension-polyfill';
import { checkOrigins } from './permissions-helper';
import { updateExtensionIcon } from './shared';
import type { User } from './types';

Expand All @@ -9,23 +10,19 @@ const accessTokenCookieUrl = 'https://gitkraken.dev';
const accessTokenCookieName = MODE === 'production' ? 'accessToken' : 'devAccessToken';

const getAccessToken = async () => {
try {
// Check if the user has granted permission to GitKraken.dev
if (!await checkOrigins(['gitkraken.dev'])) {
// If not, just assume we're logged out
return undefined;
}

// Attempt to get the access token cookie from GitKraken.dev
const cookie = await cookies.get({
url: accessTokenCookieUrl,
name: accessTokenCookieName,
});

return cookie?.value;
} catch (e) {
if ((e as Error)?.message.includes('No host permissions for cookies at url')) {
// ignore as we are waiting for required permissions
} else {
// otherwise log error and continue as if logged out
console.error(e);
}
}
return undefined;
return cookie?.value;
};

export const fetchUser = async () => {
Expand Down
6 changes: 6 additions & 0 deletions src/permissions-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,9 @@ export async function refreshPermissions(): Promise<PermissionsRequest | undefin
}
: undefined;
}

export async function checkOrigins(origins: string[]): Promise<boolean> {
return permissions.contains({
origins: origins.map(domainToMatchPattern)
});
}

0 comments on commit 09b099c

Please sign in to comment.