Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /adapters/application.js
- import DRFAdapter from './drf';
- import DataAdapterMixin from 'ember-simple-auth/mixins/data-adapter-mixin';
- export default DRFAdapter.extend(DataAdapterMixin, {
- authorizer: 'authorizer:custom',
- namespace: 'api',
- pathForType(){
- return '';
- },
- urlForCreateRecord(){
- return 'http://localhost:8000/api/';
- }
- });
- /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);
- }
- });
- /login/controller.js
- import Controller from '@ember/controller';
- import { inject } from '@ember/service';
- export default Controller.extend({
- session: inject('session'),
- actions: {
- authenticate: function(){
- let credentials = this.getProperties('identification','password');
- let authenticator = 'authenticator:jwt';
- this.get('session').authenticate(authenticator, credentials).catch(()=>{
- this.set('errorMessage','Login Failed');
- });
- this.set('userData',credentials.identification);
- }
- }
- });
- /application/controller.js
- import Controller from '@ember/controller';
- import { inject as service } from '@ember/service';
- export default Controller.extend({
- session: service('session'),
- actions: {
- invalidateSession: function(){
- this.get('session').invalidate();
- }
- }
- });
- /config /environment.js
- ......
- ENV['ember-simple-auth'] = {
- authorizer: 'authorizer:custom',
- // authenticationRoute: 'login',
- routeAfterAuthentication: '/profiles',
- //authorizationPrefix: 'JWT' - do not uncomment this line
- serverTokenEndpoint: '/auth',
- };
- ......
Add Comment
Please, Sign In to add comment