0

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?

3
  • This situation is similar to this question, but was not able to solve it: stackoverflow.com/questions/31575113/… Commented Dec 28, 2015 at 21:33
  • You need to show some code. What do your routes look like? No code = guesswork by everybody. Commented Dec 28, 2015 at 23:25
  • you put the auth routes in what middleware group? Commented Jan 16, 2016 at 2:38

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

Comments

0

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');
    }

4 Comments

Actually... What is your problem exactly? The after authentication redirect? Or the posting of the form, as in... Does your form submit to the Auth/login (form action)?
Thaks, I added that function to my auth controller, it did not seem to be working so I thought it might be my browser, I first used Chrome in incognito mode and it worked and then tried Firefox and it also worked, any idea why it is acting strange in Chrome?
Only thing I can think of is the PHP file not having been fully saved. Are you saying it doesn't work in normal chrome though? Cause a redirect, regardless should be working cross-browser. Please test it again, I have this code running on several production sites and has never caused me any trouble.
Sorry that was preemptive, it is still not working..even when I manually go to the auth/login page it redirects to the root page and displays the welcome view...I am going to completely start over later tonight. The session files are being created in storage/framework/sessions so it appears to be working up to the redirect portion...

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.