即使用户名密码完全相同,我也收到401未经授权的访问错误代码,我最初创建server.js来传递模拟数据。 我可以使用此处的模拟API登录 http://demo1967295.mockable.io/userauth0 甚至在调用api之前,即使服务器响应,它也会返回500(内部服务器错误)
Server.js文件,其中包含以前用于传递模拟数据的api调用
var express = require("express");
var bodyParser = require("body-parser");
var session = require("express-session");
var app = express();
app.use(express.static("app"));
app.use(session({
cookie: {
maxAge: 60 * 1
},
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 user = req.body.email;
var pass = req.body.password;
var location =req.location;
if (user === req.body.email && pass === req.body.pass) {
req.session.user = req.body.email;
req.session.password = req.body.password;
console.log("Inside login logic"+req.body.email);
res.end();
} else {
res.status(401).end();
console.log("Inside login else logic"+req.body.email);
}
});
app.get("/me", function (req, res, next) {
console.log(req.session.id);
if (req.session.user) {
res.send(req.session.user);
} else {
res.status(401).end();
}
});
var server = app.listen(4000, function () {
console.log("Sample login server running");
});
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"
})
// main app view definition
.when("/main", {
// controller: "mainController",
// controllerAs: "vm",
templateUrl: "main.html"
})
// many other routes could be defined here
// and redirect the user to the main view if no routes match
.otherwise({
redirectTo: "/main"
});
});
// execute this function when the main module finishes loading
app.run(function ($rootScope, $location) {
// attach to the event that fires before the router changes routes
$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", function ($http) {
return {
checkLogin: function () {
return $http.post("https://api-url/api-name/auth/login").then(function (response) {
return response.data;
});
},
login: function (email, password) {
console.log("loginn fucntion");
return $http.post("/login", {
email: vm.email,
password: vm.password
}).then(function (response) {
return response.data;
}, function (response) {
var err = new Error(response.statusText);
err.code = response.status;
throw err;
});
}
};
})
app.controller("loginController", function ($rootScope, $location, loginService) {
var vm = this;
function success() {
$rootScope.loggedIn = true;
var back = $location.search().back || "";
$location.url(back !== "/login" ? back : "");
}
function failure() {
$rootScope.loggedIn = false;
}
loginService.checkLogin().then(success);
vm.login = function () {
loginService.login(vm.user, vm.pass).then(success, failure);
};
});
login.html
<form class="form-signin" ng-submit="vm.login()">
<label >Email</label>
<input type="email" class="form-control" ng-model="vm.user" required autofocus>
<br />
<label >Password</label>
<input type="password" class="form-control" ng-model="vm.pass" required>
<button class="btn btn-md btn-primary btn-block" value="send" type="submit">Sign
in</button>
<p class="mt-5 mb-3 text-muted text-center" >© 2018-2019 <br />Version <app-version></app-version>
</p>
</form>
<form ng-submit="vm.login()">
User<input type="text" ng-model="vm.user">
User<input type="text" ng-model="vm.pass">
<input type="submit" value="send">
</form>
答案 0 :(得分:0)
// execute this function when the main module finishes loading
app.run(function ($rootScope, $location) {
// attach to the event that fires before the router changes routes
$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", function ($http) {
return {
checkLogin: function () {
return $http.post("/login").then(function (response) {
return response.data;
});
},
login: function (email, password) {
console.log("loginn fucntion");
return $http.post("https://api-url/api-name/auth/login", {
email: vm.email,
password: vm.password
}).then(function (response) {
return response.data;
}, function (response) {
var err = new Error(response.statusText);
err.code = response.status;
throw err;
});
}
};
})