[英]How to auto redirect after authenticate using ember-simple-auth
I am using JWT (JSON Web Token) to authenticate user.我正在使用 JWT(JSON Web 令牌)来验证用户。 I do include the
ApplicationRouteMixin
in application.js
route and it supports to handle two events (success login and success logout) by default.我确实在
application.js
路由中包含了ApplicationRouteMixin
,它默认支持处理两个事件(成功登录和成功注销)。
Now when I login with a user credential using code below,现在,当我使用以下代码使用用户凭据登录时,
login(credential) {
const authenticator = 'authenticator:jwt';
const session = this.get('session');
session.authenticate(authenticator, credential)
.catch(() => {
const errorMessage = "Wrong email or password. Please try again!";
this.set('errorMessage', errorMessage);
});
},
The URL stay the same even the response is success and includes the valid JWT (see below).即使响应成功,URL 也保持不变,并且包含有效的 JWT(见下文)。
{"jwt":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE0ODg1NDIyMTcsInN1YiI6Mn0.Ma0ZtLWhoO1mb5cluGJZcKuHb-J2ZJdZOd3AFhVI258"}
When I refresh the page manually then the page get redirected.当我手动刷新页面时,页面会被重定向。
However, when I am in the protected route after authentication, the auto redirect is working for this.get('session').invalidate()
which kick me out of the protected route.但是,当我在身份验证后处于受保护的路由中时,自动重定向正在为
this.get('session').invalidate()
,它将我踢出受保护的路由。
I know one walkaround is to add a then
after authenticate
method (see code below) to redirect the URL to the right one when there is a success response from the method;我知道一种解决方法是添加一个
then
after authenticate
方法(请参见下面的代码),以便在该方法有成功响应时将 URL 重定向到正确的方法; however, after browse so many examples I see no one does something like below.然而,在浏览了这么多示例之后,我发现没有人做类似下面的事情。
session.authenticate(authenticator, credential)
.then(() => {
this.transitionTo('browse');
})
.catch(() => {
const errorMessage = "Wrong email or password. Please try again!";
this.set('errorMessage', errorMessage);
});
I am missing anything here?我在这里错过了什么? Why my route does not auto redirect after authentication?
为什么我的路由在身份验证后没有自动重定向?
This works for me:这对我有用:
this.get('session').authenticate('authenticator:jwt', username, password).then(
() => this.transitionTo("index"),
(err) => this.controller.set('error', err.error || err)
);
I found out a way on the internet!!!我在网上找到了方法!!! Hope you all enjoy it a lot!!!
希望你们都喜欢它! Here where I found: https://browntreelabs.com/redirection-in-ember-simple-auth/
在这里我找到了: https : //browntreelabs.com/redirection-in-ember-simple-auth/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.