-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
95 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { TokenGuard } from './token.guard'; | ||
|
||
describe('TokenGuard', () => { | ||
let guard: TokenGuard; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({}); | ||
guard = TestBed.inject(TokenGuard); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(guard).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import {Injectable} from '@angular/core'; | ||
import {ActivatedRouteSnapshot, CanActivate, Router} from '@angular/router'; | ||
import {map, Observable, tap} from 'rxjs'; | ||
import {Store} from "@ngrx/store"; | ||
import {AppState} from "../state/app.state"; | ||
import {selectTokenUser} from "../state/selectors/user.selector"; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class TokenGuard implements CanActivate { | ||
|
||
constructor(private _store: Store<AppState>, private _router: Router) { | ||
} | ||
|
||
canActivate(route: ActivatedRouteSnapshot): Observable<boolean> | boolean { | ||
return this._store.select(selectTokenUser).pipe( | ||
map(value => !!value), | ||
tap(hasToken => { | ||
if (!hasToken) { | ||
this._router.navigate(['/login']); | ||
} | ||
}) | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 16 additions & 2 deletions
18
src/presentation/app/modules/secure/components/header/header.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,24 @@ | ||
import {Component, ViewEncapsulation} from '@angular/core'; | ||
import {Component, OnInit, ViewEncapsulation} from '@angular/core'; | ||
import {Store} from "@ngrx/store"; | ||
import {AppState} from "../../../../state/app.state"; | ||
import {selectUserData} from "../../../../state/selectors/user.selector"; | ||
import {DataUser} from "../../../../state/reducers/user.reducer"; | ||
|
||
@Component({ | ||
selector: 'app-header', | ||
templateUrl: './header.component.html', | ||
encapsulation: ViewEncapsulation.None | ||
}) | ||
export class HeaderComponent { | ||
export class HeaderComponent implements OnInit { | ||
|
||
public userData!: DataUser | ||
|
||
constructor(private _store: Store<AppState>) { | ||
} | ||
|
||
ngOnInit(): void { | ||
this._store.select(selectUserData).subscribe(value => { | ||
this.userData = value; | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import {AppState} from "../app.state"; | ||
import {createSelector} from "@ngrx/store"; | ||
import {DataUser} from "../reducers/user.reducer"; | ||
|
||
export const selectUser = (state: AppState) => state.user.user | ||
export const selectToken = (state: AppState) => state.user.user.token | ||
|
||
export const selectUserData = createSelector( | ||
selectUser, | ||
(state: DataUser) => state | ||
) | ||
|
||
export const selectTokenUser = createSelector( | ||
selectToken, | ||
(state) => state | ||
) |