简体   繁体   English

Main(Aurelia)之前的登录页面

[英]Login page before main (Aurelia)

I want when user coming check if he login before - if not -> redirect to login page -> if his email & password are okay -> redirect back to welcome page. 我想当用户来时检查他是否之前登录-如果不是->重定向到登录页面->如果他的电子邮件和密码还可以->重定向回到欢迎页面。

What problem I have -> service work properly with login class but welcome class totally ignore any changes in service. 我遇到什么问题->服务与登录类一起正常工作,但欢迎类完全忽略了服务中的任何更改。

Code

users.js 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 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 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')
    }
  }
}

So, tech issue that securedUser changed in login.js, but totally ignored in welcome.js. 因此,secureUser在login.js中发生了技术问题,但在welcome.js中却被完全忽略了。 Is this because of canActivate method? 这是因为可以使用canActivate方法吗? Because I also use activate() - the same problem. 因为我也使用activate()-同样的问题。

Will appreciate any help with understanding the issue. 将不胜感激任何帮助理解问题。

There is a solution for your problem in the official documentation: 官方文档中有针对您问题的解决方案:

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

Notice the addition of config.addPipelineStep('authorize', AuthorizeStep); 请注意config.addPipelineStep('authorize', AuthorizeStep); compared to the regular router config logic. 与常规路由器配置逻辑相比。

Source: http://aurelia.io/docs.html#/aurelia/framework/1.0.0-beta.1.2.2/doc/article/cheat-sheet/7 (you need to scroll down a bit to section "Customizing the Navigation Pipeline") 来源: http : //aurelia.io/docs.html#/aurelia/framework/1.0.0-beta.1.2.2/doc/article/cheat-sheet/7 (您需要向下滚动至“自定义”部分导航管道”)

I tried an own example with this and maybe i found your Problem. 我尝试了一个自己的例子,也许我找到了你的问题。

You are checking if your securedUser is null. 您正在检查您的securedUser是否为null。 If it is NOT null you will reditect to login 如果不为空,您将重新login

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

And you are setting your securedUser initial to an empty object: 并且您将您的secureUser初始值设置为一个空对象:

securedUser = {};

Because of that your first redirect was working even your if condition was wrong. 因此,即使条件错误,您的第一个重定向也可以正常工作。 Don´t set a initial value, set undefined or change your if condition and check on sercuredUser properties. 不要设置初始值,设置未定义或更改if条件并检查sercuredUser属性。

Your solution might work, but if you have 50 routes that must be authorised you would have to use canActivate hook 50 times! 您的解决方案可能会起作用,但是如果必须授权50条路由,则必须使用canActivate hook 50次! That is not an elegant solution for an authorising workflow. 对于授权工作流程而言,这不是一个很好的解决方案。

This is a more elegant solution: 这是一个更优雅的解决方案:

Instead of having a "route" to login, create an isolated root component and then update your main.js file: 创建一个隔离的根组件,然后更新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);
});

Now, in your login/login.js root component you have to configure the router: 现在,您必须在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;
}

Create appropriated methods to authenticate the user and then redirect it to the app/app component: 创建适当的方法来认证用户,然后将其重定向到app/app组件:

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

Now, if an unauthorised user access 'users/add', it will be redirected to the login page. 现在,如果未经授权的用户访问“用户/添加”,它将被重定向到登录页面。 If the login succeeds, the 'users/add' page will be shown. 如果登录成功,将显示“用户/添加”页面。

Hope this helps! 希望这可以帮助!

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

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