Using the Aurelia Redirect Class

I have added an authorizing pipeline step to my router. Everything works fine, but when I use the class Redirectto point the user to the login page, it takes the URL as an argument. I would rather pass the name of the route as if I were using Router.navigateToRoute(). Is it possible?

@inject(AuthService)
class AuthorizeStep {
    constructor (authService) {
        this.authService = authService;
    }

    run (navigationInstruction, next) {
        if (navigationInstruction.getAllInstructions().some(i => i.config.auth)) {
            if (!this.authService.isLoggedIn) {
                return next.cancel(new Redirect('url-to-login-page')); // Would prefer to use the name of route; 'login', instead.
            }
        }

        return next();
    }
}
+4
source share
1 answer

After some Googling, I found a method Router.generate()that takes the name of the router (and optional parameters) and returns the URL. Now I updated my authorization step as follows:

@inject(Router, AuthService)
class AuthorizeStep {
    constructor (router, authService) {
        this.router = router;
        this.authService = authService;
    }

    run (navigationInstruction, next) {
        if (navigationInstruction.getAllInstructions().some(i => i.config.auth)) {
            if (!this.authService.isLoggedIn) {
                return next.cancel(new Redirect(this.router.generate('login')));
            }
        }

        return next();
    }
}

Edit: after a few more searches, I found a class RedirectToRoute;

import { RedirectToRoute } from 'aurelia-router';

...

return next.cancel(new RedirectToRoute('login'));
+7

Source: https://habr.com/ru/post/1650460/


All Articles