V
V
victor_stone2017-06-15 17:37:30
JavaScript
victor_stone, 2017-06-15 17:37:30

Angular4 + Nodejs: How to show content only to a certain part of users?

I have an idea: Show certain menu items in the SPA to persons that matter in the DB.
Like, if user1 has an agency field with a value of true, then the section in the menu is shown to him: for agencies.
Any ideas how best to implement this? I tried to implement it through a directive that is responsible for whether the user is authorized or not, then it came out crap.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
victor_stone, 2017-06-15
@victor_stone

Here is an example of directive content

import {
  Directive,
  Input,
  OnInit,
  TemplateRef,
  ViewContainerRef
} from '@angular/core';

import { UserService } from './services/user.service';

@Directive({ selector: '[showAuthed]' })
export class ShowAuthedDirective implements OnInit {
  constructor(
    private templateRef: TemplateRef<any>,
    private userService: UserService,
    private viewContainer: ViewContainerRef
  ) {}

  condition: boolean;

  ngOnInit() {
    this.userService.isAuthenticated.subscribe(
      (isAuthenticated) => {
        if (isAuthenticated && this.condition || !isAuthenticated && !this.condition) {
          this.viewContainer.createEmbeddedView(this.templateRef);
        } else {
          this.viewContainer.clear();
        }
      }
    )
  }

  @Input() set showAuthed(condition: boolean) {
    this.condition = condition;
  }

}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question