angular ngrx/store authguard user property undefined issue

174 Views Asked by At

I'm building an app in angular that is using ngrx/store for state management and it has a user object as part of the state.

User class

export class User {
    constructor (
        public email: string
    ) {}

    get is_signed_in(): boolean {
        return this.email !== '';
    }
}

AuthGuard:

export class AuthGuard implements CanActivate {
    private user$: Observable<User>;
    constructor(
        private service: UserService,
        private router: Router
    ) {
        this.user$ = service.user$;
    }

    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
        return Observable.create((observer: NextObserver<boolean>) => {
            this.user$.subscribe((user: User) => {
                console.log('authguard', user);
                console.log('authguard', user.email);
                console.log('user.is_signed_in', user.is_signed_in);
                if (!user.is_signed_in) {
                    console.log('redirecting to signin page');
                    this.router.navigate(
                        ['user', 'signin'],
                        {
                            queryParams: {
                                url: state.url
                            }
                        }
                    );
                }

                observer.next(user.is_signed_in);
                observer.complete();
            });
        });
    }
}

but within the authguard class the user.is_signed_in property is undefined. Why?

0

There are 0 best solutions below