Unable to authenticate the user

157 Views Asked by At

I'm getting a 401 unauthorized access error code even when the username password are exactly the same, I created server.js initially to pass the mock data. I was able to login using the mock-api which is here http://demo1967295.mockable.io/userauth0 and even before the api is called it is returning the 500(internal server error) even though server are responding

Server.js file which contains the api call previously it was used to pass the mock data

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" >&copy; 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>
1

There are 1 best solutions below

0
On BEST ANSWER
// 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;
            });
        }
    };
})