I'm following usePods and using jwt authentication with Django-rest in backend and ember js in front end
I'm giving a portion of my code (authentication code) Please find a solution why routeAfterAuthentication is not working in the code
/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}`);
}
}
});
Aucun commentaire:
Enregistrer un commentaire