Skip to content

Commit

Permalink
fix: oauth login
Browse files Browse the repository at this point in the history
  • Loading branch information
0x4007 committed Dec 4, 2023
1 parent d92e012 commit b375ed2
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 53 deletions.
2 changes: 1 addition & 1 deletion build/plugins/invert-colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const invertColors: esbuild.Plugin = {
if (color.length === 3) {
color = color
.split("")
.map((char) => char + char)
.map((char: string) => char + char)
.join("");
}
const r = parseInt(color.slice(0, 2), 16);
Expand Down
35 changes: 12 additions & 23 deletions src/home/authenticated-get-github-user.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Octokit } from "@octokit/rest";
import { getExistingSessionToken } from "./check-for-github-access-token";
import { getLocalStoreOauth } from "./check-for-github-access-token";

export async function authenticatedGetGitHubUser(): Promise<GitHubUser | null> {
const activeSessionToken = await getActiveSessionToken();
Expand All @@ -11,39 +11,28 @@ export async function authenticatedGetGitHubUser(): Promise<GitHubUser | null> {
}

async function getActiveSessionToken(): Promise<string | null> {
let token = getExistingSessionToken();
if (!token) {
token = await getNewSessionToken();
const cachedSessionToken = getLocalStoreOauth();
if (cachedSessionToken) {
return cachedSessionToken.provider_token;
}
if (!token) {
console.error("No token found");

const newSessionToken = await getNewSessionToken();
if (newSessionToken) {
return newSessionToken;
}
return token;

console.error("No session token found");

return null;
}

async function getNewSessionToken(): Promise<string | null> {
const hash = window.location.hash;
const params = new URLSearchParams(hash.substr(1)); // remove the '#' and parse
// access_token=eyJhbGciOiJIUzI1NiIsImtpZCI6InJCQVV5bHBBeUN5Sk1LVUIiLCJ0eXAiOiJKV1QifQ.eyJhdWQiOiJhdXRoZW50aWNhdGVkIiwiZXhwIjoxNzAxMzQ0NjY0LCJpYXQiOjE3MDEzNDEwNjQsImlzcyI6Imh0dHBzOi8vd2Z6cGV3bWx5aW96dXB1bGJ1dXIuc3VwYWJhc2UuY28vYXV0aC92MSIsInN1YiI6IjE3NmRlZjk0LWNiMGEtNGNlYi1iMWMwLTg1ODJiMDlmZmE5ZCIsImVtYWlsIjoiZ2l0aHViQHBhdmxvdmNpay5jb20iLCJwaG9uZSI6IiIsImFwcF9tZXRhZGF0YSI6eyJwcm92aWRlciI6ImdpdGh1YiIsInByb3ZpZGVycyI6WyJnaXRodWIiXX0sInVzZXJfbWV0YWRhdGEiOnsiYXZhdGFyX3VybCI6Imh0dHBzOi8vYXZhdGFycy5naXRodWJ1c2VyY29udGVudC5jb20vdS80OTc1NjcwP3Y9NCIsImVtYWlsIjoiZ2l0aHViQHBhdmxvdmNpay5jb20iLCJlbWFpbF92ZXJpZmllZCI6dHJ1ZSwiZnVsbF9uYW1lIjoi44Ki44Os44Kv44K144Oz44OA44O8LmV0aCIsImlzcyI6Imh0dHBzOi8vYXBpLmdpdGh1Yi5jb20iLCJuYW1lIjoi44Ki44Os44Kv44K144Oz44OA44O8LmV0aCIsInBob25lX3ZlcmlmaWVkIjpmYWxzZSwicHJlZmVycmVkX3VzZXJuYW1lIjoicGF2bG92Y2lrIiwicHJvdmlkZXJfaWQiOiI0OTc1NjcwIiwic3ViIjoiNDk3NTY3MCIsInVzZXJfbmFtZSI6InBhdmxvdmNpayJ9LCJyb2xlIjoiYXV0aGVudGljYXRlZCIsImFhbCI6ImFhbDEiLCJhbXIiOlt7Im1ldGhvZCI6Im9hdXRoIiwidGltZXN0YW1wIjoxNzAxMzQxMDY0fV0sInNlc3Npb25faWQiOiI0YzBlNjA5MC0zZGJmLTQ4OTMtYTRjYi1lYTlmZTIwZDQ1YjcifQ.YgLDGqngdIBCO2o041Xv0UzymdMgYQlW8GLBmdfDKkM
// &expires_at=1701344664
// &expires_in=3600
// &provider_token=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
// &refresh_token=Zi1ixXNvljvBkqexEriiVA
// &token_type=bearer
const providerToken = params.get("provider_token");

if (!providerToken) {
// throw new Error("Access token not found in URL fragment");
return null;
}

const expiresAt = params.get("expires_at");
if (expiresAt && parseInt(expiresAt, 10) < Date.now() / 1000) {
localStorage.removeItem("provider_token");
} else if (providerToken) {
localStorage.setItem("provider_token", providerToken);
}

return providerToken;
}

Expand Down
82 changes: 70 additions & 12 deletions src/home/check-for-github-access-token.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,84 @@
import { renderGitHubLoginButton } from "./github-login-button";

let gitHubAccessToken: null | string = null;

export function checkForGitHubAccessToken(): string | null {
const accessToken = localStorage.getItem("provider_token");
const expiresAt = localStorage.getItem("expires_at");
const oauthToken = getLocalStoreOauth();

if (expiresAt && parseInt(expiresAt, 10) < Date.now() / 1000) {
// expired
localStorage.removeItem("provider_token");
localStorage.removeItem("expires_at");
const expiresAt = oauthToken?.expires_at;
if (expiresAt) {
if (expiresAt < Date.now() / 1000) {
localStorage.removeItem("sb-wfzpewmlyiozupulbuur-auth-token");
}
}

const accessToken = oauthToken?.provider_token;
if (accessToken) {
gitHubAccessToken = accessToken;
return accessToken;
} else {
renderGitHubLoginButton();
return null;
}
return null;
}

export function getLocalStoreOauth(): ExampleAuthToken | null {
const oauthToken = localStorage.getItem("sb-wfzpewmlyiozupulbuur-auth-token");
if (!oauthToken) return null;
return JSON.parse(oauthToken);
}

export function getExistingSessionToken() {
return gitHubAccessToken;
interface ExampleAuthToken {
provider_token: string;
access_token: string;
expires_in: number;
expires_at: number;
refresh_token: string;
token_type: string;
user: {
id: string;
aud: string;
role: string;
email: string;
email_confirmed_at: string;
phone: string;
confirmed_at: string;
last_sign_in_at: string;
app_metadata: { provider: string; providers: string[] };
user_metadata: {
avatar_url: string;
email: string;
email_verified: boolean;
full_name: string;
iss: string;
name: string;
phone_verified: boolean;
preferred_username: string;
provider_id: string;
sub: string;
user_name: string;
};
identities: [
{
id: string;
user_id: string;
identity_data: {
avatar_url: string;
email: string;
email_verified: boolean;
full_name: string;
iss: string;
name: string;
phone_verified: boolean;
preferred_username: string;
provider_id: string;
sub: string;
user_name: string;
};
provider: string;
last_sign_in_at: string;
created_at: string;
updated_at: string;
},
];
created_at: string;
updated_at: string;
};
}
15 changes: 7 additions & 8 deletions src/home/display-github-issues.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Octokit } from "@octokit/rest";
import { homeController } from "./home-controller";
import { GitHubIssue } from "./github-types";
import { homeController } from "./home-controller";

export type GitHubIssueWithNewFlag = GitHubIssue & { isNew?: boolean };

Expand Down Expand Up @@ -70,21 +70,20 @@ async function fetchIssues(container: HTMLDivElement, accessToken: string | null
console.error(error);
}
// Fetch fresh issues and mark them as new
const freshIssues: GitHubIssueWithNewFlag[] = (
await octokit.paginate("GET /repos/ubiquity/devpool-directory/issues", {
state: "open",
})
).map((issue) => ({ ...issue, isNew: true }));
const freshIssues: GitHubIssue[] = await octokit.paginate("GET /repos/ubiquity/devpool-directory/issues", {
state: "open",
});
const freshIssuesWithNewFlag = freshIssues.map((issue) => ({ ...issue, isNew: true })) as GitHubIssueWithNewFlag[];

// Sort the fresh issues
const sortedIssuesByTime = sortIssuesByTime(freshIssues);
const sortedIssuesByTime = sortIssuesByTime(freshIssuesWithNewFlag);
const sortedIssuesByPriority = sortIssuesByPriority(sortedIssuesByTime);

// Pass the fresh issues to the homeController
await homeController(container, sortedIssuesByPriority);

// Remove the 'isNew' flag before saving to localStorage
const issuesToSave = freshIssues.map(({ ...issue }) => {
const issuesToSave = freshIssuesWithNewFlag.map(({ ...issue }) => {
delete issue.isNew;
return issue;
});
Expand Down
4 changes: 2 additions & 2 deletions src/home/display-github-user-information.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async function signOut() {
console.error("Error logging out:", error);
alert(error);
}
localStorage.removeItem("provider_token");
localStorage.removeItem("expires_at");
// localStorage.removeItem("provider_token");
// localStorage.removeItem("expires_at");
window.location.reload();
}
8 changes: 1 addition & 7 deletions src/home/github-login-button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,7 @@ export function getSupabase() {
}

async function gitHubLoginButton() {
const { error } = await supabase.auth.signInWithOAuth({
provider: "github",
options: {
redirectTo: "https://wfzpewmlyiozupulbuur.supabase.co/auth/v1/callback",
},
});

const { error } = await supabase.auth.signInWithOAuth({ provider: "github" });
if (error) {
console.error("Error logging in:", error);
}
Expand Down

0 comments on commit b375ed2

Please sign in to comment.