如何使用stormpath使angularjs调用nodejs api和auth

Wil*_*pez 3 node.js express angularjs angular-ui-router stormpath

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

angular - > node/stormpath - > external api

节点路由:

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

stormpath设置:

  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
  }));
Run Code Online (Sandbox Code Playgroud)

角度代码:

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

}
Run Code Online (Sandbox Code Playgroud)

网址正确格式化.如果我在新的浏览器窗口中进行调用,它会按预期工作(并通过stormpath api验证我.我想要这个功能,这样没有人可以使用api而无需登录到应用程序.

网址格式:

  http://localhost:3000/api/v1/accounts/ap/
Run Code Online (Sandbox Code Playgroud)

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>

  .....
Run Code Online (Sandbox Code Playgroud)

谢谢!

rob*_*tjd 5

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