需要 Ember-Simple-Auth currentUser 示例帮助 [英] Ember-Simple-Auth currentUser example help required
问题描述
提供用于检索当前登录帐户的代码.我只是对这个例子有几个问题,因为我试图让它在 Ember-CLI 应用程序中工作.
SimpleAuth
正在示例中使用,但是我不知道它来自哪里,因为我正在尝试 import SimpleAuth from ...
但我不知道不知道从哪个文件导入它.
此外,我想知道来自服务器的响应现在是否需要返回 user_id 以及 access_token/refresh_token?
如果是,那 oauth 兼容吗?
我当前的代码
//app/initializers/oauth-custom.js从 'ember' 导入 Ember;从'front/authenticators/oauth-custom'导入OAuthCustomAuthenticator;从simple-auth/session"导入会话;导出默认{name: 'oauth-custom',之前:'简单身份验证',初始化:函数(容器){Session.reopen({当前用户:函数(){var user_id = this.get('user_id');如果 (!Ember.isEmpty(user_id)) {return container.lookup('store:main').find('user', user_id);}}.property('user_id')});容器.注册('oauth-custom:oauth2-password-grant',OAuthCustomAuthenticator);}};
身份验证很完美,只是我没有看到 ember 试图发出的任何调用/user/id.
来自服务器的响应示例:
<代码>{"access_token": "asdf","token_type": "承载",过期":1406082157,expires_in":604800,"refresh_token": "asdf",用户 ID":1"}
SimpleAuth 全局仅在库的浏览器分发中定义.当您使用 ember-cli 时,您会导入单个组件而不是通过全局访问它们(例如参见此示例:http://log.simplabs.com/post/90339547725/using-ember-simple-auth-with-ember-cli).>
例如获得您喜欢的会话:
从'simple-auth/session'导入会话;Session.reopen({…});
上面链接的示例要求服务器在响应中包含 user_id
,这不符合 OAuth 2.0 规范而是自定义.如果您希望/需要合规,您可以获取当前用户,例如通过 GET/me
或某事.就像会话通过身份验证一样.
So I realize that the account example here: https://github.com/simplabs/ember-simple-auth/blob/8863c032fcea6148a5b3365be5d66dc2389d301d/examples/4-authenticated-account.html
Provides the code for retrieving the currently logged in account. I just have a few questions about the example as I am trying to get it working in an Ember-CLI app.
SimpleAuth
is being used in the example however, I don't know where it's coming from as I am trying to import SimpleAuth from ...
but I don't know what file to import it from.
Furthermore, I am wondering if the response from the server now needs to return the user_id as well with the access_token/refresh_token?
If so, is that oauth compatible?
EDIT: My current code
// 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
);
}
};
Authentication works out perfectly, it's just that I don't see any call /user/id that ember tries to make.
Sample response from the server:
{
"access_token": "asdf",
"token_type": "bearer",
"expires": 1406082157,
"expires_in": 604800,
"refresh_token": "asdf",
"user_id": "1"
}
The SimpleAuth global is only defined in the browserified distribution of the library. When you're using ember-cli you import the individual components instead of accessing them via the global (see e.g. this example: http://log.simplabs.com/post/90339547725/using-ember-simple-auth-with-ember-cli).
To e.g. get the session you'd do sty like:
import Session from 'simple-auth/session';
Session.reopen({
…
});
The example linked above requires the server to include the user_id
in the response which is not compliant with the OAuth 2.0 specification but a customization. If you want/need to be compliant you could get the current user e.g. via a GET /me
or sth. like that once the session is authenticated.
这篇关于需要 Ember-Simple-Auth currentUser 示例帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!