无法存储Cookie并允许会话

时间:2019-04-09 06:33:41

标签: javascript html css angularjs

我正在尝试创建一个简单的票务应用程序。

用户登录后,它应该显示一个仪表板,其中包含项目名称和3种票证类别以及一个添加票证按钮以添加新票证

问题,我能够在登录屏幕上对用户进行身份验证,但是当它导航到仪表板的下一个屏幕时,用户将无法获得身份验证,因此无法获取api并显示正在发送的数据< / p>

index.js

var app = angular.module("loginTest", ["ngRoute"]);

app.config(function ($routeProvider) {
    $routeProvider
        // login view definition
        .when("/login", {
            controller: "loginController",
            controllerAs: "vm",
            templateUrl: "login.html"
        })

        .when("/main", {
            controller: "mainController",
            controllerAs: "vm",
            templateUrl: "main.html",
            authenticated:true
        })
        .when("/setting", {
            controller: "settingController",
            controllerAs: "vm",
            templateUrl: "project.html",
            authenticated:true
        })
        .when("/about", {
            controller: "aboutController",
            controllerAs: "vm",
            templateUrl: "about.html",
            authenticated:true
        })
        .when("/addTicket",{
            controller:"addTicketCtrl",
            controllerAs:"vm",
            templateUrl:"addTicket.hmtl",
            authenticated:true
        })
        .otherwise({
            redirectTo: "/main",
            authenticated:true
        });
});

app.run(function ($rootScope, $location) {

    $rootScope.$on("$routeChangeStart", function (event, next) {
        // check current login status and filter out if navigating to login
        if (!$rootScope.loggedIn && next.originalPath !== "/login") {
            // remember the original url
            $location.url("/login?back=" + $location.url());
        }
    });
});

app.config(['$qProvider', function ($qProvider) {
    $qProvider.errorOnUnhandledRejections(false);
}]);

app.service("loginService",$cookies, function ($http, $cookies) {

    console.log("service");
    $cookies.get(email),
    $cookies.get(password)
    return {
        checkLogin: function () {
            return $http.post("/login").then(function (response) {
                console.log(response.data);
                return response.data;
            });
        },
        login: function (email, pass, $cookies) {
            console.log("loginn fucntion");
            return $http.post("https://qa.workdaysync.io/getcadentapi/auth/login", {
                email: email,
                password : pass,


            }).then(function (response) {
                //console.log("1st response"+response.status);
                return response.data;
            }, function (response) {
                var err = new Error(response.statusText);
                //console.log("2st response" +response.errorCode);
                err.code = response.status;
                if (response.status == 401) {
                    var error = "USERNAME OR PASSWORD IS INCORRECT"
                    var loginfailed = true;
                    //alert(error);
                    //console.log(loginfailed);
                }
                throw err;
            });
        }
    };
})
/* app.service("addTicketService",function($rootScope,$http){
    var vm=this;
    addTicket: function (description){
        return $http.post("https://qa.workdaysync.io/getcadentapi/tickets", {
            description: description,
            id: name
        }).then(function (response) {
            //console.log("1st response"+response.status);
            return response.data;
            console.log(response.data);
        }

})
}); */
app.controller("loginController",$cookies, function ($rootScope, $location, loginService,$cookies) {
    var vm = this;

    function success() {
        $rootScope.loggedIn = true;
        $rootScope.loginError = false;
        $cookies.put('username', email);
        $cookies.put('password', password);
        var back = $location.search().back || "";
        $location.url(back !== "/login" ? back : "");/* 
        $cookies.put('username', email);
        $cookies.put('password', password); */


    }

    function failure() {
        $rootScope.loggedIn = false;
        $rootScope.loginError = true;
    }

    loginService.checkLogin().then(success);

    vm.login = function () {
        loginService.login(vm.user, vm.pass).then(success, failure);
    };

    vm.logout = function () {
        loginService.logut(
            localStorage.clear(),
            sessionStorage.clear()
        );
    }
})
app.service('HttpService', function ($http) {
    return {
        getPost: function () {
            // $http returns a promise, which has a then function, which also returns a promise.
            return $http.get('https://qa.workdaysync.io/getcadentapi/sync')
                .then(function (response) {
                    // In the response, resp.data contains the result. Check the console to see all of the data returned.
                    console.log('Get Post', response);
                    return response.data;
                });
        },
        getUsers: function () {
            // $http returns a promise, which has a then function, which also returns a promise.
            return $http.get('https://qa.workdaysync.io/getcadentapi/sync')
                .then(function (response) {
                    // In the response, resp.data contains the result. Check the console to see all of the data returned.
                    console.log('Get Users', response);
                    return response.data;
                });
        }
    }
});
app.controller('mainController', function ($scope, HttpService) {
    HttpService.getPost()
        .then(function (response) {
            $scope.post = response;
        });
    HttpService.getUsers()
        .then(function (response) {
            $scope.users = response;
            console.log("asdfdsfsdf");
            console.log("umm"+ $scope.users.inProgressCount);
            console.log("asdfdsfsdf++++sdsad");
            var myNumber =  $scope.users.inProgressCount;
            var formattedNumber = ("0" + myNumber).slice(-2);
            console.log(formattedNumber);
        });
});
app.filter('counterValue', function(){
    return function(value){
      value = parseInt(value);
       if(!isNaN(value) && value >= 0 && value < 10)
          {return "0"+ value;
       return "";
    }else{
        return value;
        return "";
    }
    }
 })

server.js

var express = require("express");
var cookieSession = require('cookie-session');
var bodyParser = require("body-parser");
var session = require("express-session");

var app = express();

app.use(express.static("app"));
app.use(cookieSession({
    secret: '1234567qwerty',
    signed: true,
  }));
app.use(session({
    cookie: {
        maxAge: 60 * 10
    },
    resave: false,
    rolling: true,
    saveUninitialized: true,
    secret: "COOKIE_SECRET"
}));
app.use(bodyParser.json());

app.get("/login", function (req, res, _next) {
    console.log(req.session.id);
    var email = req.body.email;
    var pass = req.body.pass;
    if (email == req.session.user && pass == req.session.pass) {
        res.status(401).end();
        console.log("Inside login else logic" + req.body.email);

    } else {
        req.session.user = req.body.email;
        req.session.password = req.body.password;
        console.log("Inside login if logic" + req.body.email);
        res.end();
    }
});

app.get("/me", function (req, res, next) {
    console.log(req.session.id);
    if (req.session.user) {
        res.send(req.session.user);
    } else {
        res.status(405).end();
        console.log("status");
    }
});

var server = app.listen(4000, function () {
    console.log("Sample login server running");
});

它应该登录,然后显示票证筹集数量,并且当单击添加票证按钮时,cookie应该有效

0 个答案:

没有答案