react-redux passport-google react-router: success callback url redirects to '/' and redux state resets as page reloads - react-redux

I'm building a page, where a user is allowed to fill in a form, but in order to submit it, he has to login. I am only allowing Google Login (passport-google-oauth2). After filling the form, if he has to submit it, and is not logged in, a dialogue box appears, where he is asked to login by Google.
After a successful login, the the google callback route, in I am redirecting him to '/' (homepage)
Callback route code
app.get('/auth/google/redirect',passport.authenticate('google'),
(req,res,next)=>{
res.redirect('/');
});
I am using react router to handle routing on client side
However, when he is redirected to '/' the page refreshes and my redux state resets. (My form data is stored in redux state and it becomes empty again).
Here are my 2 questions.
1) How can I redirect to a page after a successful login without the page refreshing (redux not losing it's state)
2) How can I know from which route I came from and redirect back to the same route? ie: if I called google login from '/form', then I should be redirected to '/form' and so on
Thanks in advancce!

Related

how to prevent browser back to display login page after logged in in inertia js

How to prevent browser back to display login page after logged in in inertia js?
if you login to inertia demo CRM with this url :
Demo Inertia Js : https://demo.inertiajs.com/login
afetr loginning you can see login page by browser back again.
How can I solve it?
Thanks
If you have successfully logged in and you go back using history back, the only thing you are doing is previewing how the login page looked like. You aren't doing any request, just visiting your browser's history.
If you go back and refresh the page, you can see that you are now being redirected to the dashboard, which means that you did a request and the server detected you are logged in. As you are logged in, redirects you from /login (guest) to /dashboard (auth).
So in my opinion there is nothing to solve, you don't need to prevent browser back to display login page, you need a middleware to redirect you out from guest routes if you are logged in, that is it.
Docs:
History.back() - MDN docs
RedirectIfAuthenticated middleware - PingCRM
Reason login page is rendered even logged in is because login page rendering request is not sent to the server.
In the login form submission, you can put additional option to replace the page.
Browser back button will also replace the page and eventually, rendering request gets sent.
Inertia.post('/login', {
email: email,
password: password
}, {
replace: true
})

Middleware not working correctly after hitting browser 'go back' button for the first time Laravel

I have a login page and when the user enters their login and password correctly, I redirect them to a dashboard page. I am using Laravel guest middleware, so when a logged in user tries to go to login page, they get redirected to dashboard. Everything works fine, except when the user logs in and gets redirected to dashboard, if they hit the browser back button, the login page still shows. It only goes away after a refresh. How can I fix this problem?
Here's my routes:
Route::group( ['middleware' => 'guest' ],function()
{
Route::get('/', 'MainController#index');
Route::get('/loadLogin','MainController#loadLogin');
});
Surely this is because the browser caches the previous page? If the user attempted to POST or GET data to the page, the server would redirect them to loadLogin. The only thing the user is really seeing is the client-side code their browser saved, hence why the refresh fixes it.
JCode said check if the user is logged in the controller but controllers are not the place to be checking this.
Just check if user is logged in inside the index of MainController, if so - redirect to loadLogin.

CSRF _token value refresh in a login form

I have a login form submitted with Ajax. In one specific case, after the user logs in, I need to log them out with Auth::logout() and display an additional modal box. All of this happens with no page reload.
When the login modal is opened and submitted again, I get a Token mismatch error. The reason why this happens is because the logout uses Session::flush(). After this the _token Session variable is refreshed, while the _token input in the login form stays the same (because of the mentioned lack of page reload).
How can I refresh the CSRF _token in the login form, so it matches the one refreshed in the Session in a secure way?
When your login is submitted via ajax, your server is presumably sending back some kind of response to let the browser know the login was successful. You should send back the new CSRF token with this response, so that you can update the form client-side (with javascript).
To provide any more detail (how to update form fields, for example) we would need to see more of your code.

JQuery Mobile - AJAX URL redirect breaks pageinit

TL;DR - AJAX URL redirect breaks 'pageinit' and any subsequent 'pageinit's that occur after the redirect
(see https://stackoverflow.com/a/14469041/2133816 for information on pageinit)
How to recover from this?
In Detail:
My mobile site has user accounts, so when a homepage button is selected that requires a user login it will first try to access the site in question...
m.smellyeggs.com/stuff?id=2497349187324&tr=454543522525
But once it is detected that the user has not logged in, the site reroutes the user to...
m.smellyeggs.com/login
Sadly, this event breaks functionality for all pageinit events:
$(document).on('pageinit', function(){
// things are done here
});
So after a redirect event occurs, no more pageinit events are detected, and unfortunately my data is loaded from the cache.
How can I recover from a "broken" pageinit, specifically occurring after an AJAX redirect.
Thank you!

redirect to original page upon ajax login

I am trying to implement the following scenario:
1) A user is not logged in, and on page foo.
2) The user clicks login on that page which shows a lightbox.
3) The user logs in via the lightbox.
4) The page, foo, is refreshed upon login success.
Steps 1-3 are done. In step 4 right now, the user is always redirected to their profile page upon login IF the user is logging in via that lightbox method.
I have login redirects to pages if the user is trying to GET some page which requires login access. Then the user logs in and is automatically redirected to that page the user was trying to go to. But in the above scenario, the user is already on a page, and I want to refresh that page upon login.
Any tips on how I can implement the above?
Thanks.
An answer to your question would be to redirect to the current page instead of "refreshing" it :
redirect_to request.url
But, be aware that this will issue a REDIRECT, which means a 302 status code (rails default). You can specify the status code you want with :
redirect_to request.url, :status => 301
Hope this helps!
Make the login form in the lightbox submit via ajax (rather than the usual post). In Rails 3 just use :remote => true in the form. In the ajax response run some javascript that refreshes the page: window.location.href = window.location.href;
Note that there are many ways to accomplish your task, and this is just one option. I've found it to work well for me on my site: http://www.tmatthew.net/blog
One option you can easily use in many circumstances is:
redirect_to :back
This just sends the browser back to the page it originated the request from. This is handy when you have a form that appears in multiple places and you want the person submitting the form to be taken back to whatever page they happened to submit it from rather than always redirecting to a certain page (like you're describing).

Resources