So I have read, but not found a solution to my problem. My question is as my title asks.
Here is my issue, I have created 2 components (NavbarComponent and HomepageComponent). NarbarComponent is nested in AppComponent and handles the authentication while HomepageComponent is a route enabled component and default page of the app.
What I want to do is be able to use an authentication service like Auth0 to authenticate a user by clicking the Login button in the nav-bar (NavbarComponent) and then render his/her profile on the homepage (HomepageComponent). Vice-versa, when I click the Logout button, the user's profile content should be removed.
I tried configure a few architecture, but to no avail. First, I tried NavbarComponent to HomepageComponent with the shared Service, but it only updates the HomepageComponent when the page is reloaded.
I tried using a shared service between the AppComponent and the NavbarComponent and then using @Input() to transmit from Parent to Child, HomepageComponent.
Then I tried @Output() and EventEmitter from NavbarComponent to AppComponent while running the shared Service between the AppComponent and HomepageComponent.
Any information or insight would greatly be appreciated!!
UPDATED
HomepageComponent
import { Component, OnInit } from '@angular/core'
import { Authenticated } from '../definitions/authenticated'
import { AuthCheckService } from '../shared/auth-check.service'
@Component({
selector: 'afn-homepage',
templateUrl: './homepage.component.html'
})
export class HomepageComponent implements OnInit{
private authenticated: Authenticated;
isAuthenticated: boolean;
profile: any;
constructor(public authCheckService: AuthCheckService) { }
ngOnInit() {
this.authCheckService.getAuthenticated().subscribe(authenticated => {
console.log('Receiving auth info in home component from navbar component');
this.authenticated = authenticated;
this.isAuthenticated = this.authenticated.isAuthenticated;
this.profile = this.authenticated.profile;
});
}
}
I tried making a plunker here. I wasn't able to get it to load, but it does have most of the code and a mock of AuthService.