我有一个可以通过电子邮件中的直接链接访问的视图。
前任。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
},
因此,如果客户端单击此链接但未登录,我希望视图重定向到登录屏幕(我正在使用 aurelia-authentication 插件),然后当它成功时,我希望它使用相同的方法返回到此页面网址参数。
我已重定向到登录页面,但事实证明返回此视图很困难。如果我只是尝试使用history.back()
,问题是身份验证插件navigationInstruction (loginRedirect)
在我可以做任何事情之前已经将另一个推入了历史记录。如果我只是尝试对“返回两次”导航进行硬编码,当用户只是尝试从主页重新登录并且没有历史记录时,我会遇到问题。
似乎这样应该比实际容易,我在做什么错呢?
我通过用我自己的替换插件的 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)
});
});
};
希望这可以帮助某人!
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句