Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
536 views
in Technique[技术] by (71.8m points)

authorization - How to prevent angular from briefly showing page when using RouteGuard?

We are currently using an angular route guard and Cognito Hosted UI to protect routes when the user is not authenticated. This works great, but I am trying to prevent the page from showing at all until the redirect/oauth flow is completed.

The way it is now: When url of the application is hit, the page shows for maybe 1/2 to 1 second and then redirects to hosted ui. User signs in and it redirects back to the page.

I am trying to prevent the page from showing for that brief 1/2 - 1 second so that nothing can be seen (just a blank white page) until the user is authorized.

Here's the code example that doesn't do the job:

  canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
    
    return new Promise<boolean>((resolve, reject) => {
        this.authenticationService.isAuthenticated()
        .catch((error: any) => {
            reject(error);
        })
        .then((result: boolean) => {
            if (!result) {
                window.location.href = this.authenticationService.getCognitoLoginUrl();
            }
            resolve(result);
        })
    });
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...