Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export class NavbarComponent implements OnInit {
- loggedUser: Object = {}
- isLogged: boolean = false
- imageUrl: string = ''
- constructor(private router: RouterModule, private route: Router, private authService: AuthService) {
- }
- ngOnInit() {
- this.imageUrl = env.amazon_images_s3
- const user = AuthService.getUser()
- if(user) {
- this.authService.userLoggedIn$.subscribe(data => {
- this.isLogged = true
- this.loggedUser = data
- })
- }
- }
- export class AuthService {
- private static CURRENT_USER = 'currentUser'
- private userLoggedIn = new Subject<any>()
- private userLoggedOut = new Subject<any>()
- userLoggedIn$ = this.userLoggedIn.asObservable()
- userLoggedOut$ = this.userLoggedOut.asObservable()
- constructor(private _apiService: ApiService) { }
- public isAuthenticated(): any {
- const user = JSON.parse(localStorage.getItem(AuthService.CURRENT_USER))
- if (user) {
- return user.token.access_token !== null;
- }
- return false
- }
- logIn(username: string, password: string) {
- return this._apiService.post('user/login', { username: username, password: password, auth_type: 'ADMIN' })
- .pipe(map((data) => {
- if (data.token_info) {
- localStorage.setItem(AuthService.CURRENT_USER, JSON.stringify({ "token": data.token_info, "user": data.user }));
- this.userLoggedIn.next(data.user)
- }
- return data;
- }))
- }
- <mat-toolbar class="nav-bar" *ngIf="isLogged">
- <div class="navbar-menu" *ngIf="route.url == '/map'">
- <i class="material-icons">
- menu
- </i>
- </div>
- </mat-toolbar>
Add Comment
Please, Sign In to add comment