Guest User

Untitled

a guest
Oct 19th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. /adapters/application.js
  2.  
  3. import DRFAdapter from './drf';
  4. import DataAdapterMixin from 'ember-simple-auth/mixins/data-adapter-mixin';
  5. export default DRFAdapter.extend(DataAdapterMixin, {
  6. authorizer: 'authorizer:custom',
  7. namespace: 'api',
  8.  
  9. pathForType(){
  10. return '';
  11. },
  12. urlForCreateRecord(){
  13. return 'http://localhost:8000/api/';
  14. }
  15. });
  16.  
  17. /authenticators/jwt.js
  18.  
  19. import Ember from 'ember';
  20. import Base from 'ember-simple-auth/authenticators/base';
  21. import config from '../config/environment';
  22.  
  23. const { RSVP: { Promise }, $: { ajax }, run } = Ember;
  24. export default Base.extend({
  25.  
  26. tokenEndpoint: `http://localhost:8000/auth`,
  27.  
  28. restore(data) {
  29. return new Promise((resolve, reject) => {
  30. if (!Ember.isEmpty(data.token)) {
  31. resolve(data);
  32. } else {
  33. reject();
  34. }
  35. });
  36. },
  37.  
  38. authenticate(creds) {
  39. const { identification, password } = creds;
  40. const data = JSON.stringify({
  41. email: identification,
  42. password: password
  43. });
  44. const requestOptions = {
  45. url: this.tokenEndpoint,
  46. type: 'POST',
  47. data,
  48. contentType: 'application/json',
  49. dataType: 'json'
  50. };
  51. return new Promise((resolve, reject) => {
  52. ajax(requestOptions).then((response) => {
  53. const { jwt } = response;
  54. // Wrapping aync operation in Ember.run
  55. run(() => {
  56. resolve({
  57. token: jwt
  58. });
  59. });
  60. }, (error) => {
  61. // Wrapping aync operation in Ember.run
  62. run(() => {
  63. reject(error);
  64. });
  65. });
  66. });
  67. },
  68.  
  69. invalidate(data) {
  70. return Promise.resolve(data);
  71. }
  72. });
  73.  
  74. /login/controller.js
  75.  
  76. import Controller from '@ember/controller';
  77. import { inject } from '@ember/service';
  78.  
  79. export default Controller.extend({
  80. session: inject('session'),
  81. actions: {
  82.  
  83. authenticate: function(){
  84.  
  85. let credentials = this.getProperties('identification','password');
  86. let authenticator = 'authenticator:jwt';
  87. this.get('session').authenticate(authenticator, credentials).catch(()=>{
  88. this.set('errorMessage','Login Failed');
  89. });
  90. this.set('userData',credentials.identification);
  91.  
  92. }
  93. }
  94. });
  95.  
  96.  
  97. /application/controller.js
  98.  
  99.  
  100. import Controller from '@ember/controller';
  101. import { inject as service } from '@ember/service';
  102.  
  103. export default Controller.extend({
  104. session: service('session'),
  105. actions: {
  106. invalidateSession: function(){
  107. this.get('session').invalidate();
  108. }
  109. }
  110. });
  111.  
  112. /config /environment.js
  113. ......
  114. ENV['ember-simple-auth'] = {
  115. authorizer: 'authorizer:custom',
  116. // authenticationRoute: 'login',
  117. routeAfterAuthentication: '/profiles',
  118. //authorizationPrefix: 'JWT' - do not uncomment this line
  119. serverTokenEndpoint: '/auth',
  120. };
  121. ......
Add Comment
Please, Sign In to add comment