繁体   English   中英

Main(Aurelia)之前的登录页面

[英]Login page before main (Aurelia)

我想当用户来时检查他是否之前登录-如果不是->重定向到登录页面->如果他的电子邮件和密码还可以->重定向回到欢迎页面。

我遇到什么问题->服务与登录类一起正常工作,但欢迎类完全忽略了服务中的任何更改。

users.js

export class Users {
users = [
    { name: 'Lucian', email: 'lucian1992@zalando.de', password: 'lucian' },
    { name: 'Corki', email: 'corki2010@supplier.de', password: 'corki' },
    { name: 'Vayne', email: 'vaynii@zalando.de', password: 'vayne' }];

securedUser = {};
error = {};
usedOnce = 0;

check(checkUser) {
    this.usedOnce = 1;
    this.securedUser = this.users.filter((user) => user.email === checkUser.email
                                                && user.password ===   checkUser.password)[0];
    if (typeof this.securedUser === 'undefined'){
        this.error = { message: "No such kind of user or wrong password" };
    }
 }

}

login.js

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

@inject(Users, Router)
export class Login {
    constructor(userData, router) {
        this.router = router;
        this.users = userData.users;
        this.check = userData.check;
        this.securedUser = userData.securedUser;  // Changed after check function
        this.userError = userData.error;
    }
    checkUser = {};
    login() {
        this.check(this.checkUser);
        if (typeof this.userError === 'undefined') {
            alert(this.error.message);
        } else {
            this.router.navigate("")
        }
    }
}

welcome.js

import {inject} from 'aurelia-framework';
import {Users} from 'users-list';
import {Redirect} from 'aurelia-router';

@inject(Users)
export class Overview {
  constructor(userData) {
    this.usedOnce  = userData.usedOnce;
    this.securedUser = userData.securedUser;  // This object changed in login.js, but totally ignores in this class
  }
  heading = 'Welcome to the Article Manager Prototype!';

  canActivate() {
    if (this.securedUser) {
      return new Redirect('/login')
    }
  }
}

因此,secureUser在login.js中发生了技术问题,但在welcome.js中却被完全忽略了。 这是因为可以使用canActivate方法吗? 因为我也使用activate()-同样的问题。

将不胜感激任何帮助理解问题。

官方文档中有针对您问题的解决方案:

import {Redirect} from 'aurelia-router';

export class App {
  configureRouter(config) {
    config.title = 'Aurelia';
    config.addPipelineStep('authorize', AuthorizeStep);
    config.map([
      { route: ['welcome'],    name: 'welcome',       moduleId: 'welcome',      nav: true, title:'Welcome' },
      { route: 'flickr',       name: 'flickr',        moduleId: 'flickr',       nav: true, auth: true },
      { route: 'child-router', name: 'childRouter',   moduleId: 'child-router', nav: true, title:'Child Router' },
      { route: '', redirect: 'welcome' }
    ]);
  }
}

class AuthorizeStep {
  run(navigationInstruction, next) {
    if (navigationInstruction.getAllInstructions().some(i => i.config.auth)) {
      var isLoggedIn = /* insert magic here */false;
      if (!isLoggedIn) {
        return next.cancel(new Redirect('login'));
      }
    }

    return next();
  }
}

请注意config.addPipelineStep('authorize', AuthorizeStep); 与常规路由器配置逻辑相比。

来源: http : //aurelia.io/docs.html#/aurelia/framework/1.0.0-beta.1.2.2/doc/article/cheat-sheet/7 (您需要向下滚动至“自定义”部分导航管道”)

我尝试了一个自己的例子,也许我找到了你的问题。

您正在检查您的securedUser是否为null。 如果不为空,您将重新login

canActivate() {
    if (this.securedUser) { // change to !this.securedUser
        return new Redirect('/login')
    }
}

并且您将您的secureUser初始值设置为一个空对象:

securedUser = {};

因此,即使条件错误,您的第一个重定向也可以正常工作。 不要设置初始值,设置未定义或更改if条件并检查sercuredUser属性。

您的解决方案可能会起作用,但是如果必须授权50条路由,则必须使用canActivate hook 50次! 对于授权工作流程而言,这不是一个很好的解决方案。

这是一个更优雅的解决方案:

创建一个隔离的根组件,然后更新main.js文件,而不是使用“路由”进行登录:

//for example, imagine that your root component is located at src/app/app.js
//and your login component at src/login/login.js
//app.isLoggedIn() checks if the credentials/tokens are OK
aurelia.start().then(a => {
  let rootComponent = '' app.isLoggedIn() ? 'app/app' : 'login/login';
  a.setRoot(rootComponent, document.body);
});

现在,您必须在login / login.js根组件中配置路由器:

configureRouter(config, router) {
  config.title = 'YourTitle';
  config.map([
    { route: '', name: 'user-password', moduleId: './user-password', title: 'Sign In' }, //route for "username/password" screen
    { route: 'forgot-password', name: 'forgot-pwd', moduleId: './forgot-pwd', title: 'Forgot Password?' } //example of "forgot password" screen
  ]);
  config.mapUnknownRoutes(instruction => {
    //if an unknown route has been found,
    //a route from the app component, 'users/add' for example
    //redirect to sign-in component
    return './user-password';
  });

  this.router = router;
}

创建适当的方法来认证用户,然后将其重定向到app/app组件:

//import and inject { Aurelia } from 'aurelia-framework';
this.aurelia.setRoot('app/app');

现在,如果未经授权的用户访问“用户/添加”,它将被重定向到登录页面。 如果登录成功,将显示“用户/添加”页面。

希望这可以帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM