[英]Redirect logged in user to different pages in node.js
我有一个可以使用注册用户登录的应用程序。 我想根据登录用户的角色(此处为实体类型)将其重定向到其他主页。 这是该应用程序的登录控制器,还有node.js代码以及登录html。 我希望有人能帮助我。
login.html-
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Login User</title>
</head>
<body>
<div ng-controller="loginCtrl">
<h1>Login</h1>
<div class="row">
<div class="col-md-8">
<section>
<form role = "form" data-ng-submit = "SendLoginData()"
class="loginpage">
<h4>Use a local account to log in</h4>
{{ PostDataResponse }}
<br />
<div class="form-horizontal">
<div class="form-group">
<label class="col-md-2 control-label">Email</label>
<div class="col-md-10">
<input type="email" ng-model="email"/>
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">Password</label>
<div class="col-md-10">
<input type="password" ng-model="password" />
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">Remember me?</label>
<div class="col-md-10">
<input type="checkbox" ng-model="checkboxModel.value1"
ng-true-value="'YES'" ng-false-value="'NO'">
<!-- </form> -->
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<button type="submit" class="btn btn-default">Log in</button>
</div>
</div>
<div>
<p>
<a href="#/register">Register as a new user?</a>
</p>
<p>
<a href="ForgotPassword">Forgot your password?</a>
</p>
</div>
</form>
</section>
</div>
<div class="col-md-4">
<section>
<h4>Use another service to log in.</h4>
<p>
Test content here!
</p>
</form>
</section>
</div>
</div>
</div>
</body>
</html>
app.js-
app.controller('loginCtrl', function($scope, $location, $http) {
console.log("My Controller reporting for duty.");
$scope.SendLoginData = function() {
var data = ({
'LoginEmail' : $scope.email,
'LoginPassword' : $scope.password
});
console.log(data);
console.log(JSON.stringify(data));
JSON.stringify(data);
$http.post('/login', data)
.success(function (data, status, headers, config) {
console.log(status);
if (status == 201) {
$location.path('/');
}
})
.error(function(data, status, header, config){
$scope.PostDataResponse = "Error " +status + ": Email/Password are not matching. Please check your credentials.";
});
};
});
index.js-
var PORT = 4569;
var body_parser = require('body-parser');
var express = require('express')
var app = express();
var bigInt = require('big-integer');
var async = require('async');
var bcrypt = require('bcryptjs');
var location = require('location');
var path = require('path');
var http = require('http');
var sql = require('mssql');
app.use(body_parser.urlencoded({extended: true}));
app.use(express.static('public'));
app.use(body_parser.json());
app.set('views', 'app/views');
app.set('view engine', 'ejs');
var config = {
服务器:“ localhost”,数据库:“ chico”,用户:“ Soumya”,密码:“ secret”,端口:1433};
app.post('/login', function(req, res) {
console.log(req.body.LoginEmail);
console.log(req.body.LoginPassword);
var dbConn = new sql.ConnectionPool(config);
dbConn.connect().then(function () {
console.log("I am the error 4");
var transaction = new sql.Transaction(dbConn);
console.log("I am the error 5");
transaction.begin().then(function () {
var request = new sql.Request(transaction);
console.log("I am the error 6");
request.query("select Email,EntityType from dbo.Userregister1 where Email = '"+req.body.LoginEmail+"' and PasswordHash = '"+req.body.LoginPassword+"'", function (err, row) {
if(err) {
console.log('Error1');
}
else if (row.rowsAffected[0] == 1) {
console.log('Error2');
res.sendStatus(201);
} else if (row.rowsAffected[0] != 1) {
console.log('Error3');
res.sendStatus(399)
};
})
})
});
});
app.listen(PORT, function () {
console.log('Server Started. You can access the editor from http://localhost:' + PORT)
})
在服务器上,如果可以检测到哪种类型的用户登录(在错误检测块之后的代码中),则可以执行以下操作:
if(row.data['userType'] === "admin") {
res.redirect("/adminPanel");
}
else if(row.data['userType'] === "editor") {
res.redirect("/editorPanel");
}
对于您可能在应用程序中扮演的任何角色,您都将执行类似的操作。 我不确定关于row.data的语法,因为我不使用MSSQL,但似乎应该与之接近。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.