Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TELESTION-437: Seamless Login and Redirect Experience #403

Merged
merged 1 commit into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,18 @@ import {
} from '../../../user-data';
import { isUserDataUpToDate } from '../../../utils.ts';
import { TelestionOptions } from '../../model.ts';
import { setResumeAfterLogin } from '../login';

export function dashboardEditorLoader({ version }: TelestionOptions) {
return ({ params }: LoaderFunctionArgs) => {
if (!isLoggedIn()) {
if (params.dashboardId) {
setResumeAfterLogin(
generatePath('/dashboards/:dashboardId', {
dashboardId: params.dashboardId
})
);
}
return redirect('/login');
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
ActionFunctionArgs,
generatePath,
LoaderFunctionArgs,
redirect
} from 'react-router-dom';
Expand All @@ -10,10 +11,18 @@ import { getUserData, setUserData, UserData } from '../../../user-data';
import { isUserDataUpToDate } from '../../../utils.ts';

import { dashboardCreateAction } from '../dashboard.ts';
import { setResumeAfterLogin } from '../login';

export function dashboardPageLoader({ version }: TelestionOptions) {
return ({ params }: LoaderFunctionArgs) => {
if (!isLoggedIn()) {
if (params.dashboardId) {
setResumeAfterLogin(
generatePath('/dashboards/:dashboardId', {
dashboardId: params.dashboardId
})
);
}
return redirect('/login');
}

Expand Down
20 changes: 19 additions & 1 deletion frontend-react/src/lib/application/routes/login/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@ import { isLoggedIn, login, LoginError } from '../../../auth';
import { TelestionOptions } from '../../model.ts';
import { wait } from '../../../utils.ts';

let resumeAfterLogin: string | undefined = undefined;

/**
* Sets a URL that the login action redirects after the user has logged in.
* @param url - the url the login action redirects after the user login
*/
export function setResumeAfterLogin(url: string) {
resumeAfterLogin = url;
}

/**
* Resets the url the login action redirects after the user has logged in.
*/
export function resetResumeAfterLogin() {
resumeAfterLogin = undefined;
}

export function loginLoader({ defaultBackendUrl }: TelestionOptions) {
return () => {
if (isLoggedIn()) {
Expand Down Expand Up @@ -52,7 +69,8 @@ export function loginAction() {
login(natsUrl, username, password as string),
wait(500)
]);
return redirect('/');
console.log('Resume after login url:', resumeAfterLogin);
return redirect(resumeAfterLogin ?? '/');
} catch (err) {
console.error(err);
if (err instanceof LoginError) {
Expand Down
2 changes: 2 additions & 0 deletions frontend-react/src/lib/application/routes/logout.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { redirect } from 'react-router-dom';
import { isLoggedIn, logout } from '../../auth';
import { wait } from '../../utils.ts';
import { resetResumeAfterLogin } from './login';

export function logoutLoader() {
return async () => {
Expand All @@ -9,6 +10,7 @@ export function logoutLoader() {
}

await Promise.all([logout(), wait(500)]);
resetResumeAfterLogin();
return redirect('/login');
};
}
Loading