gpt4 book ai didi

javascript - 实现自定义 ember-simple-auth 身份验证器

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:39:53 24 4
gpt4 key购买 nike

首先,我不是经验丰富的 JS 开发人员,所以请原谅我可能犯的明显错误。

我正在尝试实现自定义身份验证器以使用 Keycloak 对用户进行身份验证使用需要将 client_id 作为请求正文的一部分传递的 OAuth2 密码授予。

import OAuth2PasswordGrant from 'ember-simple-auth/authenticators/oauth2-password-grant';
export default OAuth2PasswordGrant.extend({
serverTokenEndpoint: 'http://localhost:8080/something/token',
makeRequest(url, data, headers = {}) {
data.client_id = 'my-app';
return this._super(url, data, headers);
}
});

我有一个 Controller 通过调用此操作来使用此 Authenticator:

actions: {
authenticate() {
let {username, password} = this.getProperties('username', 'password');
this.get('session').authenticate('authenticator:oauth2', username, password).then(() => {
// Do something
}).catch((response) => {
// Show error
});
}
}

这会导致 Firefox 挂起并给我一条无响应的脚本消息。

如果我从 makeRequest() 方法中删除 return ,我可以从浏览器调试器中看到对 Keycloak 的调用实际上正确地返回了包含我的对象 token 等。然而,ember 检查员显示了一些与 Unresolved promise 相关的错误。但我想那是因为我不再兑现 promise 。

我在这里做错了什么?

如何解决脚本无响应的问题?

还有其他方法可以实现我的目标吗?

编辑 1:这是我删除返回的时间

这是返回的实际对象:

{
"access_token":"eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJTRUNSd09fMlZWdGhxUVBUWnFxNHlqX0tKekxnOElSTjBrQkx5UTlacklrIn0.eyJqdGkiOiI1NDgzZDdkMi0zMDdhLTQyZjItYWUxZC0xYTZjMTZjOTM2ZjAiLCJleHAiOjE1MDgzMzE5MjAsIm5iZiI6MCwiaWF0IjoxNTA4MzMxNjIwLCJpc3MiOiJodHRwOi8vbG9jYWxob3N0OjgwODAvYXV0aC9yZWFsbXMvc2Z4LWl0cmFuc2Zlci13ZWItYWdlbnQiLCJhdWQiOiJhZ2VudC13ZWItYXBwIiwic3ViIjoiMzZiMWY4OW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":"bearer",
"not-before-policy":0,
"session_state":"40831eaa-4f2a-496e-945d-7beb17e46e44"
}

这是 ember inspector(Promises)显示的内容:

enter image description here

这是来自 Promise 的堆栈跟踪:

Ember Inspector ($E):  authenticate/<@http://localhost:4200/assets/vendor.js:77927:9
initializePromise@http://localhost:4200/assets/vendor.js:63591:7
Promise@http://localhost:4200/assets/vendor.js:64067:35
authenticate@http://localhost:4200/assets/vendor.js:77919:14
authenticate@http://localhost:4200/assets/vendor.js:78528:14
authenticate@http://localhost:4200/assets/vendor.js:79420:14
authenticate@http://localhost:4200/assets/sfx-itransfer-web-agent.js:855:9
join@http://localhost:4200/assets/vendor.js:20249:24
run$1.join@http://localhost:4200/assets/vendor.js:37657:12
makeClosureAction/</<@http://localhost:4200/assets/vendor.js:29073:16
exports.flaggedInstrument@http://localhost:4200/assets/vendor.js:37087:14
makeClosureAction/<@http://localhost:4200/assets/vendor.js:29072:15
submit/<@http://localhost:4200/assets/vendor.js:70453:20
tryCatch@http://localhost:4200/assets/vendor.js:63549:14
invokeCallback@http://localhost:4200/assets/vendor.js:63562:15
publish@http://localhost:4200/assets/vendor.js:63532:9
@http://localhost:4200/assets/vendor.js:54458:16
invoke@http://localhost:4200/assets/vendor.js:19948:17
flush@http://localhost:4200/assets/vendor.js:19827:25
flush@http://localhost:4200/assets/vendor.js:20019:25
end@http://localhost:4200/assets/vendor.js:20128:26
run@http://localhost:4200/assets/vendor.js:20212:21
join@http://localhost:4200/assets/vendor.js:20219:24
run$1.join@http://localhost:4200/assets/vendor.js:37657:12
handleEvent/<@http://localhost:4200/assets/vendor.js:58233:18
exports.flaggedInstrument@http://localhost:4200/assets/vendor.js:37087:14
handleEvent@http://localhost:4200/assets/vendor.js:58232:17
_Mixin$create.handleEvent@http://localhost:4200/assets/vendor.js:57385:12
_bubbleEvent@http://localhost:4200/assets/vendor.js:57685:14
setupHandler/<@http://localhost:4200/assets/vendor.js:57619:20
dispatch@http://localhost:4200/assets/vendor.js:5546:16
add/elemData.handle@http://localhost:4200/assets/vendor.js:5355:6

最佳答案

实际上,您的解决方案看起来是正确的。

我猜你在服务器响应或请求方法不匹配方面有问题 GET/POST。要解决此问题,请尝试在 makeRequest 中调试 promise。

    return new RSVP.Promise((resolve, reject) => {
fetch(url, options).then((response) => {
response.text().then((text) => { //<-- here debug text
let json = text ? JSON.parse(text) : {};
if (!response.ok) { //<-- and here debug response
response.responseJSON = json;
reject(response);
} else {
resolve(json);
}
});
}).catch(reject);

因此,如果这里出现问题,只需重写 makeRequest 的整个方法,并使用自定义 fetch 添加您自己的 promise 。

另一种方法是编写自定义 Authenticator,重写 authenticaterestore 和(可选)invalidate 方法作为在文档中写道:https://github.com/simplabs/ember-simple-auth#implementing-a-custom-authenticator

关于javascript - 实现自定义 ember-simple-auth 身份验证器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46804682/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com