generated from ubiquity/ts-template
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: removed key and value parameters of POST request and added refe…
…rralCode to POST body
- Loading branch information
Showing
5 changed files
with
78 additions
and
62 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
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,37 +1,43 @@ | ||
import { CustomRequest } from "./types"; | ||
import { OAuthToken } from "../src/home/getters/get-github-access-token"; | ||
import { CustomRequest, POSTRequestBody, ValidationResult } from "./types"; | ||
import { Octokit } from "@octokit/rest"; | ||
|
||
export async function validatePOST(url: URL, request: CustomRequest): Promise<boolean> { | ||
export async function validatePOST(request: CustomRequest): Promise<ValidationResult> { | ||
try { | ||
const jsonData: unknown = await request.json(); | ||
const authToken = jsonData as OAuthToken; | ||
const jsonData: POSTRequestBody = await request.json(); | ||
|
||
const providerToken = authToken?.provider_token; | ||
const { authToken, referralCode } = jsonData; | ||
|
||
if (providerToken) { | ||
const octokit = new Octokit({ auth: providerToken }); | ||
if (!authToken || !referralCode) { | ||
console.error("Missing authToken or referralCode"); | ||
return { isValid: false }; | ||
} | ||
|
||
const providerToken = authToken.provider_token; | ||
|
||
try { | ||
await octokit.request("GET /user"); | ||
if (!providerToken) { | ||
console.error("Missing provider token"); | ||
return { isValid: false }; | ||
} | ||
|
||
const githubUserId = authToken?.user?.user_metadata?.provider_id; | ||
const octokit = new Octokit({ auth: providerToken }); | ||
|
||
const key = url.searchParams.get("key"); | ||
try { | ||
await octokit.request("GET /user"); | ||
|
||
if (githubUserId && githubUserId === key) { | ||
return true; | ||
} | ||
const githubUserId = authToken.user?.user_metadata?.provider_id; | ||
|
||
return false; | ||
} catch (error) { | ||
console.error("User is not logged in"); | ||
return false; | ||
if (githubUserId) { | ||
return { isValid: true, githubUserId: githubUserId, referralCode: referralCode }; | ||
} else { | ||
console.error("Missing GitHub user ID"); | ||
return { isValid: false }; | ||
} | ||
} catch (error) { | ||
console.error("User is not logged in"); | ||
return { isValid: false }; | ||
} | ||
return false; | ||
} catch (error) { | ||
console.error("Invalid JSON"); | ||
return false; | ||
return { isValid: false }; | ||
} | ||
} |
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