3

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

角度 -> 节点/风暴路径 -> 外部 api

节点路由:

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

风暴路径设置:

  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
  }));

角码:

  $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>

  .....

谢谢!

4

1 回答 1

5

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

于 2015-01-06T01:53:47.120 回答