From 4667f1245aea9c72b5e6e299af9d029d8eb18162 Mon Sep 17 00:00:00 2001 From: koya0 Date: Wed, 16 Oct 2024 19:07:24 -0300 Subject: [PATCH] feat: fixed githubUserId and added providerToken checker --- functions/validators.ts | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/functions/validators.ts b/functions/validators.ts index 2018972a..fab19a68 100644 --- a/functions/validators.ts +++ b/functions/validators.ts @@ -1,18 +1,33 @@ import { CustomRequest } from "./types"; import { OAuthToken } from "../src/home/getters/get-github-access-token"; +import { Octokit } from "@octokit/rest"; export async function validatePOST(url: URL, request: CustomRequest): Promise { try { const jsonData: unknown = await request.json(); - const authToken = jsonData as OAuthToken; - const githubUserId = authToken?.user?.id; + const providerToken = authToken?.provider_token; + + if (providerToken) { + const octokit = new Octokit({ auth: providerToken }); + + try { + await octokit.request("GET /user"); + + const githubUserId = authToken?.user?.user_metadata?.provider_id; + + const key = url.searchParams.get("key"); - const key = url.searchParams.get("key"); + if (githubUserId && githubUserId === key) { + return true; + } - if (githubUserId && githubUserId == key) { - return true; + return false; + } catch (error) { + console.error("User is not logged in"); + return false; + } } return false; } catch (error) {