Retrieving Database Data during LogIn

96 Views Asked by At

I'm currently using Slim and Ajax to develop a mobile application. In my database I have a column which stores session codes for logins. I need to be able to once logged in, compare the username entered to the database and retrieve the sessionCode based on that.

My current PHP is this:

$app->post('/logIn/', 'logIn');
function logIn()
{       
    $request = \Slim\Slim::getInstance()->request();
    $q = json_decode($request->getBody());
    //$hashedPassword = password_hash($q->password, PASSWORD_BCRYPT);


    $sql = "SELECT * FROM users where username=:username AND password=:password";
    try {
        $db = getConnection();
        $stmt = $db->prepare($sql);         
        $stmt->bindParam("username", $q->username);
                    $stmt->bindParam("password", $q->password);
        $stmt->execute();
        //$row=$stmt->fetch(PDO::FETCH_ASSOC);
        $row=$stmt->fetch(PDO::FETCH_OBJ);
        //$verify = password_verify($q->password, $row['password']);
        $db = null;
        echo json_encode($row);                 
    } catch (PDOException $e) {
        echo $e->getMessage();
    }
} //PHP 5.4 LogIn

$app->get('/getSessionCode/:username','getSessionCode');
function getSessionCode($username)
{
    $request = \Slim\Slim::getInstance()->request();
    $q = json_decode($request->getBody());

    $sql = "SELECT * FROM users WHERE username=:username";
    try{
        $db = getConnection();
        $stmt = $db->prepare($sql);
        $stmt->bindParam("username", $username);
        $stmt->execute();
        $row=$stmt->fetchALL(PDO::FETCH_OBJ);
        $dbh=null;
        echo json_encode($row);
    }
    catch(PDOException $e){
        if(db != null) $db = null;
                echo $e->getMessage();
    }
}

Ajax Code:

    $("#checkLogIn").click(function()
{   
    username = $("#enterUser").val();

    $.ajax({
        type: 'POST',
        contentType: 'application/json',
        url: rootURL + '/logIn/',
        dataType: "json",
        data: checkLogIn(),
        success: function(data)
        {
            if(data != false)
            {                   
                $.ajax({
                    type: 'GET',
                    url: rootURL + "/getSessionCode/" + username,
                    dataType: "json",
                    success: sessionData
                });
            }
            else
            {
                alert("Username and/or Password was incorrect");
            }
        }
    })
});
function checkLogIn(data)
{               
    return JSON.stringify({
        "username": $("#enterUser").val(),
        "password": $("#enterPass").val(),
    });                 
}   
function sessionData(data){
        session = data.sessionCode;
        alert(username);
        alert(session);

        $.mobile.changePage("#mainMenu");
}

When I run the application and log in. It runs though no problem but when it reaches alert(session) it returns undefined. I have both session and username set globally as variables so that isn't an issue and when it reaches alert(username), it returns the username I entered.

1

There are 1 best solutions below

0
On

I was able to solve my issue by adding in a for loop into the sessionData function

function sessionData(data){
    for(var i = 0; i < 1; i++)
    {
        sessionStorage.code = data[i].sessionCode;
    }

        $.mobile.changePage("#mainMenu");
}