Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 38 additions & 21 deletions studio/src/components/Authwrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,61 @@ import { getSession } from '../actions/session';
import { login } from '../utils/zitadel';
import { addErrorNotification } from '../actions/notifications';
import { useLocation } from 'react-router-dom';

const AuthWrapper = ({ children }) => {
const dispatch = useDispatch();
const location = useLocation();

// List of public paths that don't require authentication
const publicPaths = [
'/auth/login',
'/auth/registration',
'/auth/login/recovery',
'/redirect',
'/auth/verify',
];

useEffect(() => {
checkAuthenticated();
}, [location.pathname]);

const isPublicPath = (path) => {
return publicPaths.some((publicPath) => path.includes(publicPath));
};

const checkAuthenticated = () => {
dispatch(getSession()).then((res) => {
if (!res.success) {
if (res.noToken) {
// If there's no token or authentication failed
if (res.noToken || !res.success) {
const currentURL = window.location.href;
const searchParams = new URLSearchParams(window.location.search);
const authRequest = searchParams.get('authRequest');
if (
(currentURL.includes('/auth/login') ||
currentURL.includes('/auth/registration') ||
currentURL.includes('/redirect') ||
currentURL.includes('/auth/verify') ||
currentURL.includes('/auth/login/recovery')) &&
authRequest
) {

// If current path is public and has authRequest, allow access
if (isPublicPath(currentURL) && authRequest) {
return;
}

window.localStorage.setItem('return_to', window.location.href);
login().then((d) => {
if (d.error) {
dispatch(
addErrorNotification({
message: d.error,
}),
);
return;
}
window.location.href = d.authorizeURL;
});
// Store the return URL for post-login redirect
if (!isPublicPath(location.pathname)) {
window.localStorage.setItem('return_to', window.location.href);
}

// Initiate login process
if (!isPublicPath(location.pathname)) {
login().then((d) => {
if (d.error) {
dispatch(
addErrorNotification({
message: d.error,
}),
);
return;
}
window.location.href = d.authorizeURL;
});
}
}
}
});
Expand Down