1

Since, I was not able to find any question like this in SO, I am writing this post hoping this is not a duplicate...

I have a page developed using KnockoutJs which I am using to protect via Laravel Authentication. Basically, I am using Laravel only for the purpose of login/registration and once the user logs/signs in, he is redirected to the KnockoutJS page.

Now lets just say I have a URL,

http://example.com/mypage#q=some+parameters&p=somethingsomething

If I share that URL with one of my friends, it obviously redirects my friend to the Login Page. Now, the login page URL (where he is redirected to) is

http://example.com/login#q=some+parameters&p=somethingsomething

But once he logs in, he is being redirected to

http://example.com/mypage#

Which obviously is not right, because I need the parameters to be there...

My Routes page is as follows,

Route::group(['middleware' => 'web'], function() {
    Route::auth();
    Route::get('/', 'MainController@index')->name('home.index');
});

Route::group(['middleware' => ['web','auth']], function () {
    Route::get('/mypage', 'MyController@index')->name('mypage.index');
});

And my AuthController has the redirectTo Url set

protected $redirectTo = '/mypage';

What change should I do (in AuthController, or in Routes? Or in a MiddleWare) to redirect the user to

http://example.com/mypage#q=some+parameters&p=somethingsomething

after login?

2
  • Kindly search for Redirect::intended() (in laravel documentation), cheers. The intended method on the redirector will redirect the user to the URL they were attempting to access before being caught by the authentication filter. A fallback URI may be given to this method in case the intended destination is not available. Commented Feb 20, 2016 at 19:34
  • @Kyslik It's not redirecting me back to that page... It gets me to http://example.com/mypage# when I login. Does laravel consider the parameters given after # in the url for this method? Commented Feb 22, 2016 at 6:24

2 Answers 2

0

You can use the intended method in Laravel.

eg:

 public function authenticate()
    {
        if (Auth::attempt(['email' => $email, 'password' => $password])) {
            // Authentication passed...
            return redirect()->intended('dashboard');
        }
    }

This will redirect the user to the intended URL to the login page or if there is no intended URL, then to dashboard.

https://laravel.com/docs/5.2/authentication#authenticating-users

Sign up to request clarification or add additional context in comments.

3 Comments

It's not redirecting me back to that page... It gets me to http://example.com/mypage# when I login. Does laravel consider the parameters given after # in the url for this method?
Yes it will consider. I've tested myself and it's working very well.
Thanks for all your support... But laravel I believe doesn't support that... I am finally using Javascript Cookies to help me redirect me back to the old page...Thanks anyways :)
0

Since Laravel's Redirect::intended did not work for me, I am posting the answer to this question myself...

User Jquery/Javscript to create cookies after redirection like this :

(function(){
    'use strict';

    var intended = window.location.hash;
(intended.length > 0) ? jQuery.cookie('intended', '/app' + intended) : jQuery.cookie('intended', '/app');

})();

and make these changes into Laravel 5.2's default AuthController

//Replace the default constructor with this
public function __construct(Request $request) {
    if(isset($_COOKIE['intended'])) {
        $this->setRedirect($request);
    }
}

//Copy paste it anywhere in your AuthController Class
protected function setRedirect($request) {
        $intended = (url($_COOKIE['intended']));
        $url['intended'] = $intended;
        $request->session()->put('url', $url);
}

Hope this helps someone....

Thanks, :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.