Skip to content

Commit

Permalink
refactor: added redirect to alert log if not logged in
Browse files Browse the repository at this point in the history
  • Loading branch information
mrrishimeena committed Oct 18, 2024
1 parent 818787b commit f6f5442
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 27 deletions.
9 changes: 8 additions & 1 deletion lib/web/src/actions/userActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,17 @@ export function getUserProfile (callback) {
if (error.response) {
if (error.response.status === 401 || error.response.status === 403) {
window.localStorage.removeItem('errsole-jwt-token');
const currentURL = window.location.href;
const logsPathRegex = /\/#\/logs\?errsole_log_id=[^&]+/;
const shouldRedirect = logsPathRegex.test(currentURL);
const encodedHash = encodeURIComponent(window.location.hash);
const { protocol, hostname, port, pathname } = window.location;
const path = `${protocol}//${hostname}${port ? ':' + port : ''}${pathname}`;
const loginUrl = shouldRedirect
? `${path}#/login?redirect=${encodedHash}`
: `${path}#/login`;
dispatch(resetUserProfileState());
window.location.href = path + '#/login';
window.location.href = loginUrl;
} else {
Notifications.showErrors(error);
}
Expand Down
1 change: 0 additions & 1 deletion lib/web/src/components/ConsoleLogs.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ class ConsoleLogs extends React.Component {
errsoleLogQueryTimestamp = qparams.get('timestamp');
if (errsoleLogQueryId || errsoleLogQueryTimestamp) window.sessionStorage.removeItem('errsole-filter-details');
}

this.state = {
currentConsoleLogs: ConsoleLogs || [],
consoleLogLoading: false,
Expand Down
40 changes: 15 additions & 25 deletions lib/web/src/components/Dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,33 +52,22 @@ class Dashboard extends React.Component {
});
}

// render () {
// const isUserLoggedIn = this.state.isUserLoggedIn;
// const loadingStatus = this.state.loadingStatus;
// const antIcon = <LoadingOutlined type='loading' style={{ fontSize: 40 }} spin />;

// return (
// <>
// {!isUserLoggedIn && loadingStatus && <Spin className='center-info' indicator={antIcon} />}
// {isUserLoggedIn && !loadingStatus &&
// <Layout>
// <Sidebar className='sidebar-section' />
// <Layout>
// <Header />
// {this.props.children}
// </Layout>
// </Layout>}
// {!isUserLoggedIn && !loadingStatus && <Navigate to='/login' />}

// </>
// );
// }
// }

render () {
const { isUserLoggedIn, loadingStatus } = this.state;
const antIcon = <LoadingOutlined style={{ fontSize: 40 }} spin />;

// User is not authenticated; redirect to login with the current URL as a query parameter
const currentUrl = window.location.href;
let encodedRedirect;
const isRedirectableLink = (url) => {
const logsPathRegex = /\/#\/logs\?errsole_log_id=[^&]+/;
return logsPathRegex.test(url);
};

if (isRedirectableLink(currentUrl)) {
encodedRedirect = encodeURIComponent(currentUrl);
}

return (
<>
{!isUserLoggedIn && loadingStatus && (
Expand All @@ -89,11 +78,12 @@ class Dashboard extends React.Component {
<Sidebar className='sidebar-section' />
<Layout>
<Header />
<Outlet /> {/* This is where UsefulLinks and other child routes will render */}
<Outlet />
</Layout>
</Layout>
)}
{!isUserLoggedIn && !loadingStatus && <Navigate to='/login' />}
{!isUserLoggedIn && !loadingStatus && !encodedRedirect && <Navigate to='/login' />}
{!isUserLoggedIn && !loadingStatus && encodedRedirect && <Navigate to={`/login?redirect=${encodedRedirect}`} />}
</>
);
}
Expand Down
21 changes: 21 additions & 0 deletions lib/web/src/components/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,34 @@ class Login extends React.Component {
self.setState({
isUserLoggedIn: true
});
// redirect if needed
self.checkRedirectOrNot();
} else {
message.error('Something went wrong. Please check email and password.');
}
}
});
}

checkRedirectOrNot () {
// Extract URL parameters from the hash fragment
const hashFragment = window.location.hash.split('?')[1];
const hashParams = new URLSearchParams(hashFragment);
const redirectUrl = hashParams.get('redirect') || null;
if (redirectUrl) {
// Decode the redirect URL
const decodedUrl = decodeURIComponent(redirectUrl);
// Validate that the decoded URL is a relative path starting with '#/'
const isRelativePath = decodedUrl.startsWith('#/');
// Additional check to ensure it doesn't contain protocol schemes
const hasProtocol = /^https?:\/\//i.test(decodedUrl);
if (isRelativePath && !hasProtocol) {
// Safe to redirect
window.location.href = decodedUrl;
}
}
}

render () {
const isUserLoggedIn = this.state.isUserLoggedIn;
const name = this.state.name || '';
Expand Down

0 comments on commit f6f5442

Please sign in to comment.