Skip to content

Commit 1e8496c

Browse files
committed
fix(sign-up): update auth state and routes guards
1 parent 731ef39 commit 1e8496c

15 files changed

Lines changed: 109 additions & 43 deletions

File tree

src/app/app.component.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core';
44
import { RouterOutlet } from '@angular/router';
55

66
import { GetCurrentUser } from '@core/store/user';
7+
import { InitializeAuth } from '@osf/features/auth/store';
78

89
import { FullScreenLoaderComponent, ToastComponent } from './shared/components';
910

@@ -15,9 +16,13 @@ import { FullScreenLoaderComponent, ToastComponent } from './shared/components';
1516
changeDetection: ChangeDetectionStrategy.OnPush,
1617
})
1718
export class AppComponent implements OnInit {
18-
actions = createDispatchMap({ getCurrentUser: GetCurrentUser });
19+
actions = createDispatchMap({
20+
getCurrentUser: GetCurrentUser,
21+
initializeAuth: InitializeAuth,
22+
});
1923

2024
ngOnInit(): void {
25+
this.actions.initializeAuth();
2126
this.actions.getCurrentUser();
2227
}
2328
}

src/app/app.routes.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ export const routes: Routes = [
6767
{
6868
path: 'collections',
6969
loadChildren: () => import('./features/collections/collections.routes').then((mod) => mod.collectionsRoutes),
70-
canActivate: [authGuard],
7170
},
7271
{
7372
path: 'meetings',

src/app/core/components/header/header.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<osf-breadcrumb />
33

44
<div class="header-dropdown-button ml-auto">
5-
@if (isAuthenticated) {
5+
@if (isAuthenticated()) {
66
<p-button
77
class="custom-dark-hover"
88
icon="fas fa-chevron-down"

src/app/core/components/header/header.component.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { select } from '@ngxs/store';
1+
import { createDispatchMap, select } from '@ngxs/store';
22

33
import { TranslatePipe } from '@ngx-translate/core';
44

@@ -10,7 +10,7 @@ import { Router } from '@angular/router';
1010

1111
import { NavigationService } from '@osf/core/services';
1212
import { UserSelectors } from '@osf/core/store/user';
13-
import { AuthService } from '@osf/features/auth/services';
13+
import { AuthSelectors, Logout } from '@osf/features/auth/store';
1414
import { LoaderService } from '@osf/shared/services';
1515

1616
import { BreadcrumbComponent } from '../breadcrumb/breadcrumb.component';
@@ -24,11 +24,12 @@ import { BreadcrumbComponent } from '../breadcrumb/breadcrumb.component';
2424
})
2525
export class HeaderComponent {
2626
currentUser = select(UserSelectors.getCurrentUser);
27+
isAuthenticated = select(AuthSelectors.isAuthenticated);
2728

2829
private readonly router = inject(Router);
29-
private readonly authService = inject(AuthService);
3030
private readonly loaderService = inject(LoaderService);
3131
private readonly navigationService = inject(NavigationService);
32+
private readonly actions = createDispatchMap({ logout: Logout });
3233

3334
items = [
3435
{
@@ -40,17 +41,12 @@ export class HeaderComponent {
4041
label: 'navigation.logOut',
4142
command: () => {
4243
this.loaderService.show();
43-
this.authService.logout();
44-
this.router.navigate(['/']);
45-
this.loaderService.hide();
44+
this.actions.logout();
45+
this.router.navigate(['/home']).then(() => window.location.reload());
4646
},
4747
},
4848
];
4949

50-
get isAuthenticated() {
51-
return this.authService.isAuthenticated();
52-
}
53-
5450
navigateToSignIn() {
5551
this.navigationService.navigateToSignIn();
5652
}

src/app/core/components/nav-menu/nav-menu.component.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1+
import { select } from '@ngxs/store';
2+
13
import { TranslatePipe } from '@ngx-translate/core';
24

35
import { MenuItem } from 'primeng/api';
46
import { PanelMenuModule } from 'primeng/panelmenu';
57

68
import { filter, map } from 'rxjs';
79

8-
import { Component, computed, effect, inject, output } from '@angular/core';
10+
import { Component, computed, inject, output } from '@angular/core';
911
import { toSignal } from '@angular/core/rxjs-interop';
1012
import { ActivatedRoute, NavigationEnd, Router, RouterLink, RouterLinkActive } from '@angular/router';
1113

1214
import { PROJECT_MENU_ITEMS, REGISTRATION_MENU_ITEMS } from '@core/constants';
1315
import { NavigationService } from '@core/services';
16+
import { AuthSelectors } from '@osf/features/auth/store';
1417
import { IconComponent } from '@osf/shared/components';
1518

1619
@Component({
@@ -26,14 +29,26 @@ export class NavMenuComponent {
2629
private readonly route = inject(ActivatedRoute);
2730
private readonly navigationService = inject(NavigationService);
2831

29-
protected menuItems = this.navigationService.getFilteredMenuItems();
32+
private readonly isAuthenticated = select(AuthSelectors.isAuthenticated);
33+
3034
protected readonly myProjectMenuItems = PROJECT_MENU_ITEMS;
3135
protected readonly registrationMenuItems = REGISTRATION_MENU_ITEMS;
3236

3337
protected readonly mainMenuItems = computed(() => {
34-
return this.isCollectionsRoute()
35-
? this.menuItems
36-
: this.menuItems.filter((item) => item.routerLink !== '/collections');
38+
const isAuthenticated = this.isAuthenticated();
39+
const menuItems = this.navigationService.getFilteredMenuItems(isAuthenticated);
40+
41+
if (this.isRegistryRouteDetails()) {
42+
menuItems.map((menuItem) => {
43+
if (menuItem.id === 'registries') {
44+
menuItem.expanded = true;
45+
return menuItem;
46+
}
47+
return menuItem;
48+
});
49+
}
50+
51+
return this.isCollectionsRoute() ? menuItems : menuItems.filter((item) => item.routerLink !== '/collections');
3752
});
3853

3954
protected readonly currentRoute = toSignal(
@@ -52,21 +67,6 @@ export class NavMenuComponent {
5267
protected readonly isRegistryRoute = computed(() => this.currentRoute().isRegistryRoute);
5368
protected readonly isRegistryRouteDetails = computed(() => this.currentRoute().isRegistryRouteDetails);
5469

55-
constructor() {
56-
effect(() => {
57-
const isRouteDetails = this.isRegistryRouteDetails();
58-
if (isRouteDetails) {
59-
this.menuItems = this.menuItems.map((menuItem) => {
60-
if (menuItem.id === 'registries') {
61-
menuItem.expanded = true;
62-
return menuItem;
63-
}
64-
return menuItem;
65-
});
66-
}
67-
});
68-
}
69-
7070
private getRouteInfo() {
7171
const urlSegments = this.router.url.split('/').filter((segment) => segment);
7272

src/app/core/interceptors/auth.interceptor.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ export const authInterceptor: HttpInterceptorFn = (
2727
'Content-Type': 'application/vnd.api+json',
2828
};
2929

30+
if (token) {
31+
headers['Authorization'] = `Bearer ${token}`;
32+
}
33+
3034
if (csrfToken) {
3135
headers['X-CSRFToken'] = csrfToken;
3236
}
Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { MenuItem } from 'primeng/api';
22

3-
import { inject, Injectable } from '@angular/core';
4-
5-
import { AuthService } from '@osf/features/auth/services';
3+
import { Injectable } from '@angular/core';
64

75
import { MENU_ITEMS } from '../constants';
86
import { filterMenuItemsByAuth } from '../helpers';
@@ -13,15 +11,12 @@ import { environment } from 'src/environments/environment';
1311
providedIn: 'root',
1412
})
1513
export class NavigationService {
16-
private readonly authService = inject(AuthService);
17-
1814
navigateToSignIn(): void {
1915
const loginUrl = `${environment.casUrl}/login?service=${environment.webUrl}/login`;
2016
window.location.href = loginUrl;
2117
}
2218

23-
getFilteredMenuItems(): MenuItem[] {
24-
const isAuthenticated = this.authService.isAuthenticated();
19+
getFilteredMenuItems(isAuthenticated: boolean): MenuItem[] {
2520
return filterMenuItemsByAuth(MENU_ITEMS, isAuthenticated);
2621
}
2722
}

src/app/features/auth/services/auth.service.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import { createDispatchMap } from '@ngxs/store';
2+
13
import { CookieService } from 'ngx-cookie-service';
24

35
import { inject, Injectable } from '@angular/core';
46

57
import { JsonApiService } from '@osf/core/services';
8+
import { SetAuthenticated } from '@osf/features/auth/store';
69

710
import { SignUpModel } from '../models';
811

@@ -14,14 +17,19 @@ import { environment } from 'src/environments/environment';
1417
export class AuthService {
1518
private readonly jsonApiService = inject(JsonApiService);
1619
private readonly cookieService = inject(CookieService);
20+
private readonly actions = createDispatchMap({ setAuthenticated: SetAuthenticated });
1721

1822
isAuthenticated(): boolean {
1923
const csrfToken = this.cookieService.get('api-csrf');
20-
return !!csrfToken;
24+
const authenticated = !!csrfToken;
25+
26+
this.actions.setAuthenticated(authenticated);
27+
return authenticated;
2128
}
2229

2330
logout(): void {
2431
this.cookieService.deleteAll();
32+
this.actions.setAuthenticated(false);
2533
}
2634

2735
register(payload: SignUpModel) {

src/app/features/auth/store/auth.actions.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,17 @@ export class ResetPassword {
2121
public newPassword: string
2222
) {}
2323
}
24+
25+
export class InitializeAuth {
26+
static readonly type = '[Auth] Initialize Auth';
27+
}
28+
29+
export class SetAuthenticated {
30+
static readonly type = '[Auth] Set Authenticated';
31+
32+
constructor(public isAuthenticated: boolean) {}
33+
}
34+
35+
export class Logout {
36+
static readonly type = '[Auth] Logout';
37+
}
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
1-
export class AuthSelectors {}
1+
import { Selector } from '@ngxs/store';
2+
3+
import { AuthStateModel } from './auth.model';
4+
import { AuthState } from './auth.state';
5+
6+
export class AuthSelectors {
7+
@Selector([AuthState])
8+
static isAuthenticated(state: AuthStateModel): boolean {
9+
return state.isAuthenticated;
10+
}
11+
}

0 commit comments

Comments
 (0)