完全失去了对AngularJS认证实施失去了、AngularJS

2023-09-13 03:33:36 作者:辣萝卜条儿

我没有新的节目,但我是新来的AngularJS。我与我的第一个AngularJS现场工作,并正在寻求增加一个认证和放大器;授权层到我的应用程序。我想按照本文这似乎是非常简单的: https://开头中.COM / @ mattlanham /认证与-angularjs-4e927af3a15f

I am not new to programming, but I am new to AngularJS. I am working with my first AngularJS site and am looking to add an authentication & authorization layer to my app. I am trying to follow this article which seems to be very straight forward: https://medium.com/@mattlanham/authentication-with-angularjs-4e927af3a15f

不过,我发现,太多的AngularJS文章只给出片段不上实际提供指导 ,其中的以真正实现了code!非常,非常沮丧。 。 。

However, I find that too many AngularJS articles only give snippets without actually giving guidance on where to actually implement the code! Very, very frustrating . . .

我有一个:

在我的应用程序的根目录app.js controller.js为一个网页,我想提出一个授权层后面我也有相应的REST服务来处理用户身份验证

有人可以帮我区分app.js应该从文章中哪些项目(如果有的话???),哪些应该在我的个人网页去?

Can someone please help me distinguish what items from the article should go in app.js (if any???) and which ones should go in my individual page?

我的app.js了像:

My app.js has looked like:

var app = angular.module('appname', [$stateProvider]);
app.config(function($stateProvider, $urlRouterProvider){
    alert("auth fired");
    $stateProvider
        .state("account", {
            url: "/account.html",
            templateUrl: "/account.html",
            controller: "HistoryCtrl",
            authenticate: true
        })
        .state("login", {
            url: "/login.html",
            templateUrl: "/login.html",
            controller: "LoginCtrl",
            authenticate: false
        });
    // Send to login if the URL was not found
    $urlRouterProvider.otherwise("/login.html");
});

和我想要的网页,以确保看起来像:

And the page I want to secure looked like:

var app = angular.module('appname',  []);
app.controller('HistoryCtl', function($scope, $http) {
    app.config(['$httpProvider', function ($httpProvider) {
        $httpProvider.defaults.useXDomain = true;
        delete $httpProvider.defaults.headers.common['X-Requested-With'];
        $httpProvider.defaults.headers.common = {};
        $httpProvider.defaults.headers.post = {};
        $httpProvider.defaults.headers.put = {};
        $httpProvider.defaults.headers.patch = {};
    }]);

    $http({
        method: "post",
        url: "http://serviceURL/v1/account",
        headers:{'Content-Type': 'application/json'},
        data: {"txtToken":"myusertoken"}
    }).success(function (response) { $scope.names = response.entries;});
});

app.run(function ($rootScope, $state, AuthService) {
    $rootScope.$on("$stateChangeStart", function(event, toState, toParams, fromState, fromParams){
        if (toState.authenticate && !AuthService.isAuthenticated()){
            // User isn’t authenticated
            $state.transitionTo("login");
            event.preventDefault();
        }
    });
});

但这些似乎火灾和有在控制台中没有错误。如何将一个着手从文章贯彻片段?可有人请根据每个片断应该去的文件拆分出来?我是否需要申报app.js在我的HTML源文件或HTML标记使用的应用程序名称是否足够?

But none of these seemed to fire and there were no errors in the console. How would one go about implementing the snippets from the article? Can someone please break it out according to the file in which each snippet should go? Do I need to declare app.js as a source file in my HTML or is using the app name in the HTML tag sufficient?

推荐答案

欢迎到角!你能确认你有一个 NG-应用='APPNAME'指令在你的HTML?你需要用它来引导整个应用程序。您还需要申报 ui.router 作为依赖,你需要为了这个工作。

Welcome to Angular! Can you verify that you have an ng-app='appname' directive in your HTML? You'll need this to "bootstrap" the entire application. You'll also need to declare ui.router as a dependency, which you'll need in order for this to work.

我在 Stormpath 工作,我们有一个指南,告诉您如何从头构建一个角应用,包括授权(使用我们的模块)。你可以在这里找到: Stormpath的AngularJS指南

I work at Stormpath and we have a guide which shows you how to build an Angular application from scratch, including authorization (using our module). You can find it here: Stormpath's AngularJS Guide

希望这有助于!