The question about Angular CanActivate

export class AuthGuard implements CanActivate {

    constructor(private PassportService: PassportService, private CookieService: CookieService, private router: Router) {

    }

    canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
        console.log("yoyoyo");
        console.log(next);
        console.log(state);
        console.log("Need Login = " + next.data.auth);
        console.log("PassportService isLogin = " + this.PassportService.isLogin);
        console.log("PassportService userData = " + this.PassportService.userData);

        if (next.data.auth === true) {

            if (this.PassportService.isLogin === true) {

                return true;

            }

            if (this.PassportService.isLogin === false) {

                //this.PassportService.getUserData()
                this.http.get("/api/user/data")
                    .subscribe(r => {

                        if ( r.success === true ){

                            this.PassportService.userData = r.content;
                            this.PassportService.isLogin = true;
                            return true;

                        }
                        if ( r.success === false ){

                            this.router.navigateByUrl("/passport/login");

                        }

                    })

            }

        }
        if (next.data.auth === false) {

            return true;

        }

    }

}

Why return true; in subscribe has no effect

< hr >

know that subscribe cannot return something, but how to solve this


the Observable object is returned for asynchronous detection. Here

this.PassportService.getUserData()
                    .subscribe(r => {

                        if ( r.success === true ){

                            this.PassportService.userData = r.content;
                            this.PassportService.isLogin = true;
                            return true;

                        }
                        if ( r.success === false ){

                            this.router.navigateByUrl('/passport/login');

                        }

                    })

does not return data at the outermost layer, and cannot be received outside the return value inside the subscribe


canActivateChild(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {

        if ( this.PassportService.isLogin === false ) {

            return this.PassportService.getUserData()
                .pipe(
                    map((data: any) => {

                        if ( data.success === true ) {

                            this.PassportService.userData.portrait = data.content.portrait
                            this.PassportService.userData.nickname = data.content.nickname
                            this.PassportService.userData.email = data.content.email

                            this.PassportService.isLogin = true

                            return true

                        }

                        if ( data.success === false ) {

                            this.router.navigateByUrl('/passport/login')

                        }

                    })
                )

        } else {

            return true

        }

    }
Menu