vendredi 19 octobre 2018

What does this code mean in RedirectIfAuthenticated.php?

I have come across this particular code in RedirectIfAuthenticated.php and I don't understand ho this actually works?

public function handle($request, Closure $next, $guard = null)
 {
     if (Auth::guard($guard)->check()) {
        return redirect('/home');
     }
     else {
        return redirect()->action('AdminController@login')->with('flash_message_error','Please login to access ');
     }

   return $next($request);
 }

Now the main point I want to ask here is that if I comment out the else part in the code above, I am able to access the default pages (login, register etc) which were formed using php artisan make:auth. But once I uncomment the else part, I am not able to access the default pages but I directly hit AdminController's login page that was created in the project. Why is that so?

For reference, I am also giving the controller and routes code along.

AdminController

class AdminController extends Controller {

public function login(Request $request) 
{

    if ($request->isMethod('post')) {
        $data = $request->input();
        if (Auth::attempt(['email' => $data['email'], 'password' => $data['password'], 'admin' => '1'])) { 

            return redirect('/admin/dashboard');
        } else {
            return redirect('/admin')->with('flash_message_error','Invalid Username or Password');
        }
    }


    return view('admin\admin_login');
}

public function dashboard()
    {

        return view('admin.dashboard');
    }

Routes

Route::match(['get','post'],'/admin','AdminController@login');
Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

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

    Route::get('admin/dashboard', 'AdminController@dashboard');
    Route::get('settings','AdminController@settings');
});


Route::get('logout', 'AdminController@logout');



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire