gpt4 book ai didi

angularjs - 如何使用 stormpath 对 nodejs api 和 auth 进行 angularjs 调用

转载 作者:搜寻专家 更新时间:2023-10-31 23:38:26 26 4
gpt4 key购买 nike

以下代码在 angularjs 中开始,然后通过 Node 进行路由,然后 Node 应该进行外部 api 调用。发生了什么,stormpath 正在拦截调用并尝试对其进行身份验证(提供登录屏幕...我检查了 res.data)。我认为该应用程序已经可以使用了,因为我最初必须登录。我没有在 ui-router 中定义路由,因为我希望 Node 处理它而不是 Angular 。所以,我的问题是如何配置不同的层来处理调用。

Angular -> Node/stormpath -> 外部 api

Node 路由:

  app.use('/api/v1/accounts/:search', stormpath.groupsRequired(['dataentry']), apiAccounts.findAll);

Storm 路径设置:

  app.use(stormpath.init(app, {
apiKeyFile: config.stormpathapi.apiKeyFile,
application: config.stormpathapi.application,
secretKey: config.stormpathapi.secretKey,
sessionDuration: 1000 * 60 * 30,
enableAutoLogin: true,
enableUsername: true
}));

Angular 代码:

  $scope.getCustomers = function(chars) {
var customers = [],
url = 'api/v1/accounts/' + encodeURIComponent(chars) + '/';

console.log('getCustomers: ' + url);
try
{
//return $http.jsonp(url)
return $http.get(url)
.then(function(res) {
if(angular.isArray(res.data))
{
customers = res.data;
}

return customers;
}, function (error) {
console.log('Unable to load the customers: ' + error)
});
}
catch(e){
console.log("error searching comapny: " + e);
}

}

网址格式正确。如果我在新的浏览器窗口中进行调用,它会按预期工作(并通过 stormpath api 对我进行身份验证。我想要此功能,以便没有人可以在未登录应用程序的情况下使用该 api。

网址格式:

  http://localhost:3000/api/v1/accounts/ap/

res.data 包含 stormpath html:

   <!DOCTYPE html><!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--><html lang="en" class="no-js"><!--<![endif]--><head><meta charset="utf- 8"><title>Log In</title><meta content="Log into your account!" name="description"><meta content="width=device-width" name="viewport"><link href="//fonts.googleapis.com/css? family=Open+Sans:300italic,300,400italic,400,600italic,600,700italic,700,800italic,800" rel="stylesheet" type="text/css"><link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet"><style>html,
body {
height: 100%;

.....

<body class="login"><div class="container custom-container"><div class="va-wrapper"><div class="view login-view container"><div class="box row"><div class="email-password-area col-xs-12 large col-sm-12"><div class="header"><span>Log In or <a href="/register">Create Account</a>

.....

谢谢!

最佳答案

我相信 Stormpath 中间件正在拦截 API 调用并尝试呈现登录页面。我认为解决方案是将 stormpath.LoginRequired 中间件放在为您的 Angular 应用程序提供服务的路由上,这将强制他们进入登录页面,并且在他们登录后应该有一个 session cookie,用于API 调用。

关于angularjs - 如何使用 stormpath 对 nodejs api 和 auth 进行 angularjs 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27786049/

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