Skip to content

Commit

Permalink
Support redirection to a specific page
Browse files Browse the repository at this point in the history
Support redirection to a specific page after successful authentication.
For example, if the user requested `Update Campaigns` page in the url,
instead of going to `Devices` page after authentication, go to
`Update Campaigns`.

Signed-off-by: Emina Muminovic <emina.muminovic@secomind.com>
  • Loading branch information
Emina-M committed Sep 6, 2024
1 parent e782ab5 commit c4eba21
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [0.9.0-dev] - Unreleased
### Added
- Allow generating admin JWT using `gen-edgehog-jwt`.
- Support redirection to a specific page after successful authentication
### Changed
- Change logo and brand images with the latest brand revision.

Expand Down
5 changes: 4 additions & 1 deletion frontend/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
This file is part of Edgehog.
Copyright 2021-2023 SECO Mind Srl
Copyright 2021-2024 SECO Mind Srl
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -49,6 +49,7 @@ import UpdateCampaignCreate from "pages/UpdateCampaignCreate";
import UpdateCampaigns from "pages/UpdateCampaigns";
import Login from "pages/Login";
import Logout from "pages/Logout";
import AttemptLogin from "pages/AttemptLogin";

import { version, repository, bugs } from "../package.json";

Expand All @@ -58,11 +59,13 @@ type RouterRule = {
};

const publicRoutes: RouterRule[] = [
{ path: Route.auth, element: <AttemptLogin /> },
{ path: Route.login, element: <Login /> },
{ path: "*", element: <Navigate to={Route.login} /> },
];

const authenticatedRoutes: RouterRule[] = [
{ path: Route.auth, element: <AttemptLogin /> },
{ path: Route.devices, element: <Devices /> },
{ path: Route.devicesEdit, element: <Device /> },
{ path: Route.deviceGroups, element: <DeviceGroups /> },
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/Navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ enum Route {
updateCampaigns = "/update-campaigns",
updateCampaignsNew = "/update-campaigns/new",
updateCampaignsEdit = "/update-campaigns/:updateCampaignId",
auth = "/auth",
login = "/login",
logout = "/logout",
}
Expand Down Expand Up @@ -108,6 +109,7 @@ const generatePath = (route: ParametricRoute): string => {
case Route.updateCampaignsNew:
case Route.login:
case Route.logout:
case Route.auth:
return route.route;
}
};
Expand Down
45 changes: 45 additions & 0 deletions frontend/src/pages/AttemptLogin.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { useEffect } from "react";
import { Spinner } from "react-bootstrap";
import { useLocation, useNavigate } from "react-router-dom";

import { Route } from "Navigation";
import { AuthConfig, useAuth } from "../contexts/Auth";

const AttemptLogin = () => {
const auth = useAuth();
const { search } = useLocation();
const searchParams = new URLSearchParams(search);
const tenantSlug = searchParams.get("tenantSlug") || "";
const authToken = searchParams.get("authToken") || "";
const redirectTo = searchParams.get("redirectTo") || "";
const navigate = useNavigate();

useEffect(() => {
const authConfig: AuthConfig = {
tenantSlug: tenantSlug,
authToken: authToken,
};
auth
.login(authConfig, false)
.then((isValidLogin) => {
if (isValidLogin) {
navigate(redirectTo, { replace: true });
} else {
navigate(Route.login, { replace: true });
}
})
.catch(() => {
navigate(Route.login, { replace: true });
});
}, []);

return (
<div data-testid="app" className="d-flex vh-100 flex-column">
<div className="d-flex justify-content-center">
<Spinner />
</div>
</div>
);
};

export default AttemptLogin;

0 comments on commit c4eba21

Please sign in to comment.