刷新后重建路线

Rebuilding routes after refresh

我在 Aurelia 中工作时遇到了一些路由问题。

当用户访问我的应用程序时,如果他们之前经过身份验证,我想将他们重定向到登录页面。如果没有,直接进入登录页面。

我有经过身份验证的用户重定向工作正常(app.js -> login.js -> setupnav.js -> 着陆页)。

我现在遇到的问题是:

有人知道如何将用户从丢失的路由重定向到我的 login 页面吗?

也欢迎任何关于我如何设置路由的评论。

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");
        });
    };
}

重定向至:

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");
    }
}

要将未知路由映射到特定页面,请使用 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')。当用户单击 link/button 时,landing 页面将调用 setRoot('login') ].

以下是对相关问题的回答,可能会有帮助: