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

标签 angularjs node.js express angular-ui-router stormpath

以下代码在 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/

相关文章:

javascript - 如何在 AngularJS 应用程序中显示代码块

angularjs - 使用Gulp用IIFE包装javascript文件

javascript - Node.js API 与 express 和 mysql - WHERE IN 和带有多个逗号分隔值的绑定(bind)参数

node.js - 如何在快速 route 显示图像

javascript - thinkster.io 教程上的错误 : [ng:areq] Argument is not a function, 未定义

javascript - AngularJS 用函数调用数组

javascript - Node.JS 大端 UCS-2

javascript - 我想从多个文本输入中获取输入并将它们发送到我的数据库,但只有最后一个输入发送

node.js - Node :- 92% chunk asset optimization TerserPlugin FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory

authentication - 检查每个 node.js 请求以获取身份验证凭据