Aurelia认证后如何return查看

How to return to view after authentication in Aurelia

我有一个视图可以通过电子邮件直接 link 访问。

例如。 http://myServer:7747/#/pics/ClientId/YYYY-MM-DD

所以这是使用路由设置的:

{ route: ['/pics', '/pics/:clientId/:sessionDate', 'pics'], 
  name: 'pics', moduleId: './views/pics', nav: false, title: 'Pictures', 
  auth: true, activationStrategy: activationStrategy.invokeLifecycle 
},

因此,如果客户端单击此 link 并且未登录,我希望视图重定向到登录屏幕(我正在使用 aurelia-authentication 插件),然后当它成功时,我想要使用相同的 urlParams return 到此页面。

我可以重定向到登录页面,但事实证明返回此视图很困难。如果我只是尝试使用 history.back() 问题是身份验证插件在我可以做任何事情之前将另一个 navigationInstruction (loginRedirect) 推送到历史记录中。如果我只是尝试硬编码 'go back twice' 导航,我 运行 就会遇到问题,因为当用户只是尝试从主页重新登录并且没有历史记录时。

看起来应该比实际更容易,我做错了什么?

我没有使用过 aurelia-authentication 插件,但我可以帮助您使用一种基本技术,让这一切变得非常简单。在您的 main.js 文件中,将应用程序的根设置为 "login" 组件。在登录组件中,当用户成功通过身份验证后,将应用程序的根设置为具有路由器视图的 "shell" 组件(或您选择的任何组件),并在其视图模型中配置路由器。一旦发生这种情况,路由器将根据 url 将用户带到正确的组件。如果用户注销,只需将应用程序根设置回 "login" 组件。

这里有一些粗略的代码试图传达这个想法。我假设您使用的是 SpoonX 插件,但这并不是必需的。只要在用户验证时重置应用程序的根目录,它就可以工作。

在main.js

.....
aurelia.start().then(() => aurelia.setRoot('login'));
.....

在login.js

import {AuthService} from 'aurelia-authentication';
import {Aurelia, inject} from 'aurelia-framework';

@inject(AuthService, Aurelia)
export class Login {
    constructor(authService, aurelia) {
        this.authService = authService;
        this.aurelia = aurelia;
    }

    login(credentialsObject) {
        return this.authService.login(credentialsObject)
            .then(() => {
                this.authenticated = this.authService.authenticated;

                if (this.authenticated) {
                    this.aurelia.setRoot('shell');
                }
            });
    }

    .....
}

在shell.html

.....
<router-view></router-view>
.....

在shell.js

.....
configureRouter(config, router) {
    this.router = router;
    config.map(YOUR ROUTES HERE);
}
.....

我通过将插件的 authenticateStep 替换为我自己的来实现此功能:

import { inject } from 'aurelia-dependency-injection';
import { Redirect } from 'aurelia-router';
import { AuthService } from "aurelia-authentication";
import { StateStore } from "./StateStore";

@inject(AuthService, StateStore)
export class SaveNavStep {
    authService: AuthService;
    commonState: StateStore;

    constructor(authService: AuthService, commonState: StateStore) {
        this.authService = authService;
        this.commonState = commonState;
    }

    run(routingContext, next) {
        const isLoggedIn = this.authService.authenticated;
        const loginRoute = this.authService.config.loginRoute;

        if (routingContext.getAllInstructions().some(route => route.config.auth === true)) {
            if (!isLoggedIn) {
                this.commonState.postLoginNavInstr = routingContext;
                return next.cancel(new Redirect(loginRoute));
            }
        } else if (isLoggedIn && routingContext.getAllInstructions().some(route => route.fragment === loginRoute)) {
            return next.cancel(new Redirect(this.authService.config.loginRedirect));
        }

        return next();
    }
}

我的和普通的唯一区别是我注入了一个 'StateStore' 对象,我在其中保存了需要身份验证的 NavigationInstruction。

然后在我的登录视图模型中,我注入了同一个 StateStore(单例)对象并执行类似这样的操作来登录:

login() {
    var redirectUri = '#/defaultRedirectUri';

    if (this.commonState.postLoginNavInstr) {
        redirectUri = this.routing.router.generate(this.commonState.postLoginNavInstr.config.name,
                            this.commonState.postLoginNavInstr.params,
                            { replace: true });
    }
    var credentials = {
        username: this.userName,
        password: this.password,
        grant_type: "password"
    };
    this.routing.auth.login(credentials,
        { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } },
        redirectUri
    ).catch(e => {
        this.dialogService.open({
            viewModel: InfoDialog,
            model: ExceptionHelpers.exceptionToString(e)
        });
    });
}; 

希望这对某人有所帮助!