Guest User

Untitled

a guest
Mar 10th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. condition: boolean;
  2.  
  3.  
  4. ngOnInit() {
  5. const isAuthenticated = this.authService.isAuthenticated().subscribe(
  6. (isAuthenticated) => {
  7. if (isAuthenticated && this.condition || !isAuthenticated && !this.condition ) {
  8. this.viewContainer.createEmbeddedView(this.templateRef);
  9. } else {
  10. this.viewContainer.clear();
  11. }
  12. }
  13. );
  14.  
  15. }
  16.  
  17.  
  18. @Input() set showAuthed(condition: boolean) {
  19. this.condition = condition;
  20. }
  21.  
  22. }
  23.  
  24. **Part of header template: showAuthed - directive **
  25.  
  26. <div class="menu">
  27.  
  28.  
  29.  
  30. <div class="logo-place">
  31. <img routerLink="/" class="logo" src="../assets/logo-colored.svg" width="150px" height="28px"> </div>
  32.  
  33. <div class="unauth" *showAuthed="false">
  34.  
  35.  
  36. <div *ngIf="routeIsActive('/register') || routeIsActive('/signup-outs') || routeIsActive('/signup-client')" class="login"
  37. routerLink="/signin" routerLinkActive="active" [routerLinkActiveOptions]="{exact: true}" style="cursor: pointer;">
  38. Sign in
  39. </div>
  40.  
  41. <div *ngIf="routeIsActive('/signin')" class="register" routerLink="/register" routerLinkActive="active" style="cursor: pointer;">
  42. Register
  43. </div>
  44.  
  45. </div>
  46.  
  47. <!-- Show this for logged in users -->
  48. <div class="auth" *showAuthed="true" >
  49.  
  50. var cognitoUser = userPool.getCurrentUser();
  51.  
  52.  
  53.  
  54. @Injectable()
  55. export class AuthService {
  56. authIsLoading = new BehaviorSubject<boolean>(false);
  57. authDidFail = new BehaviorSubject<boolean>(false);
  58. authStatusChanged = new Subject<boolean>();
  59. registeredUser: CognitoUser;
  60.  
  61. constructor(private router: Router) {
  62. }
  63.  
  64. // USER SIGN-IN
  65.  
  66.  
  67. signIn(username: string, password: string): void {
  68. this.authIsLoading.next(true);
  69. const authData = {
  70. Username: username,
  71. Password: password
  72. };
  73. const authDetails = new AuthenticationDetails(authData);
  74. const userData = {
  75. Username: username,
  76. Pool: userPool
  77. };
  78. const cognitoUser = new CognitoUser(userData);
  79. const that = this;
  80. cognitoUser.authenticateUser(authDetails, {
  81. onSuccess (result: CognitoUserSession) {
  82. that.authDidFail.next(false);
  83. that.authIsLoading.next(false);
  84. that.authStatusChanged.next(true);
  85. that.router.navigateByUrl('/profile', { skipLocationChange: false })
  86.  
  87.  
  88. },
  89. onFailure(err) {
  90. that.authDidFail.next(true);
  91. that.authIsLoading.next(false);
  92. console.log(err);
  93.  
  94. }
  95. });
  96.  
  97. this.authStatusChanged.next(true); // create user with cognito data
  98.  
  99.  
  100. }
  101.  
  102.  
  103. getAuthenticatedUser() {
  104. return userPool.getCurrentUser()
  105. }
  106.  
  107.  
  108. logout() {
  109.  
  110. this.authStatusChanged.next(false);
  111. this.getAuthenticatedUser().signOut();
  112. this.router.navigateByUrl('/signin')
  113. }
  114.  
  115. isAuthenticated(): Observable<boolean> {
  116. const user = this.getAuthenticatedUser();
  117. const obs = Observable.create((observer) => {
  118. if (!user) {
  119. observer.next(false);
  120. } else {
  121. user.getSession((err, session) => {
  122. if (err) {
  123. observer.next(false);
  124. } else {
  125. if (session.isValid()) {
  126. observer.next(true);
  127. } else {
  128. observer.next(false);
  129. }
  130. }
  131. });
  132. }
  133. observer.complete();
  134. });
  135. return obs;
  136. }
  137.  
  138. initAuth() {
  139. this.isAuthenticated().subscribe(
  140. (auth) => this.authStatusChanged.next(auth)
  141.  
  142. );
  143. }
  144.  
  145.  
  146. }
  147.  
  148. export class HeaderComponent implements OnInit {
  149.  
  150. showSignup: boolean;
  151. currentUrl : string;
  152. isAuthenticated: boolean;
  153.  
  154.  
  155.  
  156. constructor (
  157. private authGuard: AuthGuard,
  158. private authService: AuthService,
  159. private router: Router,
  160.  
  161.  
  162. ){}
  163.  
  164. ngOnInit() {
  165.  
  166. this.authService.isAuthenticated().subscribe(
  167. (authenticated) => {
  168. if (authenticated) {
  169. this.isAuthenticated = true;
  170. } else {
  171. this.isAuthenticated = false;
  172. }
  173.  
  174. }
  175.  
  176. );
  177.  
  178. }
Add Comment
Please, Sign In to add comment