Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- condition: boolean;
- ngOnInit() {
- const isAuthenticated = this.authService.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;
- }
- }
- **Part of header template: showAuthed - directive **
- <div class="menu">
- <div class="logo-place">
- <img routerLink="/" class="logo" src="../assets/logo-colored.svg" width="150px" height="28px"> </div>
- <div class="unauth" *showAuthed="false">
- <div *ngIf="routeIsActive('/register') || routeIsActive('/signup-outs') || routeIsActive('/signup-client')" class="login"
- routerLink="/signin" routerLinkActive="active" [routerLinkActiveOptions]="{exact: true}" style="cursor: pointer;">
- Sign in
- </div>
- <div *ngIf="routeIsActive('/signin')" class="register" routerLink="/register" routerLinkActive="active" style="cursor: pointer;">
- Register
- </div>
- </div>
- <!-- Show this for logged in users -->
- <div class="auth" *showAuthed="true" >
- var cognitoUser = userPool.getCurrentUser();
- @Injectable()
- export class AuthService {
- authIsLoading = new BehaviorSubject<boolean>(false);
- authDidFail = new BehaviorSubject<boolean>(false);
- authStatusChanged = new Subject<boolean>();
- registeredUser: CognitoUser;
- constructor(private router: Router) {
- }
- // USER SIGN-IN
- signIn(username: string, password: string): void {
- this.authIsLoading.next(true);
- const authData = {
- Username: username,
- Password: password
- };
- const authDetails = new AuthenticationDetails(authData);
- const userData = {
- Username: username,
- Pool: userPool
- };
- const cognitoUser = new CognitoUser(userData);
- const that = this;
- cognitoUser.authenticateUser(authDetails, {
- onSuccess (result: CognitoUserSession) {
- that.authDidFail.next(false);
- that.authIsLoading.next(false);
- that.authStatusChanged.next(true);
- that.router.navigateByUrl('/profile', { skipLocationChange: false })
- },
- onFailure(err) {
- that.authDidFail.next(true);
- that.authIsLoading.next(false);
- console.log(err);
- }
- });
- this.authStatusChanged.next(true); // create user with cognito data
- }
- getAuthenticatedUser() {
- return userPool.getCurrentUser()
- }
- logout() {
- this.authStatusChanged.next(false);
- this.getAuthenticatedUser().signOut();
- this.router.navigateByUrl('/signin')
- }
- isAuthenticated(): Observable<boolean> {
- const user = this.getAuthenticatedUser();
- const obs = Observable.create((observer) => {
- if (!user) {
- observer.next(false);
- } else {
- user.getSession((err, session) => {
- if (err) {
- observer.next(false);
- } else {
- if (session.isValid()) {
- observer.next(true);
- } else {
- observer.next(false);
- }
- }
- });
- }
- observer.complete();
- });
- return obs;
- }
- initAuth() {
- this.isAuthenticated().subscribe(
- (auth) => this.authStatusChanged.next(auth)
- );
- }
- }
- export class HeaderComponent implements OnInit {
- showSignup: boolean;
- currentUrl : string;
- isAuthenticated: boolean;
- constructor (
- private authGuard: AuthGuard,
- private authService: AuthService,
- private router: Router,
- ){}
- ngOnInit() {
- this.authService.isAuthenticated().subscribe(
- (authenticated) => {
- if (authenticated) {
- this.isAuthenticated = true;
- } else {
- this.isAuthenticated = false;
- }
- }
- );
- }
Add Comment
Please, Sign In to add comment