I'm attempting to implement authentication in Ember.js using JSON Web Tokens generated from Django REST Framework.
Django 2.0.13 Django REST Framework 3.9.4 Django REST Simple JWT 4.3.0
I followed the example from this repository: https://github.com/jpadilla/ember-simple-auth-token
I have plugins for DRF that render and parse according to the JSON API standard, disabling them doesn't have any effect.
# urls.py
from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView
path("api/token/", TokenObtainPairView.as_view(), name='token_obtain_pair'),
path('api/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
// config/environment.js
ENV['ember-simple-auth-token'] = {
refreshAccessTokens: true,
refreshLeeway: 300, // refresh 5 minutes (300 seconds) before expiration
serverTokenEndpoint: 'http://localhost:8000/api/token/',
serverTokenRefreshEndpoint: 'http://localhost:8000/api/token/refresh/',
tokenPropertyName: 'access',
};
// adapters/application.js
import DS from 'ember-data';
import TokenAuthorizerMixin from 'ember-simple-auth-token/mixins/token-authorizer';
export default DS.JSONAPIAdapter.extend(TokenAuthorizerMixin, {
host: 'http://localhost:8000',
namespace: 'api',
});
// controllers/u/login.js
import Controller from '@ember/controller';
import { inject } from '@ember/service';
export default Controller.extend({
session: inject('session'),
actions: {
authenticate: function() {
const credentials = this.getProperties('username', 'password');
const authenticator = 'authenticator:jwt';
this.get('session').authenticate(authenticator, credentials);
}
}
});
<!-- templates/u/login.hbs -->
<form >
<label for="username">Login</label>
<label for="password">Password</label>
<button type="submit">Login</button>
</form>
I can make a successful request using Postman to Django, and I get the JWT back.
Submitting the login form with correct credentials gives a console error: Uncaught (in promise) Error: Token is empty. Please check your backend response.
Aucun commentaire:
Enregistrer un commentaire