How to redirect login page after successfull message

237 views Asked by At

I am programing a web application, The first page will be the login page, I already did that on the RouteConfig. I developed a function that do the checking with the controller, back with successfull message and after should reidrect to my index page. The message is shown but the redirection never passed. I tried to use two separate functions and controllers for login part and redirection part, with ng-submit for all the login form to do the login check and ng-click on the login submit to do the redirection. Please help me with that.

DataController :

public JsonResult UserLogin (LoginData d)
    {
        using (GestReclamationDBEntities dc = new GestReclamationDBEntities())
        {
            var user = dc.Users.Where(a => a.Username.Equals(d.Username) && 
a.Password.Equals(d.Password)).FirstOrDefault();
            return new JsonResult { Data = user, JsonRequestBehavior = JsonRequestBehavior.AllowGet 
};
        }
    }
public ActionResult Index()
    {
        return View();
    }

login-controller.js :

(function () {
'use strict';

angular
    .module('app')

    .controller('LoginController', function ($scope, $window, LoginService) {

        $scope.IsLogedIn = false;
        $scope.Message = '';
        $scope.Submitted = false;
        $scope.IsFormValid = false;

        $scope.LoginData = {
            Username: '',
            Password: ''
        };

        //Check is Form Valid or Not
        $scope.$watch('f1.$valid', function (newVal) {
            $scope.IsFormValid = newVal;
        });

        $scope.login = function () {
            $scope.Submitted = true;
            if ($scope.IsFormValid) {
                LoginService.GetUser($scope.LoginData).then(function (d) {
                    if (d.data.Username != null) {
                        var host = $window.location.host;
                        var landingUrl = "/Data/Index";
                        $scope.IsLogedIn = true;
                        $scope.Message = "Successfully login done. Welcome ";
                        $window.location.href = landingUrl;
                    }
                    else {
                        alert('Invalid Credential!');
                    }
                });
            }
        };

    })

    .factory('LoginService', function ($http) {

        var fac = {};
        fac.GetUser = function (d) {
            return $http({
                url: '/Data/UserLogin',
                method: 'POST',
                data: JSON.stringify(d),
                headers: { 'content-type': 'application/json' }
            });
        };
        return fac;

    });

})();

Login.cshtml :

<div ng-controller="LoginController">
<div class="login-form">
    <form novalidate name="f1" ng-submit="login()">
        <div style="color:rgb(142,2,2)">{{Message}}</div>
        <div ng-show="!IsLogedIn">
            <h2 class="text-center">Log in</h2>
            <div class="form-group">
                <input type="text" class="form-control" placeholder="Username" ng- 
model="LoginData.Username" name="cUsername" ng-class="Submitted?'ng-dirty':''" required autofocus>
                <span class="error" ng-show="(f1.cUsername.$dirty || Submitted) && 
f1.cUsername.$error.required">Username required</span>
            </div>
            <div class="form-group">
                <input type="password" class="form-control" placeholder="Password" ng- 
model="LoginData.Password" name="cPassword" ng-class="Submitted?'ng-dirty':''" required autofocus>
                <span class="error" ng-show="(f1.cPassword.$dirty || Submitted) && 
f1.cPassword.$error.required">Password required</span>
            </div>
            <div class="form-group">
                <button type="submit" class="btn btn-primary btn-block">Log in</button>
            </div>
        </div>

    </form>
</div>


</div>



@section scripts{

<script src="~/Scripts/app/controllers/login-controller.js"></script>
@*<script src="~/Scripts/app/controllers/access-to-app-controller.js"></script>*@
}

EDIT :

I tried to work with location.href like this :

$("#send").click(function () {
        $.ajax({
            type: 'POST',
            url: '@Url.Action("Login")',
            //dataType: 'json',
            //contentType: dataType,
            data: { Username: $("#Username").val(), Password: 
 $("#Password").val() },
            success: function (result) {
                if (true) {
                    location.href = '/Data/Index';
                } else {
                    location.href = 'Login';
                }
            },
            error: function (result) {
            }
    });

    });

I also add the id="send" to my Login button. Now when I click on login he goes directly yo the index page without verifiying credentials.

1

There are 1 answers

2
Valerio Zanazzo On

Try to do a location.href ='Controller/Method';

Do something like that

$.ajax({
                type: 'POST',
                url: '@Url.Action("Login")',
                //dataType: 'json',
                //contentType: dataType,
                data: { Username: $("#Username").val(), Password: $("#Password").val() },
                success: function (result) {
                    if (//anything in result) {
                        location.href = 'Controller1/Index1';
                    } else {
                        location.href = 'Controller2/Index2';
                    }
                },
                error: function (result) {
                }
            });