react-admin: How to suppress multiple 401 notifications? - react-admin

I'm using react-admin and I have a page that makes api calls via the dataProvider to /posts, but there is also a custom UserMenu that fetches /user, and the authProvider also runs an AUTH_CHECK that fetches /session.
On the first page load, all three requests are issued and all three correctly return 401 unauthorized, but then I also get three pop-up notifications with the same message: "Request failed with status code 401".
Is there a way to either
show this message only once
not show any of these messages at all
Also, is there a way to customize the text of this message to something more user friendly?

Related

React-admin: don't show warning message on checkAuth failure

I have been doing the upgrade from React-Admin v2 to v3 for a while now. I'm almost finished, but there's something that I would like fine-tune.
Using v2, when user comes to our website at domain.tld without a token, Promise is rejected and user is redirected to domain.tld/#/login. No error/warning messages are shown.
Using v3, if the same situation happens, Promise is also rejected and user is redirected to the login page, but there comes the ra.auth.auth_check_error warning message - and twice!
I understand the logic, it may be useful most of the cases to display the warning message. For example if non-logged user tries to access domain.tld/#/settings, the user should be redirected to the login page and notify, that "you must login first". But if user comes to the website first time ever, he gets redirected to the login page as supposed to, but in this case that warning message should not be displayed. In this case the user is most likely known that he/she must login before seeing any content, so there's no point to warn the user since he/she didn't do anything wrong.
I don't know how easily this could be solved. Maybe just removing the notify completely? How that can be done?
Maybe some exception would be the other solution, that if user tries to access the root, domain.tld/#/, then the warning message would not be shown. And with other routes it would be shown. How this could be done?
Can the functionality what happens after Promise.reject() be overwritten somehow?
My checkAuth in authProvider.js:
checkAuth: () => localStorage.getItem("token")
? Promise.resolve()
: Promise.reject(),
In your Promise.reject you can add a string that will be presented to the user when their authorization fails.
checkAuth: () => localStorage.getItem("token")
? Promise.resolve()
: Promise.reject("You are not authorized to view this")
You could theoretically return a different message based on what page you're on but you don't have that info in the checkAuth.
You could also build your own login page and filter the messages so that no toasts popup for the login page.

Force GET_ONE request when navigating to Show page

As I believe is common in many APIs, ours returns a subset of fields for a record when it is part of a List request, and more details when it is a single-record request to its Show endpoint.
It seems that react-admin attempts to avoid doing a second request when loading a Show page (possibly re-using the record data from List?), which results in missing data. Refreshing the page fixes this, but I'm wondering if there is a setting that will force a GET_ONE request on every Show page load.
There are no setting for that. However, this should be achievable with a custom saga which would listen to LOCATION_CHANGE action (from react-redux-router) and dispatch a refreshView action (from react-admin) when the new location pathname ends with /show.
Edit: however this is very strange. We only use the data we already got from the list for optimistic display but we still request with a GET_ONE when navigating to a show page from the list. Do you have a codesandbox showing your issue?

pass url from view to controller on PaypalReturnView

I have almost got my mobile web app sorted, I just need to get the return url from the return page after finishing my paypal transaction, I need the url as it has the token and payerid so I can finish off the payment.
I have seen that I can get the Request.Url.AbsoluteUri, but is there anyway I can decode this so it is in a normal string format, that I can then extract the token and payerid, also is how do I get this information on the view loading, opposed to doing it on a button press..
Thanks

Best Practice for "Evaluate Now, Respond Later"?

Suppose I'm building a login system. The user enters a username and password into a field and it is sent via HTTPS to the server, which validates the login before the page loads. If a bad password is sent, the login obviously fails immediately, but one would want the error message to be displayed later in the page, near the login box.
The obvious solution is to set a global flag and have the login box check it and add the error message if necessary, but my understanding is that global variables are best avoided. Is there another straightforward method of achieving this functionality?
For a non-AJAX login page, it is common practice to redirect the user browser to the login page with an extra query parameter in the url, In pseudo-code, here is the login validation controller code segment:
success = checkLogin(username,password)
if (success == false)
redirect('http://example.com/login?failedlogin=true')
The login page controller would be responsible for detecting this query param and telling the view code to display a failure message. I don't believe the term 'global flag' applies to this practice so it should meet your requirements.
If the login page uses Ajax, the Javascript on the login page takes the results of the AJAX call and updates the appropriate DOM elements with the failure message.

Facebook Connect: User has logged in and given permissions, now what?

So i've been trying to get FB Connect working on my site, simply for login and authentication, using the Javascript SDK and following the code at:
https://developers.facebook.com/docs/guides/web/
So the button appears, i click it, a dialog pops up, i click that, presumably my site now has permission to know who i am...
Then what? The guide goes on to saying all the stuff I can access through the Facebook API, all the cool things about permissions, but presumably i need the user's ID or access token or something to get at this stuff. How is that given to me? left as a attribute on one of the elements? Left in a Javascript variable somewhere? Given as an argument to some callback? Thrown high into the heavens for me to receive via satellite downlink?
This is probably incredibly simple, but for the life of me i have not been able to figure it out. Facebook's tutorials have failed me, and so has Google. I want to get this in the Javascript, so I can immediately fill in form-data using the user's Facebook name, put a picture, etc. etc., and presumably send this all back to the server so the server can validate with Facebook that the data is real.
I'm assuming you're using the Login button? https://developers.facebook.com/docs/reference/plugins/login/
If you simply want form info, check out the registration plugin - https://developers.facebook.com/docs/plugins/registration/
However, to answer your question, make an API call to /me. For example:
FB.api('/me', function(user) {
if(user != null) {
// The user object now contains info about the logged in user
}
});
You should subscribe to the auth.login event and wrap the above API call in the successful response, i.e.:
FB.Event.subscribe('auth.login', function() {
// JS to run if when the user logs in, for example, the code snippet above
});