将登录用户重定向到node.js中的不同页面

时间:2017-05-01 03:06:02

标签: javascript angularjs node.js express passport.js

我有一个应用程序,我可以使用我的注册用户登录。我想根据用户的角色将登录用户重定向到不同的主页(这里是enititytypes)。这是应用程序的登录控制器以及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', 密码:'秘密', 港口: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)
})

1 个答案:

答案 0 :(得分:0)

在服务器上,如果您有办法检测登录的用户类型(在错误检测块之后的代码中),您可以执行以下操作:

if(row.data['userType'] === "admin") {
    res.redirect("/adminPanel");
}
else if(row.data['userType'] === "editor") {
    res.redirect("/editorPanel");
}

对于您在应用上可能拥有的任何角色,您都会这样做。关于row.data的语法我不是100%肯定,因为我不使用MSSQL,但它似乎应该接近它。