Preserve URL hash between HTTP redirects - javascript

I am working on an offers site and we are developing a functionality to send the offers listed in our page in an email to the end user.
In our webpage there is a section called Offers with the enclosed tag id as Offers.
When the user clicks the link in url
Link: https://www.test.com/service#Offers
Expectation: The service page should be loaded and it should scroll to Offers sections
This is working when the user is already signed in.
But when the user is not signed in , the user request will be forwarded to the sign in page and then it reverts to the /service.
But this time, # anchor is missing in url
https://www.test.com/service
and so scrolling is not happening.
Please help me how can i solve this issue.
is it advisable if i can replace it with https://www.test.com/service?Offers
Please advice.

Your problem is related to your frontend which doesn't send the anchor in the URL to the backend because it represents a frontend state (this is by design and cannot be changed). So when the login page keeps the return URL in session and then redirects after login, the redirection doesn't contain the anchor because the backend simply doesn't have it.
If your login is based on HTTP redirects, the web doesn't offer any native solution to your problem.

Related

How to track user when redirecting through different pages

I have currently made a website with a login page and a home page(with nodejs, js and html)(client side will send the entered user name and pass to server, when its checked the server will send a reply, based on which the client side redirects to the home page). When redirecting a user from one page to another how can the server track which user is connected on the client side when navigating through different pages ?
Edit:
https://www.youtube.com/watch?v=GihQAC1I39Q&ab_channel=WebDevSimplified
After reading the replies and googling about it, reached this video which was exactly what i needed.
Generally speaking
If your page uses redirects, the redirecting page becomes the landing page's referrer. For example, if you've changed your site so that index.html now redirects to home.html, then index.html becomes the referrer for home.html. If someone reached your site via a Google search that sent them first to index.html, you won't have any data regarding the Google search.
For this reason, you should place the Google Analytics tracking code on the redirecting page as well as on the landing page. This way, the redirecting page will capture the actual referrer information for your reports.
Note, some browsers may actually redirect before the JavaScript call from the code can be made.
(cf. https://support.google.com/analytics/answer/1009614?hl=en)

Landing Page, preventig access by direct url or to the user coming outside the facebook ads

I am trying to redirect all the users they are not being redirected from Facebook Ad to a regular static page instead of an Landing Page. The Landing Page should be visible just to the users they access it through a Facebook ad. If they are trying to access this landing page with direct URL or coming from different sources than they should be redirected to a different static page. Can be done just with javascript? and without modifying the .htaccess?
You could try to read out document.referrer in js and act accordingly (https://developer.mozilla.org/en-US/docs/Web/API/Document/referrer)
This should contain the page the user is comming from!
Also see here: How to get HTTP-Referer via JS | Jquery?
You may also take a look into the Referer HTTP Header to perform the redirection on the server side: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referer
One of the way to check where the request is coming from is: $_SERVER['HTTP_REFERER']. But According to the official PHP documentation:
"This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted".

Detecting if app loaded from a page

So my app is (mostly) running. This is a corporate app, that our clients will 'install' on their own Facebook page(s), and make available for THEIR clients to run. During the connection process, I use the FB UID to determine their account on our system, connect that UID to the local account ID. I save that connection in our database, so that when the app is triggered from a Facebook account, I use that mapping to access the correct data.
First issue: I want to be able to detect that the app is being triggered from a Facebook page. I went into the App Settings page, and set up the "Page Tab" platform, with a secure URL. But when my app is added to a page tab, that URL is not invoked, but the Secure Canvas URL is invoked. Why? I assumed that was the point of having a Secure Page Tab URL for my app.
But if that doesn't work, is there a way to identify that my app was invoked from the Page Tab instead of some other way? Not as clean, but that would word as well.
I have used the Javascript API to login, and connect the App to a page. This is all working well.
This is answered in the Page Tab App documentation:
When a user selects your Page Tab, their browser will send a HTTP POST
request to your app's Secure Page Tab URL. This request will contain a
Signed Request in the signed_request parameter with fields you can use
to customise the content returned to the user.
If you're seeing the 'wrong' url triggered, make sure you're not accidentally redirecting, either upon receiving the initial POST request from the user's browser, or after using Facebook Login (e.g. if your redirect_uri when opening the dialog is accidentally sending all users to the same URL after login instead of back to where they triggered the flow from)

Facebook Javascript Canvas Login Redirect

I am building a facebook canvas application and want users, when they visit "apps.facebook.com/myApp", to be presented with the appropriate login dialogue immediately instead of showing my canvas page without having been logged in. With all the different login flows, I am confused!
Using javascript, how do I do this? Do I set the default canvas page to be a page with a script with a redirect url in it?
You should not immediately present them with the login dialog, it is best practice to show some intro screen with information about your app first. Else there is only a small authorization information and no use will really know what the app is about. Then let the user click on a "Login/Use/Start/..." button to present the authorization popup with the FB.login function of the JavaScript SDK:
https://developers.facebook.com/docs/reference/javascript/FB.login/
If you really want to require user authorization right at the beginning, user the PHP SDK:
https://github.com/facebook/facebook-php-sdk
See "$facebook->getLoginUrl()" - just redirect to the resulting URL if the user is not logged in.

Javascript Cookie related issue

I am facing a problem with a backend using cookies .
The UI technology is JSP and i have login to the application and performed some operations.
At that time i copied the url and opened a new tab and pasted the copied url
In this case i would like the user to be redirected to login page.
This is what i understood from your question :
Your webserver uses cookie to store user session info , you want the user to be redirected to login page when he copy pastes the link onto a new tab .
This is what happens when you do what u said u did :
When a user requests a url from a new tab with an established session in the previous tab , the cookie is sent along with this newtab request hence the webserver renders the page to this request since it is legitimate.
However , the same will not work if you fire the request using a different browser.[In this case the backend will redirect the user to login page (if you've identified the url is without cookie info and programmed a redirect to login page in your server code).]
I don't think this can be done using cookie based session handling .
Please do check this link How to differ sessions in browser-tabs?

Categories

Resources