Route guard with asynchronous event

Asked

Viewed 33 times

0

I have a route guard that checks if the user is logged in from a token in the Torage locale that works very well. But he doesn’t expect any asynchronous events. I need to create a new route guard that checks if the user is an administrator. For this I make a query in the API that informs me the user situation.

canActivate(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
): Observable <boolean> | boolean {
    let admin;
    admin = new Promise((resolve, reject) => {
        this.adminService.isAdmin(userId).toPromise().then(
            res => {
                if (res.isAdmin) {
                    resolve();
                } else {
                    reject();
                }
            }
        );
    });
    return admin;
}

The return of the api is right, but the function does not wait for the answer so I can not make it work. Any idea to solve this problem?

1 answer

0


Browser other questions tagged

You are not signed in. Login or sign up in order to post.