Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the Aurelia Redirect class

Tags:

aurelia

I've added an authorize pipeline step to my router. Everything works fine, but when I use the Redirect class to point the user to the login page, it takes a URL as its argument. I'd prefer to pass in the route name as I would if I were using Router.navigateToRoute(). Is this 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();
    }
}
like image 426
powerbuoy Avatar asked Mar 11 '23 12:03

powerbuoy


1 Answers

After some Googling I found the Router.generate() method which takes a router name (and optional params) and returns the URL. I've now updated my authorize step to the following:

@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 some more googling I found the RedirectToRoute class;

import { RedirectToRoute } from 'aurelia-router';

...

return next.cancel(new RedirectToRoute('login'));
like image 59
powerbuoy Avatar answered Mar 24 '23 18:03

powerbuoy