-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: improved-auth-strategy (#56)
* refactor: improved-auth-strategy * chore: refactored err handling * feat: Link to OpenSauced on avatar click
- Loading branch information
Showing
7 changed files
with
57 additions
and
84 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,8 @@ | ||
export const SUPABASE_LOCAL_STORAGE_KEY = "supabase.auth.token"; | ||
export const SUPABASE_AUTH_DOMAIN = "ibcwmlhcimymasokhgvn.supabase.co"; | ||
export const SUPABASE_COOKIE_NAME = "sb-access-token"; | ||
export const SUPABASE_LOGIN_URL = "https://ibcwmlhcimymasokhgvn.supabase.co/auth/v1/authorize?provider=github&redirect_to=https://hot.opensauced.pizza/"; | ||
export const SUPABASE_LOGOUT_URL = "https://ibcwmlhcimymasokhgvn.supabase.co/auth/v1/logout"; | ||
export const SUPABASE_LOGIN_URL = "https://ibcwmlhcimymasokhgvn.supabase.co/auth/v1/authorize?provider=github&redirect_to=https://insights.opensauced.pizza/"; | ||
export const SUPABASE_AUTH_COOKIE_NAME = "supabase-auth-token"; | ||
export const OPEN_SAUCED_AUTH_TOKEN_KEY = "os-access-token"; | ||
export const OPEN_SAUCED_USERS_ENDPOINT = "https://api.opensauced.pizza/v1/users"; | ||
export const OPEN_SAUCED_SESSION_ENDPOINT = "https://api.opensauced.pizza/v1/auth/session"; | ||
export const OPEN_SAUCED_INSIGHTS_DOMAIN = "insights.opensauced.pizza"; | ||
export const GITHUB_PROFILE_MENU_SELECTOR = ".p-nickname.vcard-username.d-block"; | ||
export const GITHUB_PROFILE_EDIT_MENU_SELECTOR = "button.js-profile-editable-edit-button"; |
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
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,33 @@ | ||
import { | ||
OPEN_SAUCED_AUTH_TOKEN_KEY, | ||
SUPABASE_AUTH_COOKIE_NAME, | ||
OPEN_SAUCED_INSIGHTS_DOMAIN, | ||
} from "../constants"; | ||
import { checkTokenValidity } from "../utils/fetchOpenSaucedApiData"; | ||
import setAccessTokenInChromeStorage from "../utils/setAccessToken"; | ||
|
||
export const checkAuthentication = () => { | ||
chrome.cookies.get( | ||
{ | ||
name: SUPABASE_AUTH_COOKIE_NAME, | ||
url: `https://${OPEN_SAUCED_INSIGHTS_DOMAIN}`, | ||
}, | ||
async cookie => { | ||
if (!cookie) { | ||
return chrome.storage.sync.remove(OPEN_SAUCED_AUTH_TOKEN_KEY); | ||
} | ||
try { | ||
const authCookie = JSON.parse(decodeURIComponent(cookie.value))[0]; | ||
const isValidToken = await checkTokenValidity(authCookie); | ||
|
||
if (!isValidToken) { | ||
return chrome.storage.sync.remove(OPEN_SAUCED_AUTH_TOKEN_KEY); | ||
} | ||
void setAccessTokenInChromeStorage(authCookie); | ||
} catch (error) { | ||
void chrome.storage.sync.remove(OPEN_SAUCED_AUTH_TOKEN_KEY); | ||
console.error("Error processing cookie:", error); | ||
} | ||
}, | ||
); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,20 @@ | ||
import { SUPABASE_LOGOUT_URL, SUPABASE_AUTH_DOMAIN, SUPABASE_COOKIE_NAME, OPEN_SAUCED_AUTH_TOKEN_KEY } from "../constants"; | ||
import { checkTokenValidity } from "../utils/fetchOpenSaucedApiData"; | ||
import setAccessTokenInChromeStorage from "../utils/setAccessToken"; | ||
|
||
chrome.webRequest.onCompleted.addListener( | ||
() => { | ||
void chrome.storage.sync.remove(OPEN_SAUCED_AUTH_TOKEN_KEY); | ||
}, | ||
{ urls: [SUPABASE_LOGOUT_URL] }, | ||
); | ||
import { checkAuthentication } from "../utils/checkAuthentication"; | ||
import { SUPABASE_AUTH_COOKIE_NAME, OPEN_SAUCED_INSIGHTS_DOMAIN } from "../constants"; | ||
|
||
chrome.tabs.onUpdated.addListener((tabId, changeInfo) => { | ||
if (changeInfo.url?.includes("github.com")) { | ||
void chrome.tabs.sendMessage(tabId, { message: "GITHUB_URL_CHANGED" }); | ||
} | ||
}); | ||
|
||
chrome.cookies.onChanged.addListener(async changeInfo => { | ||
try { | ||
if (changeInfo.cookie.name !== SUPABASE_COOKIE_NAME || changeInfo.cookie.domain !== SUPABASE_AUTH_DOMAIN) { | ||
return; | ||
} | ||
if (changeInfo.removed) { | ||
return chrome.storage.sync.remove(OPEN_SAUCED_AUTH_TOKEN_KEY); | ||
} | ||
const isValidToken = await checkTokenValidity(changeInfo.cookie.value); | ||
|
||
if (!isValidToken) { | ||
return chrome.storage.sync.remove(OPEN_SAUCED_AUTH_TOKEN_KEY); | ||
chrome.cookies.onChanged.addListener(changeInfo => { | ||
if ( | ||
changeInfo.cookie.name === SUPABASE_AUTH_COOKIE_NAME || | ||
changeInfo.cookie.domain === OPEN_SAUCED_INSIGHTS_DOMAIN | ||
) { | ||
checkAuthentication(); | ||
} | ||
void setAccessTokenInChromeStorage(changeInfo.cookie.value); | ||
} catch (error) { | ||
console.error("Error processing cookie update:", error); | ||
} | ||
}); | ||
|
||
chrome.runtime.onInstalled.addListener(checkAuthentication); | ||
chrome.runtime.onStartup.addListener(checkAuthentication); |