Skip to content

Commit

Permalink
Fix Slack auth (but not default)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdb-dd committed Apr 30, 2024
1 parent 98bb140 commit 4b35f9f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 52 deletions.
70 changes: 19 additions & 51 deletions apps/admin/src/auth/SlackAuth.tsx
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 1 addition & 1 deletion apps/admin/src/routes/Routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const MainRoutes = () => {
/>

{/* Route for the Slack authentication callback */}
<Route path="/auth" element={<SlackAuth />} />
<Route path="/auth/slack" element={<SlackAuth />} />

{/* Route for email authentication */}
<Route path="/auth/email" element={<EmailAuth />} />
Expand Down

0 comments on commit 4b35f9f

Please sign in to comment.