1

所以我意识到这里的帐户示例:https ://github.com/simplabs/ember-simple-auth/blob/8863c032fcea6148a5b3365be5d66dc2389d301d/examples/4-authenticated-account.html

提供用于检索当前登录帐户的代码。当我试图让它在 Ember-CLI 应用程序中工作时,我对这个示例有几个问题。

SimpleAuth但是,在示例中使用了它,我不知道它是从哪里来的,import SimpleAuth from ...但我不知道从哪个文件导入它。

此外,我想知道服务器的响应现在是否需要返回 user_id 以及 access_token/refresh_token?

如果是这样,该oauth是否兼容?

编辑:我当前的代码

// app/initializers/oauth-custom.js
import Ember from 'ember';
import OAuthCustomAuthenticator from 'front/authenticators/oauth-custom';
import Session from 'simple-auth/session';


export default {
    name: 'oauth-custom',
    before: 'simple-auth',

    initialize: function(container) {
        Session.reopen({
            currentUser: function() {
                var user_id = this.get('user_id');
                if (!Ember.isEmpty(user_id)) {
                    return container.lookup('store:main').find('user', user_id);
                }
            }.property('user_id')
        });
        container.register(
            'oauth-custom:oauth2-password-grant',
            OAuthCustomAuthenticator
        );
    }
};

身份验证效果很好,只是我没有看到 ember 尝试进行的任何调用 /user/id。

来自服务器的示例响应:

{
    "access_token": "asdf",
    "token_type": "bearer",
    "expires": 1406082157,
    "expires_in": 604800,
    "refresh_token": "asdf",
    "user_id": "1"
}
4

1 回答 1

3

SimpleAuth 全局仅在库的浏览器化发行版中定义。当您使用 ember-cli 时,您会导入各个组件,而不是通过全局访问它们(例如,请参见此示例: http: //log.simplabs.com/post/90339547725/using-ember-simple-auth-with-余烬-cli )。

例如,获得你会做的会话:

import Session from 'simple-auth/session';

Session.reopen({
  …
});

上面链接的示例要求服务器user_id在响应中包含不符合 OAuth 2.0 规范但自定义的响应。如果您想要/需要合规,您可以通过 aGET /me或 sth 获取当前用户。一旦会话通过身份验证,就像那样。

于 2014-07-15T13:51:27.407 回答