身份验证令牌不会在请求之间保持不变

时间:2017-05-23 15:20:53

标签: angularjs grails

不确定为什么会这样。看来我的令牌被摧毁了,或者我在角度做错了什么?我最初通过身份验证和页面呈现,但此后重复并要求在每个帖子上进行身份验证。

首先,这是角度的基本结构....它不是单页应用程序,但有多个。Asset pipeline structure

fytrnlt.fytrnlt.js

//= wrapped
//= require /angular/angular 
//= require /angular/angular-ui-router
//= require /angular/angular-cookies
//= require /angular/angular-resource
//= require /fytrnlt/core/fytrnlt.core
//= require /fytrnlt/fybkbra/fytrnlt.fybkbra
//= require /fytrnlt/spriden/fytrnlt.spriden
//= require_self
//= require_tree services
//= require_tree controllers
//= require_tree directives
//= require_tree domain
//= require_tree templates

angular.module("fytrnlt.fytrnlt", [
    "ui.router",
    "ngResource",
    "ngCookies", //added May23/17
    "fytrnlt.core",
    "fytrnlt.fybkbra",
    "fytrnlt.spriden"
]).config(config);

function config($stateProvider) {
    $stateProvider
        .state('fytrnlt', {
            url: "/api/fytrnlt",
            abstract: true,
            template: "<div ui-view></div>"
        })
        .state('fytrnlt.list', {
            url: "",
            templateUrl: "/fytrnlt/fytrnlt/list.html",
            controller: "FytrnltListController as vm"
        })
        .state('fytrnlt.create', {
            url: "/create",
            params: {"fybkbraId":null,"spridenId":null, "fyrsignId":null},
            templateUrl: "/fytrnlt/fytrnlt/create.html",
            controller: "FytrnltCreateController as vm"
        })
        .state('fytrnlt.edit', {
            url: "/edit/:id",
            templateUrl: "/fytrnlt/fytrnlt/edit.html",
            controller: "FytrnltEditController as vm"
        })
        .state('fytrnlt.show', {
            url: "/show/:id",
            templateUrl: "/fytrnlt/fytrnlt/show.html",
            controller: "FytrnltShowController as vm"
        })
        .state('fytrnlt.firemyvalidations', {
        url: "/firemyvalidations",
        templateUrl: "/fytrnlt/fytrnlt/fvalid.html",
        //controller: "FytrnltShowController as vm"
            //controller: "Fytrnlt as vm"
            //TestReturnmyList
    });
}

fytrnltListController.groovy

angular
    //.module("fytrnlt.fytrnlt")
    .module("fytrnlt.fytrnlt", ["ngCookies"])
    .factory('authInterceptor', function ($rootScope, $window) {
        return {
            request: function (config) {
                config.headers = config.headers || {};
                if ($window.sessionStorage.token) {
                    config.headers.Authorization = 'Bearer ' + $window.sessionStorage.token;
                }
                return config;
            }
        };
    })
    .config(function ($httpProvider) {
        $httpProvider.interceptors.push('authInterceptor');
    })
    .controller("FytrnltListController", FytrnltListController);
//function FytrnltListController(Fytrnlt){
//function FytrnltListController(Fytrnlt, $http, $window) {
function FytrnltListController(Fytrnlt, $http, $window, $rootScope, $cookies) {
    var vm = this;

    var max = 1000, offset = 0;

    /* working code without login checking
    Fytrnlt.list({max: max, offset: offset}, function(data) {
        vm.fytrnltList = data;
    });
    */

   // vm.authenticated = false;
   // $rootScope.authenticated = false;

    //try new logic to fetch cookie
    vm.loggedcookie = $cookies.get('globals'); //see code further down

    if(vm.authenticated == null){
        vm.authenticated = false;
    }

if( $rootScope.authenticated == null)
{
    $rootScope.authenticated = false;

}
    vm.login = function () {
        $http.post('/api/login', {
            username: vm.user.username,
            password: vm.user.password
        }).then(function (response) {
            vm.authenticated = true;
            $rootScope.authenticated = true;

            //store user
            $rootScope.globals = {
                currentUser: {
                    username: username
                }
            };
            //store in cookie because I am having an arch issue
            var cookieExp = new Date();
            cookieExp.setDate(cookieExp.getDate()+1);
            //$cookies.putObject('globals', $rootScope.globals,{expires: cookieExp});
            $cookies.put('globals', $rootScope.globals,{expires: cookieExp});


                $window.sessionStorage.token = response.data.access_token;
                Fytrnlt.list({max: max, offset: offset}, function(data) {
                    vm.fytrnltList = data;
                });
        });
    };
}

List.tpl.html

<div class="nav" role="navigation">
    <ul>
        <li><a ui-sref="index" class="home">Home</a></li>
       <!-- <li><a ui-sref="fytrnlt.create" class="create">New Fytrnlt</a></li> -->
        <li><a ui-sref="index" class="home">vm.authenticate={{vm.authenticated}}</a></li>
        <li><a ui-sref="index" class="home">vm.user.username={{vm.user.username}}</a></li>
        <li><a ui-sref="index" class="home">$rootScope.authenticated={{$rootScope.authenticated}}</a></li>

        <li><a ui-sref="index" class="home">$window.sessionStorage.token={{$window.sessionStorage.token}}</a></li>

    </ul>
</div>




<div id="list-fytrnlt" class="content scaffold-list" role="main">
    <!--   <div ng-switch="vm.authenticated">
              <div ng-switch={{vm.authenticated}}>
          <div ng-switch="$rootScope.authenticated">
           <div ng-switch="$rootScope.authenticated">
          <div ng-switch={{$rootScope.authenticated}}>
           <div ng-switch on="(vm.authenticated == 'true') || (vm.loggedcookie != null)">
    -->
    <div ng-switch="vm.authenticated"> 

    <div ng-switch-when="true">

        <h1>Transfer Letter List</h1>
          <table>
        <thead>
          <tr>
            <th>Id</th>
            <th>Checknum</th>
            <th>Vendor</th>
            <th>Bankcode</th>
            <th>Transfer Bank</th>
            <th>Branchcode</th>
            <th>Docdate</th>
            <th>Doctype</th>
          </tr>
        </thead>
        <tbody>
          <tr ng-class="{'even': $index%2 == 0, 'odd': $index%2 == 1}" ng-repeat="fytrnlt in vm.fytrnltList">
            <td>
              <a ui-sref="fytrnlt.show({id: fytrnlt.id})">{{fytrnlt.id}}</a>
            </td>
            <td>
              <span>{{fytrnlt.checknum}}</span>
            </td>
            <td>
              <span>{{fytrnlt.name}}</span>
            </td>
            <td>
              <span>{{fytrnlt.bankcode}}</span>
            </td>
            <td>
              <span>{{fytrnlt.transferbank  }}</span>
            </td>
            <td>
              <span>{{fytrnlt.branch}}</span>
            </td>
            <td>
              <span>{{fytrnlt.docdate | date:'yyyy-MM-dd HH:mm:ss Z'}}</span>
            </td>
            <td>
              <span>{{fytrnlt.doctype}}</span>
            </td>
          </tr>
        </tbody>
      </table>
          <!--    <div class="pagination">
                  <g:paginate total="${partCount ?: 0}" />
              </div>-->

      <!--  <div ng-include="'/fytrnlt/index/index.html'"></div>-->
    </div>
    <div ng-switch-when="false">
      <div ng-include="'/fytrnlt/index/login.html'"></div>
    </div>
  </div>
</div>

作为侧边栏,您可能会看到一些与cookie相关的代码,因为我认为我可能需要将其用作解决方法。我不明白为什么会这样,并且已经看到其他变化(尽管有单页应用程序)正常工作。我希望同一页面上的帖子能够正常运行。

1 个答案:

答案 0 :(得分:0)

更新:已解决会话身份验证令牌持久存在。问题是我必须在angular switch语句中设置一些其他变量。此外,如果设置了令牌,我需要重新加载数据。