From 0fe78bb193f68e3a82f2fb584336a1c95f1aa395 Mon Sep 17 00:00:00 2001 From: z3rOR0ne Date: Thu, 22 Aug 2024 02:07:25 -0700 Subject: [PATCH] :construction: Set up solidjs component of interest --- solidjs/GoogleOAuth2Client.tsx | 60 ++++++++++++++++++++++++++++++++++ solidjs/readme.md | 2 ++ 2 files changed, 62 insertions(+) create mode 100644 solidjs/GoogleOAuth2Client.tsx create mode 100644 solidjs/readme.md diff --git a/solidjs/GoogleOAuth2Client.tsx b/solidjs/GoogleOAuth2Client.tsx new file mode 100644 index 00000000..4fa09f2e --- /dev/null +++ b/solidjs/GoogleOAuth2Client.tsx @@ -0,0 +1,60 @@ +import { createSignal } from "solid-js"; +const googleClientId = import.meta.env.VITE_GOOGLE_OAUTH2_CLIENT_ID; +const googleRedirectUri = import.meta.env.VITE_GOOGLE_OAUTH2_REDIRECT_URI; + +const GoogleLogin = () => { + const [accessToken, setAccessToken] = createSignal(null); + + const oauthSignIn = () => { + const oauth2Endpoint = "https://accounts.google.com/o/oauth2/v2/auth"; + + const params = { + client_id: googleClientId, + redirect_uri: googleRedirectUri, + response_type: "token", + scope: "https://www.googleapis.com/auth/drive.metadata.readonly", + included_granted_scopes: "true", + state: "pass-through value", + }; + + const urlParams = new URLSearchParams(params).toString(); + const authUrl = `${oauth2Endpoint}?${urlParams}`; + + const authWindow = window.open(authUrl, "_blank", "width=500,height=600"); + + const checkAuthWindow = () => { + try { + const url = authWindow?.location.href; + if (url && url.includes("access_token")) { + const accessTokenMatch = url.match(/access_token=([^&]+)/); + const token = accessTokenMatch ? accessTokenMatch[1] : null; + + if (token) { + setAccessToken(token); + console.log("Access token :=>", token); + authWindow?.close(); + } + } + } catch (error) { + // Ignore cross-origin access errors while waiting for the user to sign in + } + }; + + const intervalId = setInterval(() => { + if (authWindow?.closed) { + clearInterval(intervalId); + } else { + checkAuthWindow(); + } + }, 500); + }; + + return ( +
+ + {accessToken() &&

Access Token: {accessToken()}

} +
+ ); +}; + +export default GoogleLogin; diff --git a/solidjs/readme.md b/solidjs/readme.md new file mode 100644 index 00000000..c16f6968 --- /dev/null +++ b/solidjs/readme.md @@ -0,0 +1,2 @@ +This subdirectory is just to hold random solidjs components that are of some +interest.