我正在跟踪 usePods ,并在后端使用Django-rest和在前端使用ember js的 jwt身份验证
我要提供一部分代码(身份验证代码) 请找到解决方案,为什么 routeAfterAuthentication 在代码中不起作用
/app/login/controller.js
import Controller from '@ember/controller';
export default Controller.extend({
session: Ember.inject.service(),
actions: {
authenticate: function(){
let credentials = this.getProperties('identification','password');
let authenticator = 'authenticator:jwt';
this.get('session').authenticate(authenticator, credentials).catch((reason)=>{
this.set('errorMessage', reason.error || reason);
});
}
}
});
/config/environment.js
...
ENV['ember-simple-auth'] = {
authorizer: 'authorizer:custom',
routeAfterAuthentication: '/profile'
};
...
/app/authenticators/jwt.js
import Ember from 'ember';
import Base from 'ember-simple-auth/authenticators/base';
import config from '../config/environment';
const { RSVP: { Promise }, $: { ajax }, run } = Ember;
export default Base.extend({
tokenEndpoint: `http://localhost:8000/auth`,
restore(data) {
return new Promise((resolve, reject) => {
if (!Ember.isEmpty(data.token)) {
resolve(data);
} else {
reject();
}
});
},
authenticate(creds) {
const { identification, password } = creds;
const data = JSON.stringify({
email: identification,
password: password
});
const requestOptions = {
url: this.tokenEndpoint,
type: 'POST',
data,
contentType: 'application/json',
dataType: 'json'
};
return new Promise((resolve, reject) => {
ajax(requestOptions).then((response) => {
const { jwt } = response;
// Wrapping aync operation in Ember.run
run(() => {
resolve({
token: jwt
});
});
}, (error) => {
// Wrapping aync operation in Ember.run
run(() => {
reject(error);
});
});
});
},
invalidate(data) {
return Promise.resolve(data);
}
});
/app/authorizers/custom.js
import Base from 'ember-simple-auth/authorizers/base';
import { inject } from '@ember/service';
export default Base.extend({
session: inject('session'),
authorize(data, block) {
const { token } = data
if (this.get('session.isAuthenticated') && token) {
block('Authorization', `JWT ${token}`);
}
}
});