1

I'm having a bit of trouble getting some routing stuff working in Aurelia.

When a user goes to my app, if they have previously authenticated, I want to redirect them to a landing page. If not, direct to a login page.

I have the authenticated user redirect working fine (app.js -> login.js -> setupnav.js -> landing page).

The problem I have now is:

  • When a user refreshes a page (http://localhost:8088/aurelia-app/#/landing), the landing route doesn't exist anymore and an error is thrown in the console (ERROR [app-router] Error: Route not found: /landing(…)). I would like to direct the user to login if a route cannot be found.

Does anybody know how I can redirect a user from a missing route to my login page?

Also any comments on how I set the routing up is welcome.

app.js

import {inject} from 'aurelia-framework';
import {Router} from 'aurelia-router';
import {FetchConfig} from 'aurelia-auth';
import {AuthorizeStep} from 'aurelia-auth';
import {AuthService} from 'aurelia-auth';

@inject(Router,FetchConfig, AuthService )
export class App {

    constructor(router, fetchConfig, authService){
        this.router = router;
        this.fetchConfig = fetchConfig;
        this.auth = authService;
    }

    configureRouter(config, router){
        config.title = 'VDC Portal';
        config.addPipelineStep('authorize', AuthorizeStep); // Add a route filter to the authorize extensibility point.
        config.map([
          { route: ['','login'], name: 'login',      moduleId: './login',      nav: false, title:'Login' },
          { route: '', redirect: "login" },
          { route: 'setupnav', name: 'setupnav',      moduleId: './setupnav',   nav: false, title:'setupnav' , auth:true}

        ]);
        this.router = router;

    }

    activate(){
        this.fetchConfig.configure();
    }

    created(owningView: View, myView: View, router){
        /* Fails to redirect user
        if(this.auth.isAuthenticated()){
            console.log("App.js ConfigureRouter: User already authenticated..");
            this.router.navigate("setupnav");
        }
        */
    }
}

login.js

import {AuthService} from 'aurelia-auth';
import {inject} from 'aurelia-framework';
import {Router} from 'aurelia-router';
@inject(AuthService, Router)

export class Login{
    constructor(auth, router){
        this.auth = auth;
        this.router = router;

        if(this.auth.isAuthenticated()){
            console.log("Login.js ConfigureRouter: User already authenticated..");
            this.router.navigate("setupnav");
        }
    };

    heading = 'Login';

    email='';
    password='';
    login(){
        console.log("Login()...");

        return this.auth.login(this.email, this.password)
        .then(response=>{
            console.log("success logged");
            console.log(response);
        })
        .catch(err=>{
            console.log("login failure");
        });
    };
}

Redirecting to:

setupnav.js

import {Router} from 'aurelia-router';
import {inject} from 'aurelia-framework';

@inject(Router)
export class Setupnav{

    theRouter = null;

    constructor(router){
        console.log("build setupnav. router:" + this.theRouter);   
        this.theRouter = router
    };

    activate()
    {     
        this.theRouter.addRoute( { route: 'landing', name: 'landing', moduleId: 'landing', nav: true, title:'Integration Health' , auth:true});
        this.theRouter.addRoute( { route: 'tools', name: 'tools', moduleId: 'tools', nav: true, title:'Integration Tools' , auth:true});
        this.theRouter.refreshNavigation();
        this.theRouter.navigate("landing");
    }
}
4

1 回答 1

3

要将未知路由映射到特定页面,请使用以下mapUnknownRoutes功能:

configureRouter(config, router) {
  ...
  config.mapUnknownRoutes(instruction => {
    return 'login';
  });
}

也就是说,将所有与身份验证相关的逻辑排除在路由之外可能更容易,而是setRoot根据用户的身份验证状态来设置适当的根模块。

标准main.js如下所示:

main.js

export function configure(aurelia) {
  aurelia.use
    .standardConfiguration()
    .developmentLogging();

  aurelia.start().then(a => a.setRoot());
}

您可以将逻辑更改为如下所示:

main.js

export function configure(aurelia) {
  aurelia.use
    .standardConfiguration()
    .developmentLogging();

  aurelia.start().then(() => {
    if (userIsAuthenticated) {
      return aurelia.setRoot('app');
    }
    if (userPreviouslyAuthenticated) {
      return aurelia.setRoot('login');
    }
    return aurelia.setRoot('landing');
  });
}

在上面的示例中,该app模块是唯一可以配置路由的模块。该login模块将是一个登录页面,setRoot('app')一旦用户成功登录就会调用。当用户单击链接/按钮时,该landing页面将调用。setRoot('login')

这是对可能有帮助的相关问题的答案: https ://stackoverflow.com/a/33458652/725866

于 2016-01-26T18:07:07.257 回答