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 Aug 15, 2024
1 parent e782ab5 commit 6939185
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
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
46 changes: 43 additions & 3 deletions 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 All @@ -18,12 +18,19 @@
SPDX-License-Identifier: Apache-2.0
*/

import { Navigate, useRoutes } from "react-router-dom";
import {
Navigate,
useLocation,
useNavigate,
useRoutes,
} from "react-router-dom";
import { useEffect, useState } from "react";

import Footer from "components/Footer";
import Sidebar from "components/Sidebar";
import Spinner from "components/Spinner";
import Topbar from "components/Topbar";
import { useAuth } from "contexts/Auth";
import { AuthConfig, useAuth } from "contexts/Auth";
import { Route } from "Navigation";
import Device from "pages/Device";
import Devices from "pages/Devices";
Expand Down Expand Up @@ -96,6 +103,39 @@ function App() {
const auth = useAuth();
const routes = auth.isAuthenticated ? authenticatedRoutes : publicRoutes;
const RouterElement = useRoutes(routes);
const { search } = useLocation();
const searchParams = new URLSearchParams(search);
const tenantSlug = searchParams.get("tenantSlug") || "";
const authToken = searchParams.get("authToken") || "";
const redirectTo = searchParams.get("redirectTo") || "";
const attemptLogin = !!(tenantSlug && authToken);
const [attemptingLogin, setAttemptingLogin] = useState(attemptLogin);
const navigate = useNavigate();

useEffect(() => {
if (attemptLogin) {
const authConfig: AuthConfig = {
tenantSlug: tenantSlug,
authToken: authToken,
};
auth.login(authConfig, false).then((isValidLogin) => {
setAttemptingLogin(false);
if (isValidLogin) {
navigate(redirectTo);
}
});
}
}, []);

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

return (
<div data-testid="app" className="d-flex vh-100 flex-column">
Expand Down

0 comments on commit 6939185

Please sign in to comment.