Guest User

Untitled

a guest
Apr 11th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. <h2>Welcome back, {{session.user}}</h2>
  2.  
  3. authenticate: function(credentials) {
  4. var _this = this;
  5. return new Ember.RSVP.Promise(function(resolve, reject) {
  6. Ember.$.ajax({
  7. url: _this.tokenEndpoint,
  8. type: 'POST',
  9. data: JSON.stringify({username: credentials.identification, password: credentials.password }),
  10. contentType: 'application/json'
  11. }).then(function(response) {
  12. Ember.run(function() {
  13. resolve({
  14. token: response.token,
  15. username: credentials.identification
  16. });
  17. });
  18. }, function(xhr, status, error) {
  19. var response = JSON.parse(xhr.responseText);
  20. Ember.run(function() {
  21. reject(response.error);
  22. });
  23. });
  24. });
  25. },
  26.  
  27. Ember.Application.initializer({
  28. name: 'authentication',
  29. before: 'simple-auth',
  30. initialize: function(container, application) {
  31. // register the custom authenticator and authorizer so Ember Simple Auth can find them
  32. container.register('authenticator:custom', App.CustomAuthenticator);
  33. container.register('authorizer:custom', App.CustomAuthorizer);
  34. SimpleAuth.Session.reopen({
  35. user: function() {
  36. var username = this.get('username');
  37. if (!Ember.isEmpty(username)) {
  38. return container.lookup('store:main').find('user', {username: username});
  39. }
  40. }.property('username')
  41. });
  42. }
  43. });
  44.  
  45. App.CustomSession = SimpleAuth.Session.extend({
  46. account: function() {
  47. var accountId = this.get('account_id');
  48. if (!Ember.isEmpty(accountId)) {
  49. return this.container.lookup('store:main').find('account', accountId);
  50. }
  51. }.property('account_id')
  52. });
  53. container.register('session:custom', App.CustomSession);
  54. window.ENV['simple-auth'] = {
  55. session: 'session:custom',
  56. }
  57.  
  58. window.ENV['simple-auth'] = {
  59. authorizer: 'simple-auth-authorizer:devise',
  60. session: 'session:withCurrentUser'
  61. };
  62.  
  63. import Session from 'simple-auth/session';
  64.  
  65. var SessionWithCurrentUser = Session.extend({
  66. currentUser: function() {
  67. var userId = this.get('user_id');
  68. if (!Ember.isEmpty(userId)) {
  69. return this.container.lookup('store:main').find('user', userId);
  70. }
  71. }.property('user_id')
  72. });
  73.  
  74. export default {
  75. name: 'customize-session',
  76. initialize: function(container) {
  77. container.register('session:withCurrentUser', SessionWithCurrentUser);
  78. }
  79. };
Add Comment
Please, Sign In to add comment