Fix: redirect to applications page from auth pages when the user is logged in (#2771)

This commit is contained in:
Rishabh Saxena 2021-02-03 13:29:00 +05:30 committed by GitHub
parent 7a9ec45901
commit 8ae86c34e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 1 deletions

View File

@ -0,0 +1,8 @@
describe("Check for redirects associated with auth pages", function() {
it("Should redirect away from auth pages if already logged in", function() {
const loginPageRoute = "/user/login";
cy.visit(loginPageRoute);
cy.wait(2000);
cy.location("pathname").should("not.equal", loginPageRoute);
});
});

View File

@ -287,6 +287,11 @@ Cypress.Commands.add("DeleteApp", (appName) => {
});
Cypress.Commands.add("LogintoApp", (uname, pword) => {
cy.window()
.its("store")
.invoke("dispatch", { type: "LOGOUT_USER_INIT" });
cy.wait("@postLogout");
cy.visit("/user/login");
cy.get(loginPage.username).should("be.visible");
cy.get(loginPage.username).type(uname);

View File

@ -10,6 +10,8 @@ import FooterLinks from "./FooterLinks";
import * as Sentry from "@sentry/react";
const SentryRoute = Sentry.withSentryRouting(Route);
import requiresAuthHOC from "./requiresAuthHOC";
export const UserAuth = () => {
const { path } = useRouteMatch();
const location = useLocation();
@ -40,4 +42,4 @@ export const UserAuth = () => {
);
};
export default UserAuth;
export default requiresAuthHOC(UserAuth);

View File

@ -0,0 +1,22 @@
import React from "react";
import { useSelector } from "react-redux";
import { Redirect } from "react-router-dom";
import { getCurrentUser } from "selectors/usersSelectors";
import { ANONYMOUS_USERNAME } from "constants/userConstants";
import { APPLICATIONS_URL } from "constants/routes";
const requiresAuthHOC = (Component: React.ComponentType) => {
const Wrapped = (props: any) => {
const user = useSelector(getCurrentUser);
if (user?.email && user?.email !== ANONYMOUS_USERNAME) {
return <Redirect to={APPLICATIONS_URL} />;
}
return <Component {...props} />;
};
return Wrapped;
};
export default requiresAuthHOC;