Skip to content
This repository was archived by the owner on Apr 5, 2024. It is now read-only.

Commit b700cd8

Browse files
committed
use 400 error page also when not logged in, and allow redirects
1 parent 6fec456 commit b700cd8

File tree

7 files changed

+98
-41
lines changed

7 files changed

+98
-41
lines changed

src/__tests__/__snapshots__/storybook.test.ts.snap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,7 @@ exports[`Storyshots Login default 1`] = `
623623
className="form-group"
624624
>
625625
<input
626+
autoComplete="username"
626627
autoFocus={true}
627628
className="form-control"
628629
id="formBasicUsername"
@@ -635,6 +636,7 @@ exports[`Storyshots Login default 1`] = `
635636
className="form-group"
636637
>
637638
<input
639+
autoComplete="current-password"
638640
className="form-control"
639641
id="formBasicPassword"
640642
onChange={[Function]}
@@ -754,6 +756,7 @@ exports[`Storyshots Login interactionArea 1`] = `
754756
className="form-group"
755757
>
756758
<input
759+
autoComplete="username"
757760
autoFocus={true}
758761
className="form-control"
759762
id="formBasicUsername"
@@ -766,6 +769,7 @@ exports[`Storyshots Login interactionArea 1`] = `
766769
className="form-group"
767770
>
768771
<input
772+
autoComplete="current-password"
769773
className="form-control"
770774
id="formBasicPassword"
771775
onChange={[Function]}
@@ -884,6 +888,7 @@ exports[`Storyshots Login onlyInput 1`] = `
884888
className="form-group"
885889
>
886890
<input
891+
autoComplete="username"
887892
autoFocus={true}
888893
className="form-control"
889894
id="formBasicUsername"
@@ -896,6 +901,7 @@ exports[`Storyshots Login onlyInput 1`] = `
896901
className="form-group"
897902
>
898903
<input
904+
autoComplete="current-password"
899905
className="form-control"
900906
id="formBasicPassword"
901907
onChange={[Function]}

src/components/App.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { ReactElement } from "react";
22
import "./App.css";
3-
import { BrowserRouter } from "react-router-dom";
3+
import { BrowserRouter, Route, Switch } from "react-router-dom";
44
import Router from "./Router/Router";
55
import PermanentAssets from "./basicElements/PermanentAssets";
66

@@ -18,6 +18,7 @@ import { SystemState } from "../background/redux/actions/sytemState";
1818
import { checkForCookie } from "../background/api/auth";
1919
import { FFLoading } from "./basicElements/Loading";
2020
import { CookieStatus } from "../background/redux/actions/tokenTypes";
21+
import Error400 from "./pages/errors/Error400";
2122

2223
// this takes the redux store and maps everything that is needed to the function props
2324
const mapState = (state: SystemState) => ({
@@ -67,7 +68,17 @@ function App(props: Props): ReactElement {
6768
);
6869
} else {
6970
console.log("[APP] showing login");
70-
return <Login />;
71+
return (
72+
<BrowserRouter>
73+
<Switch>
74+
<Route
75+
path={"/error"}
76+
component={() => <Error400 needsLogin />}
77+
/>
78+
<Route path={"*"} component={Login} />
79+
</Switch>
80+
</BrowserRouter>
81+
);
7182
}
7283
} else {
7384
if (props.tokens.checkedCookies === CookieStatus.NOT_STARTED) {

src/components/Router/Router.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,24 @@ import Login from "../basicElements/login/Login";
66
import Registration from "../pages/User/Registration";
77
import FileSystem, { filesBaseUrl } from "../pages/filesytem/Filesystem";
88
import Profile from "../pages/User/Profile";
9+
import Error400 from "../pages/errors/Error400";
910

1011
export default function Router(): ReactElement {
1112
return (
1213
<Switch>
1314
<Route path={"/login"} component={Login} />
1415
<Route exact path={"/"}>
15-
<Redirect to={filesBaseUrl}/>
16+
<Redirect to={filesBaseUrl} />
1617
</Route>
1718
<Route path={"/start"}>
1819
<Redirect to={"/"} />
1920
</Route>
20-
<Route path={"/health"} component={Health}/>
21+
<Route path={"/health"} component={Health} />
2122
<Route path={filesBaseUrl} component={FileSystem} />
2223
<Route path={"/registration"} component={Registration} />
2324
{<Route path={"/profile"} component={Profile} />}
25+
{<Route path={"/profile"} component={Profile} />}
26+
{<Route path={"/error"} component={Error400} />}
2427
<Route path={"*"} component={Error404} />
2528
</Switch>
2629
);

src/components/basicElements/login/Login.stories.tsx

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,50 +4,60 @@ import { BrowserRouter } from "react-router-dom";
44
import "../../../style/custom.scss";
55
import Login, { LoginHeader, LoginInput, LoginInteractionArea } from "./Login";
66
import { action } from "@storybook/addon-actions";
7+
import store from "../../../background/redux/store";
8+
import { Provider } from "react-redux";
79

810
storiesOf("Login", module)
911
.add("default", () => (
10-
<BrowserRouter>
11-
<Login />
12-
</BrowserRouter>
12+
<Provider store={store}>
13+
<BrowserRouter>
14+
<Login />
15+
</BrowserRouter>
16+
</Provider>
1317
))
1418

1519
.add("onlyHeader", () => (
16-
<BrowserRouter>
17-
<LoginHeader />
18-
</BrowserRouter>
20+
<Provider store={store}>
21+
<BrowserRouter>
22+
<LoginHeader />
23+
</BrowserRouter>
24+
</Provider>
1925
))
2026

2127
.add("onlyInput", () => (
22-
<BrowserRouter>
23-
<LoginInput
24-
handleSubmit={() => console.log("Clicked on submit")}
25-
username={""}
26-
setUsername={action("changed username")}
27-
password={""}
28-
setPassword={action("changed password")}
29-
isLoading={false}
30-
setIsLoading={action("is loading")}
31-
stayLoggedIn={false}
32-
setStayLoggedIn={action("changed stay logged in")}
33-
errorMessage={""}
34-
/>
35-
</BrowserRouter>
28+
<Provider store={store}>
29+
<BrowserRouter>
30+
<LoginInput
31+
handleSubmit={() => console.log("Clicked on submit")}
32+
username={""}
33+
setUsername={action("changed username")}
34+
password={""}
35+
setPassword={action("changed password")}
36+
isLoading={false}
37+
setIsLoading={action("is loading")}
38+
stayLoggedIn={false}
39+
setStayLoggedIn={action("changed stay logged in")}
40+
errorMessage={""}
41+
/>
42+
</BrowserRouter>
43+
</Provider>
3644
))
3745

3846
.add("interactionArea", () => (
39-
<BrowserRouter>
40-
<LoginInteractionArea
41-
handleSubmit={() => console.log("Clicked on submit")}
42-
username={""}
43-
setUsername={action("changed username")}
44-
password={""}
45-
setPassword={action("changed password")}
46-
isLoading={false}
47-
setIsLoading={action("is loading")}
48-
stayLoggedIn={false}
49-
setStayLoggedIn={action("changed stay logged in")}
50-
errorMessage={""}
51-
/>
52-
</BrowserRouter>
47+
<Provider store={store}>
48+
<BrowserRouter>
49+
<LoginInteractionArea
50+
handleSubmit={() => console.log("Clicked on submit")}
51+
username={""}
52+
setUsername={action("changed username")}
53+
password={""}
54+
setPassword={action("changed password")}
55+
isLoading={false}
56+
setIsLoading={action("is loading")}
57+
stayLoggedIn={false}
58+
setStayLoggedIn={action("changed stay logged in")}
59+
errorMessage={""}
60+
/>
61+
</BrowserRouter>
62+
</Provider>
5363
));

src/components/basicElements/login/Login.test.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ import { render } from "@testing-library/react";
22
import React from "react";
33
import Login from "./Login";
44
import { RouterWrapper } from "../../../dev/testUtils/RouterWrapper";
5+
import store from "../../../background/redux/store";
6+
import { Provider } from "react-redux";
57

68
test("renders Login page", () => {
79
const breadcrumb = render(
8-
<RouterWrapper>
9-
<Login />
10-
</RouterWrapper>
10+
<Provider store={store}>
11+
<RouterWrapper>
12+
<Login />
13+
</RouterWrapper>
14+
</Provider>
1115
);
1216

1317
expect(breadcrumb).toMatchSnapshot();

src/components/basicElements/login/Login.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ import {
1717
import { loginWithUsernameAndPassword } from "../../../background/api/auth";
1818

1919
import logo from "../../../assets/images/logos/logoWithWhiteBorder.png";
20+
import { useHistory, useLocation } from "react-router-dom";
21+
import { useSelector } from "react-redux";
22+
import { RootState } from "../../../background/redux/store";
2023

2124
export interface LoginInputInterface {
2225
handleSubmit: (event: FormEvent) => void;
@@ -38,6 +41,20 @@ function Login(): ReactElement {
3841
const [errorMessage, setErrorMessage] = useState<string>("");
3942
const [isLoading, setIsLoading] = useState<boolean>(false);
4043

44+
const location = useLocation();
45+
const urlSearchParams = new URLSearchParams(location.search);
46+
const dest = urlSearchParams.get("dest");
47+
const history = useHistory();
48+
49+
const tokens = useSelector((state: RootState) => state.tokens);
50+
51+
if (tokens.refreshToken && tokens.accessToken?.token) {
52+
if (dest) {
53+
history.push(decodeURIComponent(dest));
54+
} else {
55+
history.push("/");
56+
}
57+
}
4158
const handleSubmit = (event: FormEvent) => {
4259
event.preventDefault();
4360
setIsLoading(true);
@@ -142,6 +159,7 @@ export function LoginInput(props: LoginInputInterface) {
142159
<Form.Control
143160
autoFocus
144161
placeholder="Username"
162+
autoComplete="username"
145163
value={username}
146164
onChange={(event) =>
147165
setUsername(event.target.value)
@@ -154,6 +172,7 @@ export function LoginInput(props: LoginInputInterface) {
154172
type="password"
155173
placeholder="Password"
156174
value={password}
175+
autoComplete="current-password"
157176
onChange={(event) =>
158177
setPassword(event.target.value)
159178
}

src/components/basicElements/login/__snapshots__/Login.test.tsx.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Object {
5454
class="form-group"
5555
>
5656
<input
57+
autocomplete="username"
5758
class="form-control"
5859
id="formBasicUsername"
5960
placeholder="Username"
@@ -64,6 +65,7 @@ Object {
6465
class="form-group"
6566
>
6667
<input
68+
autocomplete="current-password"
6769
class="form-control"
6870
id="formBasicPassword"
6971
placeholder="Password"
@@ -181,6 +183,7 @@ Object {
181183
class="form-group"
182184
>
183185
<input
186+
autocomplete="username"
184187
class="form-control"
185188
id="formBasicUsername"
186189
placeholder="Username"
@@ -191,6 +194,7 @@ Object {
191194
class="form-group"
192195
>
193196
<input
197+
autocomplete="current-password"
194198
class="form-control"
195199
id="formBasicPassword"
196200
placeholder="Password"

0 commit comments

Comments
 (0)