We have a mobile app (built using phonegap) accessing data from a REST API. This rest API has an authorization token specific to us - provided by them after registering with them. Now, we are using the API access URL with the token issued.
E.g. (http://api.something.com/cities/buildings/auth_token=blahblah). We are able to get the data through an AJAX request - which obviously has a SUCCESS and an ERROR callback function.
I understand now that there is an expiry for this session, and this is where I am stuck. My AJAX request fails and the control goes to the ERROR callback after a few mins. In my whole app, I do not have a login screen (I don't want to have one).
The AJAX request is being called on a button click. So, when I click this button for the first time, my data comes out successfully. But when my session expires, I get an error message (the message that I defined in my AJAX error callback).
Here, if I click the same button again, the session gets established and I am getting my data again.
Shortly ->
Step 1. Click the button -> Data retrieval successful
Step 2. Session expires
Step 3. Click the button -> Error message appears
Step 4. Click the same button again -> Data retrieval successful
Now my question is how to handle this scenario? Is my understanding of this particular REST API correct?
My expectation is to eliminate Step 3, Is that possible? Any workaround? Or should the user follow all these steps for sure, everytime a session expires?
Related
I have a web page which allows users to upload and process specific files. After an user uploads some files, after clicking the 'Process' button an ajax call is being sent to a backend service. In the beforeSend function there is an overlay applied to the screen and a spinner is displayed. When the success function is triggered, then the overlay is removed and a toast notification is being shown like 'Files were processed!'
My goal is to somehow show a progress status for each file based on specific checkpoints in the backend service.
Let's say that the backend service when called does following tasks: parse file, map to specific format, send data to database A.... and in the end it sends back http status 200 and a JSON like
{
"status":"Success",
"message": "File X was processed"
}
Now what I want is that instead of just getting an overlay and disabling the whole page until the success event is triggered, to have a progress bar which is updated for each file based on the exact step where the backend has reached.
For instance, for file A, I would like to see below transitions: 5 % Parsing file, 10 % Mapping file...90% sending data to database, 100% processed.
Is this somehow achievable?
There are few points that you need to look into.
Usually in production code, we need to have timeouts. If you are making an ajax call to the backend API, there will be a timeout associated with that api call. Suppose if the timeout is more than 2 mins, then it will send you a 504 Gateway timeout error.
To overcome this and to implement the functionality which you want, you can have any DB(lets consider SQL server). In your SQL server, make a table:
Process_Table
With schema:
Process_id( Will store the process id/name )
Percentage( Will store the percentage )
At_step ( Parsing, Mapping, Sending to DB etc)
Using Javascript(Or any framework of your choice), use setInterval(), to make check_process() api calls. For check_proceess, you can pass in the process_id, and check against the db. For interval, you can set it to 5 seconds. So that every 5 seconds the call is made.
You can read the response of those API calls and do your processing.
An HTTP request consists of a request and a response. There's no direct way to get status updates beyond the onprogress event which would let you see how much data has been transferred. This is useful for determining how much of the data has been sent to the server, but not so much for how far the server has got with working with that data.
You could store progress in a database and poll a webservice to read the most recent status.
You could also have the server push updates to the client using Websockets for bi-directional communication.
A rough outline for such a system might look like:
Open a Websocket
Send files with Ajax
Get server generated ID back in HTTP response
Pay attention to messages coming over the Websocket that mention that ID
You could also look at doing the whole thing over Websockets (i.e. upload the files that way too). A quick Google search turns up this library for uploading files to a Websocket service hosted on Node.js.
When a user scans QR code in our web page and makes purchase with it (using a 3rd party application), I want to hide that QR code and process the purchase.
I got an API to check whether the transaction was successful or not. My first thought was sending a request to that API every 2 seconds to check the transaction (terrible idea?). But they told us to create something called a "hook" and they would subscribe to it. What's that? How should I implement that?
Hook may generally mean webhook, here is the wikipedia description.
Webhooks are "user-defined HTTP callbacks". They are usually triggered
by some event, such as pushing code to a repository or a comment being
posted to a blog. When that event occurs, the source site makes an
HTTP request to the URI configured for the webhook.
Hook/Webhook
Think a hook as an endpoint, where a user can notify you / giving you information.
The way it work is like a user accessing a url and sending params
your-url.com/hook?order_id=123&status=complete
Warning: Example code, generally a good hook should provide authentication method.
That way your server can interpret that information as
order with id 123 has been completed
That was a basic implementation.
Other easier analogy is:
a user accessing a url and input a form and press submit. To which url the data is sent, that url can also be called hook.
Basically they are telling you to implement a service that they would call when a transaction ocurred successfully, instead of you going to them.
I have a website and currently I am handing the timeout on client side that is using Javascript, so that I no request is being made I log the user out, but I have seen people on SO suggesting the same approach , and I see a big lapse in it, suppose a user has 2 tabs open.
Tab 1:
www.MYSITE.com/welcome.php
Tab 2:
www.MYSITE.com/edit_profile.php
Now if user is on Tab 2 and he is editing the profile there, Tab 1 is idle that means user will be logged out/shown warning (the way you are handling Idle time).
So that doesn't seems to be consistent, in my thinking it should be on server side, is my approach correct?
One way could be,
In case of Ajax, Whenever you send any request to server, on server you can check if session expired using isset($_SESSION['variable']), send a response SESSION_TIMEOUT & then in ajax callback, check for this response.
If this is found, show user a message 'SESSION expired!' & redirect to initial page (may be login page).
[Function Summary]
The screen is redisplayed by Callback function of JavaScript when click the LIKE button.
[Symptom]
Acess sites displayed the Like button using the HTTPS protocol.
Click The Like button, then Comment column displays at div tag and processing stops.
We expected below results.
When the Like button is clicked,
the information are asynchronously transmitted to Facebook site and Callback functions of JavaScript are called
and then each processing are executed.
Callback function : FB.Event.subscribe('edge.create', function(response) { -- each processing --})
[Question]
Error Message "407 Proxy Authentication Required" displays when we access following page.
https://s-static.ak.facebook.com/connect/xd_arbiter.php
(xd_arbiter.php)
I consider this error caused by follwing reasons.
- The information is not returned from 'xd_arbiter.php.'
- Proxy-Authorization is not granted.
Please let me know workaround for avoiding this error.
[Environment]
Protocol:https
Browser:ForeFox15.0.1
Application Server:WebSphere Application Server 7.0.0.11
OS:Windows2003 Server
I want to add authentication to existing application that uses Ajax and I am thinking of the best way to do it.
Here is a typical scenario:
First request - ask for a login, proceed to the first screen. This works well.
Session expired.
Since I do ajax and send 2 requests per action (one actual request and one that records the action for statistics/audit) it could happen on one of those. When it happens the app shows me the authentication screen and after successful authentication user is forwarded to the previous request.
The problem here is that I don't want a forward, I want my custom Ajax/javascript handling to get the response and handle it. Is it possible to just pop up a window to authenticate, then when I get the response continue like the session never expired?
I have this in the web.xml :
<login-config>
<auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>/jsp/login.jsp</form-login-page>
<form-error-page>/jsp/login_error.jsp</form-error-page>
</form-login-config>
</login-config>