user3689341
user3689341

Reputation: 143

Laravel authentication redirect error

I am new to Laravel so please excuse my ignorance. I am going through the beginner tutorials and have gotten stuck on the built in authentication system...

I have created a new app and followed the docs on setting up authentication, I searched through stack overflow and overcame one issue (I had to put the auth routes in the middleware group), however now no matter what I do it redirects to the root "/" path...even when I manually go to auth/logout and then auth/login...can someone please help?

Upvotes: 0

Views: 1953

Answers (2)

mp31415
mp31415

Reputation: 6689

In Laravel 5.2 after running their standard

> php artisan make:auth

let's assume we want to ensure user authentication when going to /admin route.

In the routes.php there will be an entry like this:

Route::group(['middleware' => ['web', 'auth']], function() {
    // Only authenticated users may enter...
    Route::get('/admin', [
        'as' => 'admin', 'uses' => 'AdminController@index'
    ]);        
});

and in AuthController.php an additional method must be added:

class AuthController extends Controller
{
    ...

    public function authenticated()
    {
        return redirect()->intended();
    }    
}

As a result every time when unauthenticated user tries to access /admin URL it will be redirected to some /login page and if authentication succeeds he will be able to access /admin page.

A few points to notice in the code above:

  • both web and auth middleware groups are required (auth without web won't have session support and as a result url.intended is not saved in the session and the whole redirect mechanism does not work)
  • the method name in AuthController is authenticated and not authenticate mentioned in Laravel documentation (it's called once authentication is verified)

Upvotes: 1

Anees Saban
Anees Saban

Reputation: 605

I think I misunderstood, your routes should look like this.

Route::get('auth/login', 'Auth\AuthController@getLogin');
Route::post('auth/login', 'Auth\AuthController@postLogin');
Route::get('auth/logout', 'Auth\AuthController@getLogout');

and you should have at the very least the login.blade.php template in your Auth folder (in views).

If you truly are going to start again, consider deleting the question as it doesn't really help anyone in it's current state.


Try this in your Auth controller

    public function authenticated( $request,  $user ) {
        return redirect()->intended($this->redirectPath().'?success');
    }

Upvotes: 0

Related Questions