简体   繁体   中英

Parse.com Not logging In User

I made a login system using Parse.com as a back end and for the life of me I can't figure out why this will not work with the code I have. When the user enters their UserID and password it is supposed to check the parse database and, if found, log them in based upon which role they have. (Send them to menu1, menu2, or menu3 basically). If it can't find them it is supposed to create an alert saying "Username of Password Incorrect" and clear the form.

If you click on the 'Sign In' button as of now it doesn't do anything

The create_account.html successully registers a User in the Parse database with a username and password so I know it connects....I just can't log them in.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <!--Bootstrap-->
        <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
        <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
        <!--Parse-->
        <script type="text/javascript" src="http://www.parsecdn.com/js/parse-latest.js"></script>
        <script src="//www.parsecdn.com/js/parse-1.6.7.min.js"></script>
        <!--Javascript-->
        <!--Stylesheet-->
        <link rel="stylesheet" type="text/css" href="css/login.css">
        <title>index.html</title>
    </head>

    <body>
        <div class="container">             
            <div class="row">
                <div class="col-xs-12">
                    <h1>Study App</h1>
                    <h2>Please Sign In</h2>
                    <form class="form-signin">
                        <div class="form-group">
                            <input type="text" class="form-control" id="userID" placeholder="User ID">
                        </div> <!--end User ID-->       
                        <div class="form-group">
                            <input type="password" class="form-control" id="password" placeholder="Password">
                        </div> <!--end Password-->
                            <a href="urltt">Forgot Password?</a> <!--Not implemented yet-->
                        <button type="button" class="btn btn-lg btn-block btn-primary">Sign In</button>
                    </form> <!--end SIGN IN FORM-->
                </div> <!--END COLUMN-->
            </div> <!--END ROW-->


            <div class="row">
                <div class="col-xs-12">
                    <div id="add_User">
                        <img src="img/add_user.png" alt="Add User Icon" height="42" width="42">
                        <p><a href="create_account.html">Create Account</a></p>
                    </div>
                </div> <!--END COLUMN-->
            </div> <!--END ROW-->       

        </div> <!--end container-->
        <script src="js/login.js"></script>
    </body>
</html>

login.js

    $(function() 
{
   Parse.$ = jQuery;
   Parse.initialize("x", "y"); <!--Removed for security-->
});


$('.form-signin').on('submit', function(e) 
{ 
    e.preventDefault();
    var data = $(this).serializeArray(),userID = data[0].value,password = data[1].value;        

    Parse.User.logIn(username, password, {
        success: function (user) {
            var queryRole = new Parse.Query(Parse.Role);
            if(queryRole.equalTo('name', 'Student')) {
                window.location = "menu1.html"
            }
            else if (queryRole.equalTo('name', 'Tutor')) {
                window.location = "menu2.html"
            }
            else {
                window.location = "professor/professor-menu3.html"
            }
        },
        // If there is an error
        error: function (user, error) {
            console.log(error);
            alert("Username or Password incorrect");
            $(".form-signin")[0].reset();
        }
    });

});

You set userID and password :

var data = $(this).serializeArray(),userID = data[0].value,password = data[1].value;

then you use username and password :

Parse.User.logIn(username, password, {

Also, the way you get the userID and password seems a bit dangerous to me, if you ever add other fields in your form before those it'll break.

I would recommend you to switch to:

var username = $('#userID').val();
var password = $('#password').val();

instead.

Also, you don't have a submit button in your form. Change:

<button type="button" class="btn btn-lg btn-block btn-primary">Sign In</button>

to:

<button type="submit" class="btn btn-lg btn-block btn-primary">Sign In</button>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM