Search code examples
phplaravelrouteslaravel-5.7laravel-authentication

How can fix the change of my intended Website URL?


I created a auth for my website. I have figured out that if a User is logged in he cant go back to the homepage because he is now in the dashboard. I changed everything from the generated home.blade.php to dashboard.blade.php also all related references of home by the help of Stackoverflow

Now I get a Awkward Error. If I logout from the dashboard and want to be redirect to http://localhost:8888/ (welcome.blade.php), i get http://localhost:8888/login (login.blade.php).

More awkward if I click on the navbrand on top left after logout, I am not redirecting to http://localhost:8888/ instead i get redirected to http://localhost:8888/login.

  1. I changed my web.php routes. I added Groups between 'auth' and 'guest'.
  2. If logged User tries to manipulate the url from '/dashboard' to '/' he gets redirected.
  3. I installed Laravel/telescope for better visibility of the bug but for me as a beginner it is really confusing
  4. I changed the hone.blade.php to dashboard.blade.php and everything related to 'home'
  5. Also tried to find solution and used a code of an stackoverflow user to redirect after the user logouts (LoginController.php), doesn't work

web.php

Route::group(array(['middleware'=>['guest']]), function(){

  Route::get('/', function () {
    return view('welcome');
  });
});



Auth::routes();


// Only logged user
Route::group(array('middleware'=>'auth'), function(){
  Route::get('/dashboard', 'DashboardController@index')->name('dashboard');

  Route::get('/', function () {
    return redirect('/dashboard');
  });


});

LoginController.php

use Illuminate\Http\Request;
protected function loggedOut(Request $request) {
      return redirect('/');
    }

So I cant put a picture of telescope but I think i can try to recreate the Picture with text.

Expected Results: I don't know much about HTTP-Status so i made all 200

Verb| Path       | Status

Here by '/' should be viewed welcome.blade.php

GET | /          | 200

POST| /logout    | 200

GET | /dashboard | 200

The user can't visit the Path '/' because he is now in dashboard website.

GET | /          | 200

POST| /login     | 200

Actual Results:

Verb| Path       | Status
GET | /login     | 200

GET | /          | 302

POST| /logout    | 302

GET | /dashboard | 200

GET | /          | 302

POST| /login     | 302

If you guys have any Question and want to see some more classes then please ask me. I dont know what is relevant for the error. My thought is mainly that web.php routes are wrong.

Best Regards Tobias


Solution

  • The mistake is that you use the same route twice.

    rewrite code in web.php:

    Route::get('/', HomeController@index);
    
    Auth::routes();`
    
    // Only logged user
    Route::group(array('middleware'=>'auth'), function(){
        Route::get('/dashboard', 'DashboardController@index')->name('dashboard');
    });
    

    and in HomeController write your logic:

    public function index()
    {
        return Auth::guest() ? view('welcome') : redirect('/dashboard');
    }