From 4b35f9f3134b049ddff7d479a6c2ff123bef332b Mon Sep 17 00:00:00 2001 From: "Benjamin D. Brodie" Date: Tue, 30 Apr 2024 19:29:42 +0200 Subject: [PATCH] Fix Slack auth (but not default) --- apps/admin/src/auth/SlackAuth.tsx | 70 +++++++++---------------------- apps/admin/src/routes/Routes.tsx | 2 +- 2 files changed, 20 insertions(+), 52 deletions(-) diff --git a/apps/admin/src/auth/SlackAuth.tsx b/apps/admin/src/auth/SlackAuth.tsx index b3e3fd1..070b93b 100644 --- a/apps/admin/src/auth/SlackAuth.tsx +++ b/apps/admin/src/auth/SlackAuth.tsx @@ -1,64 +1,32 @@ +import supabase from "../supabase/SupabaseClient"; import { useEffect } from "react"; import { useNavigate } from "react-router-dom"; import { isLoggedIn } from "./authUtils"; -// Replace 'YOUR_SLACK_CLIENT_ID' with your actual Slack Client ID -const SLACK_CLIENT_ID = "5978666457744.5949309775910"; -// Replace '/auth/callback' with your actual OAuth redirect URI path -const REDIRECT_URI = `https://ncxehajalbrwbicnzxmn.supabase.co/auth/v1/callback`; +async function signInWithSlack() { + const { data, error } = await supabase.auth.signInWithOAuth({ + provider: 'slack', + }) +} const SlackAuth = () => { const navigate = useNavigate(); - useEffect(() => { - const error = new URLSearchParams(window.location.search).get("error"); - if (error) { - console.error("Error during Slack OAuth flow:", error); - // Handle error or redirect to an error page - navigate("/error"); // Redirect to an error page or show an error message - return; - } - - // Check if the OAuth process has been initiated - const code = new URLSearchParams(window.location.search).get("code"); - - if (!code) { - if (!isLoggedIn()) { - const scope = "identity.basic"; // Define required scopes - const slackAuthUrl = `https://slack.com/oauth/v2/authorize?client_id=${SLACK_CLIENT_ID}&user_scope=${encodeURIComponent(scope)}&redirect_uri=${encodeURIComponent(REDIRECT_URI)}`; - console.log("Redirecting to Slack for authentication"); - window.location.href = slackAuthUrl; + // Define an async function inside the useEffect + const checkLoginAndSignIn = async () => { + const loggedIn = await isLoggedIn(); + if (!loggedIn) { + await signInWithSlack(); + } else { + navigate("/"); // Use navigate for navigation instead of window.location.href for better SPA behavior } - } else { - // Redirect to a server-side endpoint that handles the OAuth token exchange - fetch( - `/api/authenticate?code=${code}&redirectUri=${encodeURIComponent(REDIRECT_URI)}`, - ) - .then((response) => response.json()) - .then((data) => { - if (data.access_token) { - localStorage.setItem("authToken", data.access_token); // Store the access token using a generic key - console.log( - "User authenticated successfully, redirecting to home page", - ); - navigate("/"); - } else { - throw new Error("No access token returned from server"); - } - }) - .catch((error) => { - console.error( - "Error during Slack OAuth token exchange:", - error.message, - error.stack, - ); - // Handle error or redirect to an error page - navigate("/error"); // Redirect to an error page or show an error message - }); - } - }, [navigate]); + }; + + // Call the async function + checkLoginAndSignIn(); + }, [navigate]); // Add dependencies here if any return null; }; -export default SlackAuth; +export default SlackAuth; \ No newline at end of file diff --git a/apps/admin/src/routes/Routes.tsx b/apps/admin/src/routes/Routes.tsx index a5bf14a..7b3d7c5 100644 --- a/apps/admin/src/routes/Routes.tsx +++ b/apps/admin/src/routes/Routes.tsx @@ -34,7 +34,7 @@ const MainRoutes = () => { /> {/* Route for the Slack authentication callback */} - } /> + } /> {/* Route for email authentication */} } />