Sunday 30 April 2017

Redirect logged in user to different pages in node.js

I have an application where I am able to login using my registered users. I want to redirect the logged in users to different home pages based on their roles (enititytypes here). Here is the login controller for the app and also the node.js code along with the login html. I hope somebody can help me.

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>
            
            <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 = {

     server: 'localhost',      database: 'chico',      user: 'Soumya',      password: 'secret',      port: 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)
})



via Soumya Bhat

No comments:

Post a Comment