Sharing data between backend and front end

I am building a social app like TikTok or Instagram.

Probably the most important thing about the creation of this app is the back and front end communicating with each other. That’s how users will be able to upload posts, send messages, follow each other, etc. and how I’ll be able to check if a function returns true or false and what to do with the given result(s) etc.

Right now the only communication I have between the two is show the username of the user logged in. The username shows but if I want to show other things, like messages, following lists, posts etc. how would I do that ?

This should mostly be about the network.* API and backend knowledge, isn’t it? A while ago, I tried building a quiz game and that was the case. I’d suggest handling many things on the backend and using Solar2D as the frontend tool.

Since you can show the username of the logged in user, you can go on and request other things related to that user as well. It should be the same.

1 Like

Not really I show the username like composer.setVariable( "username", username.text ).

So when the user is successfully logged in, I just show the username that they used to login.

I am currently trying to show the credentials of the user currently logged in.

That’s OK but since you already have a mechanism in place that can verify user / password, the rest can also be done the same way.

I assume your login process looks something like this:
1- Username / password entered.
2- Login pressed.
3- Username / password match is checked.
a. Check failed - show error, go back to 1.
b. Check success - show home / profile page.

So what you need to do is, after step 3-b, query the other table that contains user information like how many followers they have, how many they follow, profile picture etc.

Something like this:
SELECT * from userInfo WHERE userId = test

1 Like

Yeah on the PHP page I can show the username, email and profile picture. I just have no idea how to show that stuff on the Front end side.

My login and register pages are based on button clicks. So whenever the user goes to the profile page, I want them to see their details (or if it’s someone else’s page that users stuff).

You’d need your PHP script to return some data so you can use it to create your application. I got something like this for one of my projects. By the way, this is really the easy part. You should look more into backend stuff for this instead of frontend. If you can return some data from network.request(), it’s Solar2D knowledge from there on.

You may also want to take a look at this:

and this:



local function listenerRetrieveProfile(event)
	if (event.isError) then
		print "is error"
	else
		print (event.response)

		local decoded, pos, msg = json.decode( event.response )

		if (not decoded) then
			print "not decoded"
		else
			userData = decoded
			print (userData[1].name)
		end
	end
end


local params = {}
params.headers = headers
params.body = "actionType=retrieveProfile&userID=" .. playerID

network.request( "http://www.yourpage.com/getProfile.php", "POST", listenerRetrieveProfile, params )

Lol yeah that’s where my problem is. Because I have an entire project built with PHP that shows it correctly. But I’ll try to do it with the stuff you recommended.

I don’t know why you deleted it but saw your reply. Since you have it all built up and running with PHP, you can share us sample JSON response so we can suggest stuff for you to do.

Lol I felt like I should try one more time on my own instead of bothering everyone.

I don’t think it shows any JSON.

login script(process2.php):

$sql = "SELECT pw FROM users WHERE username = ?";
$stmt = mysqli_prepare($con, $sql);
$stmt->bind_param('s', $username);
$stmt->execute();
$stmt->bind_result($hashed_pwd); // Binds variables to a prepared statement for result storage
$stmt->fetch(); // Fetch results from a prepared statement into the bound variables

if (password_verify($pw, $hashed_pwd)) {
       
    //session_id($_POST['user_session_id']); //starts session with given session id
    // password verified
    $_SESSION["user_session_id"] = $username;

    $cookie_name = $_SESSION["user_session_id"];
    $cookie_value = $cookie_name;
    setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day

    //header('Location: bootstrap.php'); 
    die($cookie_value);
    //return true;
    //echo $_SESSION["user_session_id"];
    

} else {

    echo 'Incorrect username or Password.';
    //die('Incorrect username or Password.');
    //return false;
}

bootsrap.php(where some of the profile stuff show up):

if(isset($_SESSION['user_session_id'])) {

	$userLoggedIn = $_SESSION['user_session_id'];
	//die($userLoggedIn);

} else {

    die('Not logged in');
	//header("Location: register.php");
}

$user_data_query = $con->prepare('SELECT username, email, profile_pic FROM users WHERE username = ?');
$user_data_query->bind_param("s", $userLoggedIn);
$user_data_query->execute();
$user_data_query->bind_result($username, $email, $img);
$user_data_query->fetch();

$user_data_query->close();

echo $username . "<br><br>";
echo $email . "<br><br>";

You can start by packing up $username and $email into a JSON and return that. If you could do that successfully, network listener method that you set up for network.request() call would catch that so you can build up on that.

1 Like

Ok so I tried doing that and I still see the results on the web page but I don’t see it in the app.

I did this:

local URL = "http://192.168.1.37/hashmobile/bootstrap.php?username=" .. userName ;
        --local URL="http://localhost:8080/corona/fetch.php?usr="..namee
local response = http.request(URL)
if response == nil then
    print("No Dice")
else
    local data = json.decode(response)
    print (data);--This part gives error
end

I get a result but it says that I am not logged in.

@anon63346430 and @H.R.H_The_Duke_of_Bi

Gentlemen, regardless of opinions and bad feelings, anyone is allowed to help anyone else here.

Let’s please keep this on topic. Arguments do nothing for the community.

Thanks,

Ed

4 Likes

I can delete posts here all day guys. Please no ‘getting the last word’, Let’s just keep it on topic.

Take it to PM if you want, but I’m not keen on public fights. They don’t help the community at all.

Thanks,

Ed

3 Likes

you can delete pots? lol

Yeah. I can delete, but I obviously can’t spell. :slight_smile:

1 Like

Ok I am getting much closer. I can see the username and email results but they show up as null.

Hello any help ?

Like I said before, you have to look for returning data through JSON. Take a look at that and take a look at the sample code I shared. That’s the key part in your problem. You have to return encoded JSON data from your backend code. Please take a look at the links I shared in the post below:

1 Like