2014-07-23 16 views
5

Tôi đang cố gắng thiết lập chế độ ember-simple-auth với phần phụ trợ django-rest-framework, nhưng tôi đang gặp phải một số sự cố khi lưu người dùng vào phiên. Tôi có để có thể làm điều gì đó như thế này trong các mẫu của tôi:Cách lưu người dùng trong một phiên

<h2>Welcome back, {{session.user}}</h2> 

Vì vậy, sau một số hướng dẫn tôi tìm thấy, tôi đã có xác thực và ủy quyền làm việc để tôi có thể nhận được một thẻ hợp lệ và sử dụng là trong yêu cầu. Để có được người sử dụng trên phiên giao dịch, tôi đã sửa đổi App.CustomAuthenticator.authenticate để khi thẻ được trả về, tên người dùng cũng được lưu giữ đến phiên:

authenticate: function(credentials) { 
    var _this = this; 
    return new Ember.RSVP.Promise(function(resolve, reject) { 
     Ember.$.ajax({ 
      url: _this.tokenEndpoint, 
      type: 'POST', 
      data: JSON.stringify({username: credentials.identification, password: credentials.password }), 
      contentType: 'application/json' 
     }).then(function(response) { 
      Ember.run(function() { 
       resolve({ 
        token: response.token, 
        username: credentials.identification 
       }); 
      }); 
     }, function(xhr, status, error) { 
      var response = JSON.parse(xhr.responseText); 
      Ember.run(function() { 
       reject(response.error); 
      }); 
     }); 
    }); 
}, 

sau đó tôi sửa đổi Application.intializer để cung cấp cho session một tài sản user:

Ember.Application.initializer({ 
    name: 'authentication', 
    before: 'simple-auth', 
    initialize: function(container, application) { 
     // register the custom authenticator and authorizer so Ember Simple Auth can find them 
     container.register('authenticator:custom', App.CustomAuthenticator); 
     container.register('authorizer:custom', App.CustomAuthorizer); 
     SimpleAuth.Session.reopen({ 
      user: function() { 
       var username = this.get('username'); 
       if (!Ember.isEmpty(username)) { 
       return container.lookup('store:main').find('user', {username: username}); 
       } 
      }.property('username') 
     }); 
    } 
}); 

Tuy nhiên, khi {{session.user.username}} được hiển thị, nó chỉ là một chuỗi rỗng. Câu hỏi của tôi là:

  1. Đây có phải là cách tốt nhất để chỉ định người dùng cho phiên không? Nó có vẻ vụng về với tôi nhưng tôi không thể nhìn thấy bất cứ điều gì tốt hơn.
  2. Tôi giả định rằng chuỗi rỗng là vì Lời hứa được trả về thay vì đối tượng User, vậy làm cách nào để giải quyết?
+0

Hiện tại, đó là cách thực hiện. Với bản phát hành tiếp theo của Ember Simple Auth, việc này sẽ dễ dàng hơn khi bạn có thể chỉ định lớp phiên của riêng mình và không phải mở lại phiên hiện tại nữa. – marcoow

+0

Ok, vậy làm thế nào để tôi có được một 'Người dùng' thực tế? – aquavitae

+0

kiểm tra ví dụ trong repo thực hiện chính xác những gì bạn muốn: https://github.com/simplabs/ember-simple-auth/blob/master/examples/4-authenticated-account.html – marcoow

Trả lời

4

Với phiên bản mới nhất bây giờ bạn có thể chỉ định một lớp phiên tùy chỉnh mà không cần phải mở lại, thấy phát hành lưu ý ở đây: https://github.com/simplabs/ember-simple-auth/releases/tag/0.6.4. Đây là cách hoạt động:

App.CustomSession = SimpleAuth.Session.extend({ 
    account: function() { 
    var accountId = this.get('account_id'); 
    if (!Ember.isEmpty(accountId)) { 
     return this.container.lookup('store:main').find('account', accountId); 
    } 
    }.property('account_id') 
}); 
… 
container.register('session:custom', App.CustomSession); 
… 
window.ENV['simple-auth'] = { 
    session: 'session:custom', 
} 
14

Để tag tắt của phản ứng @ marcoow của, dưới đây là cách thực hiện nó trong Ember CLI:

index.html:

window.ENV['simple-auth'] = { 
    authorizer: 'simple-auth-authorizer:devise', 
    session: 'session:withCurrentUser' 
}; 

initializers/tùy chỉnh phiên .js:

import Session from 'simple-auth/session'; 

var SessionWithCurrentUser = Session.extend({ 
    currentUser: function() { 
    var userId = this.get('user_id'); 
    if (!Ember.isEmpty(userId)) { 
     return this.container.lookup('store:main').find('user', userId); 
    } 
    }.property('user_id') 
}); 

export default { 
    name: 'customize-session', 
    initialize: function(container) { 
    container.register('session:withCurrentUser', SessionWithCurrentUser); 
    } 
}; 
+0

Cảm ơn bạn đã cung cấp triển khai cuối cùng. Tôi đã có chính xác tình hình tương tự. Cảm ơn rất nhiều. –

+0

Điều này có thể giúp ngày hôm nay: http://stackoverflow.com/questions/30872684/ember-simple-auth-injecting-current-user-into-every-route – Toni

Các vấn đề liên quan