-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Populate permits from metadata #377
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { createClient } from "@supabase/supabase-js"; | ||
import { AppState } from "./app-state"; | ||
import { getGitHubAccessToken, renderGitHubLoginButton } from "../shared/auth/github"; | ||
|
||
declare const SUPABASE_URL: string; | ||
declare const SUPABASE_ANON_KEY: string; | ||
|
||
const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY); | ||
|
||
export async function readClaimDataFromMetadata(app: AppState) { | ||
const { | ||
data: { user }, | ||
} = await supabase.auth.getUser(); | ||
const accessToken = await getGitHubAccessToken(); | ||
if (!accessToken) { | ||
// import the "login with github" button from work.ubq.fi | ||
renderGitHubLoginButton(); | ||
} | ||
|
||
let githubId; | ||
if (user?.identities) { | ||
githubId = user?.identities[0].id; | ||
} else { | ||
return; | ||
} | ||
|
||
// Using our metadata system we can easily GraphQL query for permits. | ||
const permits = (await supabase.from("permits").select("*").eq("beneficiary_id", githubId)).data; | ||
|
||
// GraphQL query with authentication for private repos. | ||
if (!permits || !permits.length) { | ||
return false; | ||
} | ||
|
||
const userData = (await supabase.from("users").select("*").eq("id", githubId)).data; | ||
if (!userData) return; | ||
const walletData = (await supabase.from("wallets").select("*").eq("id", userData[0]['wallet_id'])).data; | ||
if (!walletData) return; | ||
app.claims.push(...(permits.map(permit => { | ||
return { | ||
...permit, | ||
beneficiary: walletData[0].address, | ||
networkId: 31337, | ||
// TODO | ||
owner: "0x70997970C51812dc3A010C7d01b50e0d17dc79C8", | ||
// TODO | ||
tokenAddres: "0xe91D153E0b41518A2Ce8Dd3D7944Fa863463a97d", | ||
tokenType: "ERC20" | ||
Comment on lines
+44
to
+48
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't make sense should be read from permits |
||
} | ||
}))); | ||
|
||
// limit to previous 100 results or whatever is the max that GraphQL can return in a single shot | ||
// check if they have already been claimed, if so, discard | ||
// if unclaimed permits are found, import into the UI (we should already support multiple permits to be loaded in the UI) | ||
Comment on lines
+52
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Logic isn't here |
||
} |
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,68 @@ | ||
import { createClient } from "@supabase/supabase-js"; | ||
|
||
declare const SUPABASE_URL: string; | ||
declare const SUPABASE_ANON_KEY: string; | ||
const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY); | ||
|
||
async function gitHubLoginButtonHandler(scopes = "public_repo read:org") { | ||
const redirectTo = window.location.href; | ||
const { error } = await supabase.auth.signInWithOAuth({ | ||
provider: "github", | ||
options: { | ||
scopes, | ||
redirectTo, | ||
}, | ||
}); | ||
if (error) { | ||
console.log(error); | ||
// renderErrorInModal(error, "Error logging in"); | ||
} | ||
} | ||
|
||
async function checkSupabaseSession() { | ||
const { | ||
data: { session }, | ||
} = await supabase.auth.getSession(); | ||
|
||
return session; | ||
} | ||
|
||
const augmentAccessButton = document.createElement("button"); | ||
export function renderAugmentAccessButton() { | ||
augmentAccessButton.id = "augment-access-button"; | ||
augmentAccessButton.innerHTML = `<span title="Allow access to private repositories"><svg viewBox="0 0 24 24" class="svg-icon"><path d="M12 17c1.1 0 2-.9 2-2s-.9-2-2-2-2 .9-2 2 .9 2 2 2m6-9h-1V6c0-2.76-2.24-5-5-5S7 3.24 7 6h1.9c0-1.71 1.39-3.1 3.1-3.1 1.71 0 3.1 1.39 3.1 3.1v2H6c-1.1 0-2 .9-2 2v10c0 1.1.9 2 2 2h12c1.1 0 2-.9 2-2V10c0-1.1-.9-2-2-2m0 12H6V10h12z"></path></svg><span/>`; | ||
augmentAccessButton.addEventListener("click", () => gitHubLoginButtonHandler("repo read:org")); | ||
return augmentAccessButton; | ||
} | ||
|
||
const gitHubLoginButton = document.createElement("button"); | ||
export const authenticationElement = document.getElementById("authentication") as HTMLDivElement; | ||
export function renderGitHubLoginButton() { | ||
gitHubLoginButton.id = "github-login-button"; | ||
gitHubLoginButton.innerHTML = "<span>Login</span><span class='full'> With GitHub</span>"; | ||
gitHubLoginButton.addEventListener("click", () => gitHubLoginButtonHandler()); | ||
if (authenticationElement) { | ||
authenticationElement.appendChild(gitHubLoginButton); | ||
authenticationElement.classList.add("ready"); | ||
} | ||
} | ||
|
||
export async function getGitHubAccessToken(): Promise<string | null> { | ||
// better to use official function, looking up localstorage has flaws | ||
const oauthToken = await checkSupabaseSession(); | ||
|
||
const expiresAt = oauthToken?.expires_at; | ||
if (expiresAt) { | ||
if (expiresAt < Date.now() / 1000) { | ||
localStorage.removeItem(`sb-${"SUPABASE_STORAGE_KEY"}-auth-token`); | ||
return null; | ||
} | ||
} | ||
|
||
const accessToken = oauthToken?.provider_token; | ||
if (accessToken) { | ||
return accessToken; | ||
} | ||
|
||
return null; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks very wrong. Permits should load from GitHub not supabase