B4J Question serve specific site to each user

ilan

Expert
Licensed User
Longtime User
hi

lately, i am working on several b4j web apps (and also other platforms). my question is more a technical question than related to b4j.
i made a media center web app where users can enter the site and watch a movie. i did it for my own needs (for my kids at home) so it is something like Netflix. you can filter categories for each user according to his age and so on.

so i came into the following issue (that i have already solved but not sure in the correct way).

i will explain.

so first there is the login page. after login, you are directed to the main page where you can browse between the movies. after you choose a movie you are directed to a site that is auto-generated that holds the movie description and the movie itself. let's call it movie.html
my problem was that all movies were redirected to this page and let say if i was watching the movie and another user would open in another browser a different movie and I would press F5 (refresh my page) then i would now watch the movie the other user was watching.

i solved it by generating for each user a new website -> save it on the server and redirect to it. now everyone has a unique site where he can watch his movie.

my question is what would be the right way to solve such an issue?

thanx, ilan
 

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
B4j can serve static pages .html and dynamic pages,

The best approach for your issue is to build the page everytime someone access the the url/endpoint

Instead of serving the html file directly, build it as a string and pass it to the method resp.write that receives a string, that string is shown in the browser

To do so just add the /endpoint to the handlers of the servers
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
B4j can serve static pages .html and dynamic pages,

The best approach for your issue is to build the page everytime someone access the the url/endpoint

Instead of serving the html file directly, build it as a string and pass it to the method resp. write that receives a string, that string is shown in the browser

To do so just add the /endpoint to the handlers of the servers

so you are saying it would be better to respond to the request with an HTML string instead of redirecting to the newly created HTML page?
but why is that better? it seems like to be the same approach, isn't it?

i am using the same technique as EJS where you have a template and fill the parts that are changing. this is what i do now and serve the site.
 
Upvote 0

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
Upvote 0

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User

This is how you write a cookie. Read a cookie is similiar just look in req

I advice you that your problem will not be solved with cookies. Or better said cookies is an overengineering in this case
 
Upvote 0

Xandoca

Active Member
Licensed User
Longtime User
Is there a simple example for b4j to creating cookies and read them?
If you are using Websocket take a look here
Specially GetAttribute2 and SetAttribute:
B4X:
Private Sub WebSocket_Connected (WebSocket1 As WebSocket)
    ws = WebSocket1
    name = ws.Session.GetAttribute2("name", "")
    If name = "" Then
        'name not found. Go to the login page. This will happen if the user goes to the chat page directly.
        WebUtils.RedirectTo(ws, "index.html")
    Else
        'remove the attribute
        'the user will be redirected to the login page next time.
        ws.Session.SetAttribute("name", "")
        CallSubDelayed3(ChatShared, "NewConnection", Me, name)
        txt.RunMethod("select", Null)
    End If
End Sub
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
If you are using Websocket take a look here
Specially GetAttribute2 and SetAttribute:
B4X:
Private Sub WebSocket_Connected (WebSocket1 As WebSocket)
    ws = WebSocket1
    name = ws.Session.GetAttribute2("name", "")
    If name = "" Then
        'name not found. Go to the login page. This will happen if the user goes to the chat page directly.
        WebUtils.RedirectTo(ws, "index.html")
    Else
        'remove the attribute
        'the user will be redirected to the login page next time.
        ws.Session.SetAttribute("name", "")
        CallSubDelayed3(ChatShared, "NewConnection", Me, name)
        txt.RunMethod("select", Null)
    End If
End Sub

but this is stored only as long the session is active. with cookies, i can store data and even if the user will get back in few days he will see those data. and everything is hashed.
like if he adds products to the chart i can store those in a cookie and later when he return to my page the chart will hold those products.

so how to create/read cookies when using WebSocket?
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
see:


1625499175882.png
 
Upvote 0

Xandoca

Active Member
Licensed User
Longtime User
but this is stored only as long the session is active. with cookies, i can store data and even if the user will get back in few days he will see those data. and everything is hashed.
like if he adds products to the chart i can store those in a cookie and later when he return to my page the chart will hold those products.

so how to create/read cookies when using WebSocket?
I think you're right about the session cookies.
I will make some tests and let you know.
Regards
 
Upvote 0

Xandoca

Active Member
Licensed User
Longtime User
I think you're right about the session cookies.
I will make some tests and let you know.
Regards
Ilan,

Attached an example on how to do it with Websocket and without Websocket.
1) With WebSockets
Add a filter at main class. In filter class, add the cookies you want for specific pages or for all pages. (Class Module SetCookies)
Maybe there are other ways to do it.

2) Without WebSockes
Same as With WebSockets ...
Or
You can the the following code inside the class module:
B4X:
Sub Handle(req As ServletRequest, resp As ServletResponse)
'...
    Dim c As Cookie
    c.Initialize("cookiename","cookvalue")
    c.MaxAge = 3600
    resp.AddCookie(c)
'...
End Sub

I made a test and works fine.

Regards
 

Attachments

  • Cookies.zip
    6.9 KB · Views: 126
Upvote 0

ilan

Expert
Licensed User
Longtime User
Ilan,

Attached an example on how to do it with Websocket and without Websocket.
1) With WebSockets
Add a filter at main class. In filter class, add the cookies you want for specific pages or for all pages. (Class Module SetCookies)
Maybe there are other ways to do it.

2) Without WebSockes
Same as With WebSockets ...
Or
You can the the following code inside the class module:
B4X:
Sub Handle(req As ServletRequest, resp As ServletResponse)
'...
    Dim c As Cookie
    c.Initialize("cookiename","cookvalue")
    c.MaxAge = 3600
    resp.AddCookie(c)
'...
End Sub

I made a test and works fine.

Regards

thank you very much @Xandoca for taking the time and making this example. i will have a look at it. although I will not use cookies to solve the issue posted in post #1 but working with cookies is very important for every web developer so I will need them for a different purpose (then eating them 😁)

 
Upvote 0

Xandoca

Active Member
Licensed User
Longtime User
thank you very much @Xandoca for taking the time and making this example. i will have a look at it. although I will not use cookies to solve the issue posted in post #1 but working with cookies is very important for every web developer so I will need them for a different purpose (then eating them 😁)

It's my pleasure. This is nothing compared to the time and work you spent at Game Tutorial (I've enjoyed so much).
Regards.
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
It's my pleasure. This is nothing compared to the time and work you spent at Game Tutorial (I've enjoyed so much).
Regards.

btw i am just getting ready to record part 5 :) (collision detection) i hope i can do it in 1 run and not 10 recording attempts :oops:
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
thank you very much @Xandoca for taking the time and making this example. i will have a look at it. although I will not use cookies to solve the issue posted in post #1 but working with cookies is very important for every web developer so I will need them for a different purpose (then eating them 😁)


πŸ˜‚πŸ˜‚πŸ˜‚πŸ˜‚πŸ˜‚πŸ˜‚πŸ˜‚πŸ˜‚πŸ˜‚πŸ˜‚πŸ˜‚πŸ˜‚

very very good
 
Upvote 0

tchart

Well-Known Member
Licensed User
Longtime User
This is partially correct. Its better to use variable sessions than cookies because the formers are not send to the client

^ This

It is better to store the data in the session as the user cannot forge them.

One of my web apps is multi-tenant app - meaning it supports many organisations under the same app. Their data is segregated.

During the authentication I store their details;

B4X:
req.GetSession.SetAttribute("registered", True)
req.GetSession.SetAttribute("fullName", fullName)
req.GetSession.SetAttribute("orgId", orgId)
req.GetSession.SetAttribute("username", username)

Then later when they make another request I can retrieve these attributes and apply to SQL queries etc

B4X:
Dim fullName As String = req.GetSession.GetAttribute2("fullName", "")
Dim orgId As String = req.GetSession.GetAttribute2("orgId", "")
 
Upvote 0
Top