Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel redirect to route when user is logged in

i am a beginner in laravel, i am trying to redirect to another route if the user is logged in, the signup and login are working perfectly and are not a problem, but when i try to do

@if(Auth::check())
    {{
        redirect()->route('news')
    }}
@endif

the redirect script gets output on the screen like this:

HTTP/1.0 302 Found Cache-Control: no-cache, private Location: http://localhost/red-sec/public/news <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <meta http-equiv="refresh" content="1;url=http://localhost/red-sec/public/news" /> <title>Redirecting to http://localhost/red-sec/public/news</title> </head> <body> Redirecting to <a href="http://localhost/red-sec/public/news">http://localhost/red-sec/public/news</a>. </body> </html>

Please excuse me if i did a rookie mistake i am extremely new to laravel, and the news route is set up correctly and is working

EDIT 1: for the first comment, yes, here is my web.php file:

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
})->name('home');

Route::post('/signup', [
    'uses' => 'UserController@postSignUp',
    'as' => 'signup'
]);

Route::post('/signin', [
    'uses' => 'UserController@postSignIn',
    'as' => 'signin'
]);

Route::get('/news', [
    'uses' => 'userController@getNews',
    'as' => 'news',
    'middleware' => 'auth'
]);
like image 498
red security Avatar asked Jan 16 '26 21:01

red security


2 Answers

You shouldn't try to (and can't) redirect in views. Views should be ONLY used to display data, not to do business logic.

Because you are not using controller to do any logic (you return view directly from router), you can do something like this:

Route::get('/', function () {
    if(Auth::check()) {
        return redirect()->route('news');
    }

    return view('welcome');
})->name('home');

Text displayed in your view is actually a HTTP response.

like image 167
Filip Sobol Avatar answered Jan 19 '26 12:01

Filip Sobol


so I'm assuming you want to see if they're already logged in, and if they are you want to redirect them away from the login page? You could accomplish that in a Route::get('/signin') method on UserController. Before it returns the signin view you could do Auth::check(), and if that is true, then do the redirect()->route('news')

You should note, however, that Laravel ships with a ton of authentication scaffolding already in place, which you can read about here.

In your web.php, have this take the place of the / route:

Route::get('/', function() {
  if (Auth::check()) {
     return redirect()->route('news');
  }
  else {
    return view('welcome');
  }
}
like image 41
dargue3 Avatar answered Jan 19 '26 11:01

dargue3



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!