简体   繁体   English

使用Angular验证用户API

[英]Using Angular to authenticate users API

I am building a web application in Angular ( Im new to Angular). 我正在Angular中构建一个Web应用程序(我是Angular的新手)。

I building a form to pass new user info to the third party software via API end point. 我构建了一个表单,以通过API端点将新的用户信息传递给第三方软件。

to post data I need to pass name(new user) email(new user) user_key(mine) and api_key (changes evry hour) 发布数据我需要通过name(new user) email(new user) user_key(mine) email(new user) user_key(mine)api_key (changes evry hour)

To get the new api_key I need to Post 要获取新的api_key我需要发布

POST: https://pi.pardot.com/api/login/version/3
message body: email=<email>&password=<password>&user_key=<user_key>

and this returns 然后返回

<rsp stat="ok" version="1.0">
    <api_key><api_key here></api_key>
</rsp>

Once I have the new Key I can pass it with the form post to post data. 获得新密钥后,我可以将其与表单发布一起传递以发布数据。

Question

I think Ill need to have a function that runs before the Posting of data that dose the post to get the new api_key and changes the api_key variable on my new user post. 我认为病态需要在发布数据之前运行一个函数,该函数对发布进行定量以获取新的api_key并更改新用户发布上的api_key变量。

so the main question is how do I do this with angular, I have added my current post controller bellow that post the new user data. 所以主要的问题是我如何使用angular做到这一点,我添加了当前的post controller波纹管,用于发布新的用户数据。

My controller 我的控制器

// submit button controller POST
// =============================================================================
FirstModule.controller('formController', function ($scope, $http) {
    $scope.formData = {};

    $scope.processForm = function (Fname, Lname, email) {
        var data = {
            Fname: $scope.formData.Fname,
            Lname: $scope.formData.Lname,
            email: $scope.formData.email,
            api_key: 'changes every hr', //needs to be dynamic
            user_key: 'my key'
        };

//Call the services
        $http.post('https://some api.com/api/prospect/version/4/do/create', JSON.stringify(data)).then(function (response) {
            if (response.data)
                $scope.formData = "Post Data Submitted Successfully!";

        }, function (response) {
            $scope.formData = "Post Data Submitted Failed";
            $scope.statusval = response.status;
            $scope.statustext = response.statusText;
            $scope.headers = response.headers();
        });
    };
});

My form 我的表格

<form name="myForm" id="signup-form" class="col-sm-8 col-sm-offset-2"
                  ng-submit="processForm()"
                  ng-click="postdata(formData)">

                <!-- our nested state views will be injected here -->
                <div id="form-views" ui-view></div>
            </form>

So this part is Ok, but how do I run a function before this runs so I get the latest api_key ? 所以这部分还可以,但是如何在运行之前运行一个函数,以便获得最新的api_key and more importantly how can I do it safe ? 更重要的是,我该如何安全?

The API is Pardot API是Pardot

For security should I place all; 为了安全起见,我应该全部放置; API request on a backed script maybe node ? 在支持脚本上的API请求也许是节点?

you can try to use app.run() and write the function in that. 您可以尝试使用app.run()并在其中编写函数。 app.run() will always run before your controller. app.run()将始终在您的控制器之前运行。 Here is one link you can check AngularJS app.run() documentation? 这是一个可以查看AngularJS app.run()文档的链接?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM