Aurelia管道未按预期工作

时间:2016-10-03 00:51:47

标签: aurelia aurelia-router

我有一个Auth管道。

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

正如您所看到的,它会检查未经身份验证的用户是否尝试访问auth页面,然后将其重定向到登录页面。这有效。现在我试图实现相反的情况,如果用户登录并尝试访问非身份验证页面,我想将它们重定向到仪表板

run(navigationInstruction, next) {
    var isLoggedIn = this.auth.isAuthenticated()
    if (navigationInstruction.getAllInstructions().some(i => i.config.auth)) {
        if (!isLoggedIn) {
            return next.cancel(new Redirect('login'));
        }
    }  else {
       if (isLoggedIn) {
            return next.cancel(new Redirect('dashboard'));
        } 
    }
    return next();
}

现在这也有效,但它会导致我相信的错误。如果我尝试访问非授权页面而我未获得授权,则会重定向到登录页面。所以更具体的例子是我在登录页面上,我没有经过身份验证,我正在尝试进入注册页面,我只是被重定向到登录页面。现在我认为由于某种原因i.config.auth认为它是真的,但我做了一些调试,我意识到return next()实际上被点击它加载了寄存器模板,但随后它重定向回登录页面。 / p>

我还注意到,在if语句之外移动var isLoggedIn函数会发生同样的错误:

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

更新

这是我的路线

config.map([

  { route: '', redirect: 'login' },
  { route: 'login', moduleId: 'components/login/login', title: 'Login', name: 'login' },
  { route: 'register', moduleId: 'components/register/register', title: 'Register', name: 'register' },
  { route: 'dashboard', moduleId: 'components/dashboard/dashboard', title: 'Dashboard', auth: true, name: 'dashboard' },
]);

1 个答案:

答案 0 :(得分:0)

我建议使用Additional Route Data (settings)来存储自定义设置。这些附加数据将始终在管道功能中可用。

这是一个完整的演示:https://gist.run/?id=b70a094db52e0cc34fd7c44de550f046

  • Home Page是公开的,可供所有人使用
  • Dashboard需要身份验证
  • Login PageRegister Page仅供未经身份验证的用户使用
  • 使用Toggle Login按钮更改身份验证状态
  • 当没有身份验证导航到Dashboard时,重定向会发生到Login Page
  • 当使用身份验证导航到LoginRegister时,会重定向到Dashboard

app.js

的相关部分

<强>路线

config.map([
  {
    "route": ["", "home"],
    "name": "home",
    "moduleId": "home",
    "nav": true,
    "title": "Home"
  },
  {
    "route": "dashboard",
    "name": "private-page",
    "moduleId": "private-page",
    "nav": true,
    "title": "Dashboard Page",
    // this is going to be available within AuthorizeStep
    "settings": {
      "auth": true
    }
  },
  {
    "route": "login",
    "name": "login",
    "moduleId": "login",
    "nav": true,
    "title": "Login Page",
    "settings": {
      "publicOnly": true
    }
  },
  {
    "route": "register",
    "name": "register",
    "moduleId": "register",
    "nav": true,
    "title": "Register Page",
    "settings": {
      "publicOnly": true
    }
  }
]);

<强>管道

// substitute auth magic
static auth = {
    isAuthenticated: false
}

run(navigationInstruction, next) {
    let isLoggedIn = AuthorizeStep.auth.isAuthenticated;

    // currently active route config
    let currentRoute = navigationInstruction.config;

    // settings object will be preserved during navigation
    let loginRequired = currentRoute.settings && currentRoute.settings.auth === true;

    if (isLoggedIn === false && loginRequired === true) {
        return next.cancel(new Redirect('login'));
    }

    let publicOnly = currentRoute.settings && currentRoute.settings.publicOnly === true;
    if (isLoggedIn === true && publicOnly === true) {
        return next.cancel(new Redirect('dashboard'));
    }

    return next();
}