Add auto-authentication for new tabs opened from embedded mode

When opening flows/runs in new tabs from within the embedded Activepieces
iframe, users were being redirected to a login page because the JWT token
stored in sessionStorage wasn't shared across tabs.

Changes:
- Modify /authenticate route to accept standalone token parameter
- Update useNewWindow hook to pass JWT token via URL in embedded mode
- Add click handler in ApSidebarItem for authenticated new-window links

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
poduck
2025-12-29 21:19:52 -05:00
parent 76be5377d9
commit edc896b10e
3 changed files with 79 additions and 7 deletions

View File

@@ -2,15 +2,30 @@ import { useNavigate, useSearchParams } from 'react-router-dom';
import { useEmbedding } from '../components/embed-provider';
import { authenticationSession } from './authentication-session';
export const useNewWindow = () => {
const { embedState } = useEmbedding();
const navigate = useNavigate();
if (embedState.isEmbedded) {
return (route: string, searchParams?: string) =>
navigate({
pathname: route,
search: searchParams,
});
// In embedded mode, open new tab with authentication token
return (route: string, searchParams?: string) => {
const token = authenticationSession.getToken();
const fullRoute = `${route}${searchParams ? '?' + searchParams : ''}`;
if (token) {
// Pass token for auto-authentication in new tab
const encodedRedirect = encodeURIComponent(fullRoute);
const authUrl = `/authenticate?token=${encodeURIComponent(token)}&redirect=${encodedRedirect}`;
window.open(authUrl, '_blank', 'noopener');
} else {
// Fallback to in-iframe navigation if no token
navigate({
pathname: route,
search: searchParams,
});
}
};
} else {
return (route: string, searchParams?: string) =>
window.open(
@@ -21,6 +36,35 @@ export const useNewWindow = () => {
}
};
/**
* Opens a route in a new browser tab with automatic authentication.
* For embedded contexts where sessionStorage isn't shared across tabs,
* this passes the JWT token via URL for auto-login.
*/
export const useOpenInNewTab = () => {
const { embedState } = useEmbedding();
return (route: string, searchParams?: string) => {
const token = authenticationSession.getToken();
if (embedState.isEmbedded && token) {
// In embedded mode, pass token for auto-authentication in new tab
const encodedRedirect = encodeURIComponent(
`${route}${searchParams ? '?' + searchParams : ''}`,
);
const authUrl = `/authenticate?token=${encodeURIComponent(token)}&redirect=${encodedRedirect}`;
window.open(authUrl, '_blank', 'noopener');
} else {
// Non-embedded mode - token is already in localStorage
window.open(
`${route}${searchParams ? '?' + searchParams : ''}`,
'_blank',
'noopener noreferrer',
);
}
};
};
export const FROM_QUERY_PARAM = 'from';
/**State param is for oauth2 flow, it is used to redirect to the page after login*/
export const STATE_QUERY_PARAM = 'state';