我希望能够根据URL中的确认令牌参数自动登录用户。在我的路径中,我向服务器发出AJAX请求,以验证令牌,并将用于登录的序列化Oauth2 JSON发回。
是否可以使用此令牌登录用户?
首先,用户进入一个URL,如下所示:
http://example.com/users/confirmation?confirmation_token=eisz6LMzmck55xHuqopF接下来,我的路由向服务器发送AJAX请求,服务器用Oauth2令牌应答。
下面是我当前的实现,试图使用身份验证器来恢复它。尽管在控制台中看到“我应该登录”,但它还是不起作用。我怀疑这是因为它不知道如何恢复会话。查看会议文件n,我看到了手动身份验证的公共方法,但没有从oauth令牌恢复的方法。
import Ember from 'ember';
import ajax from 'ic-ajax';
export default Ember.Route.extend({
model: function(params) {
var path = MyApp.API_NAMESPACE + '/confirmation?confirmation_token=' + params.confirmation_token;
var authenticator = this.container.lookup('simple-auth-authenticator:oauth2-password-grant');
return ajax(path).then(function(response) {
return authenticator.restore(response).then(function() {
console.log('I should be logged in');
});
}).catch(function(request) {
console.log(request);
});
}
});发布于 2014-08-29 19:27:07
我通过创建一个自定义身份验证器来解决这个问题,它本质上是从oauth2身份验证器继承的,只覆盖了authenticate方法。
首先,我在app/lib/confirmation-authenticator.js中创建了身份验证器。
import OAuth2Authenticator from 'simple-auth-oauth2/authenticators/oauth2';
import ajax from 'ic-ajax';
export default OAuth2Authenticator.extend({
authenticate: function(token) {
var path = MyApp.API_NAMESPACE + '/confirmation?confirmation_token=' + token;
return new Ember.RSVP.Promise(function(resolve, reject) {
ajax(path).then(function(response) {
resolve(response);
}).catch(function(request) {
reject(request.textStatus);
});
});
}
});然后在app/initializers/authentication的初始化器中注册身份验证器
import ConfirmationAuthenticator from 'my-app/lib/confirmation-authenticator';
export default {
name: 'authentication',
before: 'simple-auth',
initialize: function(container) {
container.register('simple-auth-authenticator:confirmation', ConfirmationAuthenticator);
window.ENV = window.ENV || {};
window.ENV['simple-auth'] = {
authorizer: 'simple-auth-authorizer:oauth2-bearer',
};
window.ENV['simple-auth-oauth2'] = {
serverTokenEndpoint: MyApp.API_NAMESPACE + '/oauth/token'
};
}
};最后我在app/routes/users/confirmation.js的路线
import Ember from 'ember';
export default Ember.Route.extend({
model: function(params) {
var token = params.confirmation_token;
return this.get('session').authenticate('simple-auth-authenticator:confirmation', token);
}
});https://stackoverflow.com/questions/25316510
复制相似问题