Skip to content

Commit 09364c6

Browse files
committed
Migrate to /loggedin-redirect after login
1 parent 3be7a0d commit 09364c6

File tree

6 files changed

+44
-10
lines changed

6 files changed

+44
-10
lines changed

src/clients/apiUrls.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ export const getKlageByIdUrl = (klageId: string | number): string =>
99

1010
export const getAddKlageUrl = (): string => `${Environment.REACT_APP_API_URL}/klager`;
1111

12-
export const getLoginserviceRedirectUrl = (params: string) =>
13-
`${Environment.REACT_APP_LOGINSERVICE_URL}?redirect=${Environment.REACT_APP_URL}/begrunnelse${params}`;
14-
1512
export const getVedleggUrl = (klageId: string | number, vedleggId: string) =>
1613
`${Environment.REACT_APP_API_URL}/klager/${klageId}/vedlegg/${vedleggId}`;
1714

src/components/form-landing/form-landing.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const FormLanding = () => {
3535
const [errorState, setErrorState] = useState<boolean>(false);
3636

3737
useEffect(() => {
38-
dispatch(checkAuth(location.search));
38+
dispatch(checkAuth());
3939

4040
const { klageId, tema, ytelse, saksnummer } = getResumeState(
4141
location.search,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React from 'react';
2+
import { Redirect } from 'react-router-dom';
3+
import { LOGGED_IN_REDIRECT_PATH_KEY } from '../../utils/login';
4+
5+
export const loggedInRedirect = () => <Redirect to={getRedirectPath(localStorage)} />;
6+
7+
function getRedirectPath(storage: Storage) {
8+
const redirectPath = storage.getItem(LOGGED_IN_REDIRECT_PATH_KEY);
9+
if (redirectPath !== null) {
10+
storage.removeItem(LOGGED_IN_REDIRECT_PATH_KEY);
11+
if (redirectPath.startsWith('/')) {
12+
return redirectPath;
13+
}
14+
}
15+
16+
return '/';
17+
}

src/store/actions.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { Dispatch } from 'react';
2-
import { getLoginserviceRedirectUrl, getUserDataUrl } from '../clients/apiUrls';
2+
import { getUserDataUrl } from '../clients/apiUrls';
33
import { Klage, KlageSkjema, klageSkjemaToKlage, klageSkjemaToKlageDraft } from '../types/klage';
44
import { postKlage, putKlage, getKlage } from '../services/klageService';
55
import { Vedlegg, VedleggFile } from '../types/vedlegg';
66
import { Bruker } from '../types/bruker';
77
import { logError } from '../utils/logger/frontendLogger';
88
import { StorageKey } from '../utils/get-resume-state';
99
import { AxiosError } from 'axios';
10+
import { login } from '../utils/login';
1011

1112
export type ActionTypes =
1213
| {
@@ -57,13 +58,13 @@ export type ActionTypes =
5758
value: string | null;
5859
};
5960

60-
export function checkAuth(search: string) {
61+
export function checkAuth() {
6162
return function (dispatch: Dispatch<ActionTypes>) {
6263
return fetch(getUserDataUrl(), {
6364
method: 'GET',
6465
credentials: 'include'
6566
})
66-
.then(response => sjekkAuth(response, search))
67+
.then(response => sjekkAuth(response))
6768
.then(sjekkHttpFeil)
6869
.then(response => response.json() as Promise<Bruker>)
6970
.then(bruker => {
@@ -162,9 +163,9 @@ export function clearStorageContent() {
162163
sessionStorage.removeItem(StorageKey.SAKSNUMMER);
163164
}
164165

165-
export function sjekkAuth(response: Response, params: string) {
166+
export function sjekkAuth(response: Response) {
166167
if (response.status === 401 || response.status === 403) {
167-
window.location.assign(getLoginserviceRedirectUrl(encodeURIComponent(decodeURI(params))));
168+
login();
168169
}
169170
return response;
170171
}

src/utils/login.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import Environment from './environment';
2+
3+
export const LOGGED_IN_REDIRECT_PATH_KEY = 'logged-in-redirect-path';
4+
5+
export function login(redirectAfter: string = current(window.location)) {
6+
localStorage.setItem(LOGGED_IN_REDIRECT_PATH_KEY, redirectAfter);
7+
window.location.replace(loginUrl);
8+
}
9+
10+
export const LOGGED_IN_PATH = '/loggedin-redirect';
11+
12+
const current = ({ pathname, search, hash }: Location) => pathname + search + hash;
13+
const loginUrl = `${Environment.REACT_APP_LOGINSERVICE_URL}?redirect=${Environment.REACT_APP_URL}${LOGGED_IN_PATH}`;

src/utils/routes.config.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { INNGANG_KATEGORIER, Kategori } from '../data/kategorier';
99
import RootWithQuery from '../pages/root-with-query/root-with-query';
1010
import InngangInnsendingPost from '../components/inngang/inngang-innsendingsvalg-post';
1111
import FormLanding from '../components/form-landing/form-landing';
12+
import { loggedInRedirect } from '../pages/loggedin-redirect/loggedin-redirect';
13+
import { LOGGED_IN_PATH } from './login';
1214

1315
export interface FormStep extends RouteProps {
1416
path: string;
@@ -39,6 +41,10 @@ interface Route extends RouteProps {
3941
}
4042

4143
export const routesPages: Route[] = [
44+
{
45+
path: LOGGED_IN_PATH,
46+
render: loggedInRedirect
47+
},
4248
{
4349
path: `/kvittering`,
4450
component: KvitteringPage
@@ -62,7 +68,7 @@ export const routesPages: Route[] = [
6268
}))
6369
),
6470
{
65-
path: `/`,
71+
path: '/',
6672
component: RootWithQuery
6773
},
6874
{

0 commit comments

Comments
 (0)