Skip to content

Commit 4bb39bd

Browse files
Replace direct sessionStorage access with shared service abstraction
1 parent d0a6605 commit 4bb39bd

File tree

5 files changed

+63
-8
lines changed

5 files changed

+63
-8
lines changed

nifi-frontend/src/main/frontend/apps/nifi/src/app/pages/login/state/access/access.effects.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { HttpErrorResponse } from '@angular/common/http';
2929
import { Store } from '@ngrx/store';
3030
import { NiFiState } from '../../../../state';
3131
import { resetLoginFailure } from './access.actions';
32+
import { StorageService } from '../../../../service/storage.service';
3233

3334
@Injectable()
3435
export class AccessEffects {
@@ -38,6 +39,7 @@ export class AccessEffects {
3839
private router = inject(Router);
3940
private dialog = inject(MatDialog);
4041
private errorHelper = inject(ErrorHelper);
42+
private storageService = inject(StorageService);
4143

4244
login$ = createEffect(() =>
4345
this.actions$.pipe(
@@ -59,9 +61,9 @@ export class AccessEffects {
5961
this.actions$.pipe(
6062
ofType(AccessActions.loginSuccess),
6163
tap(() => {
62-
const returnUrl = sessionStorage.getItem('returnUrl');
64+
const returnUrl = this.storageService.getReturnUrl();
6365
if (returnUrl) {
64-
sessionStorage.removeItem('returnUrl');
66+
this.storageService.removeReturnUrl();
6567
this.router.navigateByUrl(returnUrl);
6668
} else {
6769
this.router.navigate(['/']);

nifi-frontend/src/main/frontend/apps/nifi/src/app/service/client.service.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,22 @@
1717

1818
import { Injectable, inject } from '@angular/core';
1919
import { v4 as uuidv4 } from 'uuid';
20-
import { SessionStorageService } from '@nifi/shared';
20+
import { StorageService } from './storage.service';
2121

2222
@Injectable({
2323
providedIn: 'root'
2424
})
2525
export class Client {
26-
private sessionStorage = inject(SessionStorageService);
26+
private storageService = inject(StorageService);
2727

2828
private clientId: string;
2929

3030
constructor() {
31-
let clientId = this.sessionStorage.getItem<string>('clientId');
31+
let clientId = this.storageService.getClientId();
3232

3333
if (clientId === null) {
3434
clientId = uuidv4();
35-
this.sessionStorage.setItem('clientId', clientId);
35+
this.storageService.setClientId(clientId);
3636
}
3737

3838
this.clientId = clientId;

nifi-frontend/src/main/frontend/apps/nifi/src/app/service/guard/authentication.guard.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@ import { fullScreenError } from '../../state/error/error.actions';
2929
import { ErrorHelper } from '../error-helper.service';
3030
import { selectLoginConfiguration } from '../../state/login-configuration/login-configuration.selectors';
3131
import { loadLoginConfigurationSuccess } from '../../state/login-configuration/login-configuration.actions';
32+
import { StorageService } from '../storage.service';
3233

3334
export const authenticationGuard: CanMatchFn = () => {
3435
const authService: AuthService = inject(AuthService);
3536
const userService: CurrentUserService = inject(CurrentUserService);
3637
const errorHelper: ErrorHelper = inject(ErrorHelper);
3738
const store: Store<CurrentUserState> = inject(Store<CurrentUserState>);
3839
const router: Router = inject(Router);
40+
const storageService: StorageService = inject(StorageService);
3941

4042
const getAuthenticationConfig = store.select(selectLoginConfiguration).pipe(
4143
take(1),
@@ -81,7 +83,7 @@ export const authenticationGuard: CanMatchFn = () => {
8183
if (errorResponse.status === 401) {
8284
const currentUrl = router.url;
8385
if (currentUrl && currentUrl !== '/login') {
84-
sessionStorage.setItem('returnUrl', currentUrl);
86+
storageService.setReturnUrl(currentUrl);
8587
}
8688
}
8789

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import { Injectable, inject } from '@angular/core';
19+
import { SessionStorageService } from '@nifi/shared';
20+
21+
@Injectable({
22+
providedIn: 'root'
23+
})
24+
export class StorageService {
25+
private sessionStorageService = inject(SessionStorageService);
26+
27+
private static readonly RETURN_URL = 'returnUrl';
28+
private static readonly CLIENT_ID = 'clientId';
29+
30+
public setReturnUrl(url: string): void {
31+
this.sessionStorageService.setItem(StorageService.RETURN_URL, url);
32+
}
33+
34+
public getReturnUrl(): string | null {
35+
return this.sessionStorageService.getItem<string>(StorageService.RETURN_URL);
36+
}
37+
38+
public removeReturnUrl(): void {
39+
this.sessionStorageService.removeItem(StorageService.RETURN_URL);
40+
}
41+
42+
public getClientId(): string | null {
43+
return this.sessionStorageService.getItem<string>(StorageService.CLIENT_ID);
44+
}
45+
46+
public setClientId(clientId: string): void {
47+
this.sessionStorageService.setItem(StorageService.CLIENT_ID, clientId);
48+
}
49+
}

nifi-frontend/src/main/frontend/apps/nifi/src/app/state/current-user/current-user.effects.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { selectLogoutUri } from '../login-configuration/login-configuration.sele
2828
import { Router } from '@angular/router';
2929
import { AuthService } from '../../service/auth.service';
3030
import { HttpErrorResponse } from '@angular/common/http';
31+
import { StorageService } from '../../service/storage.service';
3132

3233
@Injectable()
3334
export class CurrentUserEffects {
@@ -37,6 +38,7 @@ export class CurrentUserEffects {
3738
private userService = inject(CurrentUserService);
3839
private authService = inject(AuthService);
3940
private errorHelper = inject(ErrorHelper);
41+
private storageService = inject(StorageService);
4042

4143
loadCurrentUser$ = createEffect(() =>
4244
this.actions$.pipe(
@@ -77,7 +79,7 @@ export class CurrentUserEffects {
7779
tap(() => {
7880
const currentUrl = window.location.hash.substring(1) || this.router.url;
7981
if (currentUrl && currentUrl !== '/login') {
80-
sessionStorage.setItem('returnUrl', currentUrl);
82+
this.storageService.setReturnUrl(currentUrl);
8183
}
8284
this.router.navigate(['/login']);
8385
})

0 commit comments

Comments
 (0)