redirect after login laravel

Redirect after login laravel

On beginning i will answer yes i have changed «$redirectTo» for other route after login, register.\nBut when i click again on login link i’m redirecting on «\/home». thing is i have to make a route in web.php file because without it i got exception. \n

By doing\n php artisan route:clear \n

»’Route::get(‘\/’, [‘uses’=>’IndexController@getIndex’,’as’=>’app.index’]);»’ \n

Because the login page has the guest middleware, that middleware redirects to \/home directory. \n

You have to change the App\\Http\\Middleware\\RedirectIfAuthenticated.php \n

Change from this: \n

I have just come across this, thanks for the answer as it has solved my problem. \n

I have changed the line suggested in the best answer, but I was just wondering if this was the best way to do it. Should it be overridden in my code somewhere; so as to not have it revert back in a future update to the framework perhaps? \n»,»bodyInMarkdown»:»I have just come across this, thanks for the answer as it has solved my problem.\r\n\r\nI have changed the line suggested in the best answer, but I was just wondering if this was the best way to do it. Should it be overridden in my code somewhere; so as to not have it revert back in a future update to the framework perhaps?»,»replies»:[],»user»:<"id":779,"username":"jonnyhocks","avatar":"\/\/www.gravatar.com\/avatar\/66575472d4e93710a2c7550db592a1d1?s=100&d=https%3A%2F%2Fs3.amazonaws.com%2Flaracasts%2Fimages%2Fforum%2Favatars%2Fdefault-avatar-5.png","experience":<"award_count":"1","level":4,"points":"15,515","pointsUntilNextLevel":"4,485">,»achievements»:[],»reported»:null,»profile»:<"github":"","twitter":"","full_name":null,"website":"","bio":null>,»dateSegments»:<"created_diff":"7 years ago">>,»likes»:[],»created_at»:»2018-01-28T13:07:49.000000Z»,»links»:<"delete":"\/discuss\/replies\/395204","like":"\/discuss\/replies\/395204\/likes","best_answer":"\/discuss\/conversations\/71688\/best">,»best_answer»:false,»dateSegments»:<"createdDiff":"3 years ago">>,<"id":406179,"conversation_id":71688,"body":"

Because the login page has the guest middleware, that middleware redirects to \/home directory. \n

You have to change the App\\Http\\Middleware\\RedirectIfAuthenticated.php \n

Change from this: \n

Источник

Laravel redirect back to original destination after login

This seems like a pretty basic flow, and Laravel has so many nice solutions for basic things, I feel like I’m missing something.

A user clicks a link that requires authentication. Laravel’s auth filter kicks in and routes them to a login page. User logs in, then goes to the original page they were trying to get to before the ‘auth’ filter kicked in.

Is there a good way to know what page they were trying to get to originally? Since Laravel is the one intercepting the request, I didn’t know if it keeps track somewhere for easy routing after the user logs in.

If not, I’d be curious to hear how some of you have implemented this manually.

26 Answers 26

For Laravel 5.3 and above

For Laravel 5 up to 5.2

On auth middleware:

For Laravel 4 (old answer)

At the time of this answer there was no official support from the framework itself. Nowadays you can use the method pointed out by bgdrl below this method: (I’ve tried updating his answer, but it seems he won’t accept)

For Laravel 3 (even older answer)

You could implement it like this:

Storing the redirection on Session has the benefit of persisting it even if the user miss typed his credentials or he doesn’t have an account and has to signup.

This also allows for anything else besides Auth to set a redirect on session and it will work magically.

redirect after login laravel. Смотреть фото redirect after login laravel. Смотреть картинку redirect after login laravel. Картинка про redirect after login laravel. Фото redirect after login laravel

Laravel >= 5.3

The Auth changes in 5.3 make implementation of this a little easier, and slightly different than 5.2 since the Auth Middleware has been moved to the service container.

Modify the new Middleware auth redirector

Change the handle function slightly, so it looks like:

TL;DR explanation

The only difference is in the 4th line; by default it looks like this:

Since Laravel >= 5.3 automatically saves the last «intended» route when checking the Auth Guard, it changes to:

That tells Laravel to redirect to the last intended page before login, otherwise go to «/home» or wherever you’d like to send them by default.

redirect after login laravel. Смотреть фото redirect after login laravel. Смотреть картинку redirect after login laravel. Картинка про redirect after login laravel. Фото redirect after login laravel

I found those two great methods that might be extremely helpful to you.

You can apply this filter to the routes that need authentication.

What this method basically does it’s to store the page you were trying to visit and it is redirects you to the login page.

When the user is authenticated you can call

and it’s redirects you to the page you were trying to reach at first.

It’s a great way to do it although I usually use the below method.

You can check this awesome blog.

You may use Redirect::intended function. It will redirect the user to the URL they were trying to access before being caught by the authenticaton filter. A fallback URI may be given to this method in case the intended destinaton is not available.

In post login/register:

Change your LoginControllers constructor to:

It will redirect you back to the page BEFORE the login page (2 pages back).

redirect after login laravel. Смотреть фото redirect after login laravel. Смотреть картинку redirect after login laravel. Картинка про redirect after login laravel. Фото redirect after login laravel

I have been using this for a while on my language selector code. As long as you only need to go back by just 1 page it works fine:

It ain’t the most powerful solution out there but it is super-easy and can help solve a few puzzles. 🙂

this will redirect you to default page of your project i.e. start page.

redirect after login laravel. Смотреть фото redirect after login laravel. Смотреть картинку redirect after login laravel. Картинка про redirect after login laravel. Фото redirect after login laravel

redirect after login laravel. Смотреть фото redirect after login laravel. Смотреть картинку redirect after login laravel. Картинка про redirect after login laravel. Фото redirect after login laravel

For laravel 5.* try these.

Laravel 3

I tweaked your (Vinícius Fragoso Pinheiro) code slightly, and placed the following in filters.php

And then within the my AuthController.php:

Notice that the ‘redirect’ session data is reflashed if there is a authentication issue. This keeps the redirect intact during any login mishaps, but should the user click away at any point, the next login process is not disrupted by the session data.

redirect after login laravel. Смотреть фото redirect after login laravel. Смотреть картинку redirect after login laravel. Картинка про redirect after login laravel. Фото redirect after login laravel

redirect after login laravel. Смотреть фото redirect after login laravel. Смотреть картинку redirect after login laravel. Картинка про redirect after login laravel. Фото redirect after login laravel

redirect after login laravel. Смотреть фото redirect after login laravel. Смотреть картинку redirect after login laravel. Картинка про redirect after login laravel. Фото redirect after login laravel

For Laravel 8

Following approach works for me for Laravel 8.

Controller based approach

Pre-login

The intended url will be stored in the session at create :

Post-login

Upon successful login, in case a intended url is available in session then redirect to it otherwise redirect to the default one :

In Laravel 5.8

in App\Http\Controllers\Auth\LoginController add the following method

in App\Http\Middleware\RedirectIfAuthenticated replace » return redirect(‘/home’); » with the following

Larvel 5.3 this actually worked for me by just updating LoginController.php

For Laravel 5.5 and probably 5.4

In App\Http\Middleware\RedirectIfAuthenticated change redirect(‘/home’) to redirect()->intended(‘/home’) in the handle function:

in App\Http\Controllers\Auth\LoginController create the showLoginForm() function as follows:

This way if there was an intent for another page it will redirect there otherwise it will redirect home.

I am using the following approach with a custom login controller and middleware for Laravel 5.7, but I hope that works in any of laravel 5 versions

inside controller login method

If you need to pass the intented url to client side, you can try the following

First, you should know, how you redirect user to ‘login’ route:

redirect after login laravel. Смотреть фото redirect after login laravel. Смотреть картинку redirect after login laravel. Картинка про redirect after login laravel. Фото redirect after login laravel

Laravel now supports this feature out-of-the-box! (I believe since 5.5 or earlier).

Add a __construct() method to your Controller as shown below:

After login, your users will then be redirected to the page they intended to visit initially.

You can also add Laravel’s email verification feature as required by your application logic:

The documentation contains a very brief example:

It’s also possible to choose which controller’s methods the middleware applies to by using except or only options.

Example with except :

More information about except and only middleware options:

if you are using axios or other AJAX javascript library you may want to retrive the url and pass to the front end

you can do that with the code below

This will return a json formatted string

This worked for me in laravel 8:

Add this to your LoginController.php:

It will redirect you back 2 times, so to the page you were before login.

Credits goes to @MevlütÖzdemir for the answer!

redirect after login laravel. Смотреть фото redirect after login laravel. Смотреть картинку redirect after login laravel. Картинка про redirect after login laravel. Фото redirect after login laravel

✓ Tested on Laravel 7.x

Go to LoginController.php and add the following code:

This function will set the url.intended session variable. Laravel uses this variable to look for the page in which the user will be redirected after login.

redirect after login laravel. Смотреть фото redirect after login laravel. Смотреть картинку redirect after login laravel. Картинка про redirect after login laravel. Фото redirect after login laravel

redirect after login laravel. Смотреть фото redirect after login laravel. Смотреть картинку redirect after login laravel. Картинка про redirect after login laravel. Фото redirect after login laravel

Here is my solution for 5.1. I needed someone to click a «Like» button on a post, get redirected to login, then return to the original page. If they were already logged in, the href of the «Like» button was intercepted with JavaScript and turned into an AJAX request.

In the Authenticate middleware (the handle() function), add something like this at the start:

Then, in the LikeController or whatever controller you have that handles the URL for the button pushed on the original page:

This method is super simple, doesn’t require overriding any existing functions, and works great. It is possible there is some easier way for Laravel to do this, but I am not sure what it is. Using the intended() function doesn’t work in my case because the LikeController needed to also know what the previous URL was to redirect back to it. Essentially two levels of redirection backwards.

Источник

I have a page with a some content on it and a comments section. Comments can only be left by users who are signed in so I have added a login form to the page for users to sign in with (this only shows if they are not already logged in).

The problem I have is that when the user signs in they get redirected back to the home page and not the page they were previously on.

I have not changed the login method from the out of the box set-up.

Can anyone suggest a simple way to set the redirect url. My thoughts are that it would be good to be able to set it in the form.

14 Answers 14

Solution for laravel 5.3:

In loginController overwrite the showLoginForm() function as this one:

It will set the «url.intended» session variable, that is the one that laravel uses to look for the page which you want to be redirected after the login, with the previous url.

It also checks if the variable has been set, in order to avoid the variable to be set with the login url if the user submit the form with an error.

For Laravel 5.5, following code worked for me by just updating LoginController.php

redirect after login laravel. Смотреть фото redirect after login laravel. Смотреть картинку redirect after login laravel. Картинка про redirect after login laravel. Фото redirect after login laravel

Please use redirect()->intended() instead in Laravel 5.1

For Laravel 5.3

inside App/Http/Controllers/Auth/LoginController add this line to the __construct() function

So the full code will be

It works like a charm for me i’m using laravel 5.3.30

For Laravel 5.4, following code worked for me by just updating LoginController.php

redirect after login laravel. Смотреть фото redirect after login laravel. Смотреть картинку redirect after login laravel. Картинка про redirect after login laravel. Фото redirect after login laravel

The Laravel 5.6, When user insert wrong credentials then login page will reload and session([‘link’ => url()->previous()]); will take login URL in link variable. So the user will redirect to a login page again or redirect to /home if login success. So to avoid these below code working for me! After that no matter how much time user insert wrong credentials he will redirect after login to exactly where he was before login page.

Update or overwrite public function showLoginForm() in LoginController.

Источник

Laravel 5.5: how to stop redirect to previous page after login

I am using Laravel default authentication When user loges in I redirect him/her to /dashboard instead of /home

changing this line in RedirectIfAuthenticated

now the if user hits another url that requires authentication lets say /abc it will redirect user to login but as soon as he/she gets login it takes user to /abc instead of /dashboard (As per my requirement)I want it to always take user to /dashboard whenever he/she loges in irrespective of previous page he/she was trying to hit

Anybody got any idea which function is to be overwritten or anything.

redirect after login laravel. Смотреть фото redirect after login laravel. Смотреть картинку redirect after login laravel. Картинка про redirect after login laravel. Фото redirect after login laravel

4 Answers 4

Intended url is saved in the session. So if we remove the value from the session, user will be redirected to the default route always.

Now that you added the event, artisan can create the listener with php artisan event:generate (https://laravel.com/docs/5.8/events#generating-events-and-listeners). In the handle method of the created App\Listeners\LogSuccessfulLogin file, unset the session value url.intended as below:

You have 2 choices, both of them work equally well. Option one:

Open app/Http/Exception/Handler.php and override the unauthenticated() method like this:

This way you wont be saving in the session the previous URL of the user.

Open your LoginController and override the sendLoginResponse() method like this:

This way you’ll always be redirecting to the redirect path declared in the controller when the user logs in.

redirect after login laravel. Смотреть фото redirect after login laravel. Смотреть картинку redirect after login laravel. Картинка про redirect after login laravel. Фото redirect after login laravel

First figure out exactly what you are trying to accomplish and toy around with it a bit. Looking back at what I’ve wrote, that last return redirect()->back(); seems odd! At this point back() should be pointing to /login which is accessible only as guest but then the Laravel’s RedirectIfAuthenticated middleware kicks in and saves the day!

Most likely there is way more elegant solution to it. Maybe something with the Redirect::intended($default)->getTargetUrl(); or something.

Anyway hope that helps you..

..in class App\Http\Controllers\Auth\LoginController

.. and the handle method of my class RedirectIfAdministrator

Источник

Laravel redirects to login after authentication

PROBLEM

I’m busy with my first Laravel app and although I see the benefits of the way things are written, I’m having a hard time understanding some of the behaviour.

When I try to login I get redirected to the login page. It seems like the user authenticates correctly, but it redirects to the login page regardless.

WHAT I HAVE

My users table looks like this:

This is my routes file:

WHAT I’VE DONE

I’ve read various questions on stackoverflow but for some reason I can’t get login to work.

QUESTION

How do I get it to redirect to the dashboard after login? What am I missing?

4 Answers 4

You need to add this prop in your user model.

Remove the authenticated() method from login controller.

And handle the redirection based on user type inside RedirectIfAuthenticated middleware

Extra step inside App\Exceptions\Handler class

Add this method if you have different login pages for each user type

redirect after login laravel. Смотреть фото redirect after login laravel. Смотреть картинку redirect after login laravel. Картинка про redirect after login laravel. Фото redirect after login laravel

I have had the same problem yesterday night. I have found that the issue is Auth::attempt() does not persist the login so when a user is logged in successfully, it was dropped in the session after redirect to /home.

Below link provided the answer to solve it: Laravel Auth:attempt() will not persist login

(Field user_id is auto increment key in my Schema for ‘users’ table)

redirect after login laravel. Смотреть фото redirect after login laravel. Смотреть картинку redirect after login laravel. Картинка про redirect after login laravel. Фото redirect after login laravel

for authentication in laravel first use php artisan make:auth command

and in your web.php add Auth::routes(); only which have all authentication routes

and do not forget to call auth class like use Auth;

or you can check authentication in your routes by add middleware auth

redirect after login laravel. Смотреть фото redirect after login laravel. Смотреть картинку redirect after login laravel. Картинка про redirect after login laravel. Фото redirect after login laravel

Not the answer you’re looking for? Browse other questions tagged php laravel-5.7 or ask your own question.

Linked

Related

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.9.17.40238

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *