Quantcast
Channel: Appcelerator Developer Center Q&A Unanswered Questions 20
Viewing all articles
Browse latest Browse all 8068

Authentication problem

$
0
0

Hi All,

I recently began to work on a tutorial I found on tuts+ showing a basic authentication set up using a tab group. Click here to view link. I was able to complete the tutorial with no issues using XAMPP as my local server. I decided to expand on the tutorial to cement my understanding of it and decided to display my Username used to log into the app on the main.js window. I mimicked what I had done before exactly, however on login it is not showing my username as "null". To say the least I am a bit frustrated. Below is my code:

app.js

Titanium.UI.setBackgroundColor('#fff');
var tabGroup    = Titanium.UI.createTabGroup();
var main    = Titanium.UI.createWindow();
var mainTab = Titanium.UI.createTab();
 
var login = Titanium.UI.createWindow({
    title:'My License',
    url:'main_windows/login.js'
});
 
var loginTab = Titanium.UI.createTab({
   title:"Login",
    window:login
}); 
 
var account = Titanium.UI.createWindow({
    title:'New Account',
    url:'main_windows/account.js'
});
 
var accountTab = Titanium.UI.createTab({
   title:'New Account',
   window:account
});
 
tabGroup.addTab(loginTab);
tabGroup.addTab(accountTab);
tabGroup.open();
 
Ti.App.addEventListener('grantEntrance', function(event)
{
    main.tabBarHidden   = true;
    main.title      = 'Welcome ' + event.name;
    main.url        = 'main_windows/main.js';
    main.name       = event.name;
    main.email      = event.email;
    main.username           = event.username;
    mainTab.window      = main;
 
    tabGroup.addTab(mainTab);
    tabGroup.removeTab(loginTab);
    tabGroup.removeTab(accountTab);
});
login.js
var win = Titanium.UI.currentWindow;
 
var username = Titanium.UI.createTextField({
    color:'#336699',
    top:10,
    left:10,
    width:300,
    height:40,
    hintText:'Username',
    keyboardType:Titanium.UI.KEYBOARD_DEFAULT,
    returnKeyType:Titanium.UI.RETURNKEY_DEFAULT,
    borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED
});
win.add(username);
 
var password = Titanium.UI.createTextField({
    color:'#336699',
    top:60,
    left:10,
    width:300,
    height:40,
    hintText:'Password',
    passwordMask:true,
    keyboardType:Titanium.UI.KEYBOARD_DEFAULT,
    returnKeyType:Titanium.UI.RETURNKEY_DEFAULT,
    borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED
});
win.add(password);
 
var loginBtn = Titanium.UI.createButton({
    title:'Login',
    top:110,
    width:90,
    height:35,
    borderRadius:1,
    font:{fontFamily:'Arial',fontWeight:'bold',fontSize:14}
});
win.add(loginBtn);
 
var loginReq = Titanium.Network.createHTTPClient();
 
 loginReq.onload = function()
{
    var json = this.responseText;
    var response = JSON.parse(json);
    if (response.logged == true)
    {
        username.blur();
        password.blur(); 
        Ti.App.fireEvent('grantEntrance', {
            name:response.name,
            email:response.email
        });
        win.close();
    }
    else
    {
        alert(response.message);
    }
};
 
loginBtn.addEventListener('click',function(e)
{
    if (username.value != '' && password.value != '')
    {
        loginReq.open("POST","http://localhost/Tuts/post_auth.php");
        var params = {
            username: username.value,
            password: Ti.Utils.md5HexDigest(password.value)
        };
        loginReq.send(params);
    }
    else
    {
        alert("Username/Password are required");
    }
});
main.js
var win = Titanium.UI.currentWindow;
 
var msg = Titanium.UI.createLabel({
    text:"\n\nYour email is:\n" + win.email + "\n\nyour name is:\n" + win.name + "\n\nyour username is:\n" + win.username,
    top:10,
    left:10,
    width:300,
    height:'auto'
});
win.add(msg);
my PHP file
<?php
 
$con = mysql_connect('localhost');
if (!$con)
{
    echo "Failed to make connection.";
    exit;
}
 
$db = mysql_select_db('test');
if (!$db)
{
    echo "Failed to select db.";
    exit;
}
 
$username = $_POST['username'];
$password = $_POST['password'];
 
$sql = "SELECT * FROM users WHERE username = '" . $username . "' AND password = '" . $password . "'";
$query = mysql_query($sql);
 
if (mysql_num_rows($query) > 0)
{
    $row = mysql_fetch_array($query);
    $response = array(
        'logged' => true,
        'name' => $row['name'],
        'email' => $row['email'],
        'username' => $row['username']
    );
    echo json_encode($response);
}
else
{
    $response = array(
        'logged' => false,
        'message' => 'Invalid Username and/or Password'
    );
    echo json_encode($response);
}
?>
Any help would be greatly appreciated because I am completely out of ideas.

Thanks in advance!


Viewing all articles
Browse latest Browse all 8068

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>