0

In my Laravel project, I am using multi auth. But form my newly created user agency, log out function is not working

Following is my code in view for logout

 <li>
                                 <a href="{{ url('/agency/logout') }}"
                                    onclick="event.preventDefault();
                                    document.getElementById('logout-form').submit();">
                                 Logout
                                 </a>
                                 <form id="logout-form" action="{{ url('/agency/logout') }}" method="POST" style="display: none;">
                                    {{ csrf_field() }}
                                 </form>
                              </li>

Following is the code in routes (web.php)

Route::group(['prefix' => 'agency'], function () {
    Route::get('/', 'Agency\AgencyAuth\LoginController@showLoginForm')->name('agency_login');
    Route::get('/login', 'Agency\AgencyAuth\LoginController@showLoginForm')->name('login');
    Route::post('/login', 'Agency\AgencyAuth\LoginController@login');
    Route::post('/logout', 'Agency\AgencyAuth\LoginController@logout')->name('logout');

   // Route::get('/agencie_home', 'Agency\HomeController@index')->name('agencie_home');
});

Following is the code in LoginController.php

<?php

namespace App\Http\Controllers\Agency\AgencyAuth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;
use Hesto\MultiAuth\Traits\LogsoutGuard;
use JsValidator;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers, LogsoutGuard {
        LogsoutGuard::logout insteadof AuthenticatesUsers;
    }

    protected $validationRules = [
                                        'email' => 'required|email',
                                        'password' => 'required'
                                    ];

    /**
     * Where to redirect users after login / registration.
     *
     * @var string
     */
    public $redirectTo = '/agency/dashboard';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('agencie.guest', ['except' => 'logout']);
    }

    /**
     * Show the application's login form.
     *
     * @return \Illuminate\Http\Response
     */
    public function showLoginForm()
    {
        $validator = JsValidator::make($this->validationRules,[],[],'#loginform');
        return view('agency.auth.login')->with('validator', $validator);
    }

    /**
     * Get the guard to be used during authentication.
     *
     * @return \Illuminate\Contracts\Auth\StatefulGuard
     */
    protected function guard()
    {
        return Auth::guard('agencie');
    }

    public function logout(Request $request) {
        Auth::logout();
       return '/agency';
    }
}

When I tried to log out it is returning following error

Class App\Http\Controllers\Agency\AgencyAuth\Request does not exist

How can I implement logout functionality?

Xavier Issac
  • 414
  • 1
  • 4
  • 25

1 Answers1

0

You are not using the required namespace, try to use the following in your LoginController:

use Illuminate\Http\Request;

You are getting the error due to the fact that your script tries to load the Request class from the current namespace :

App\Http\Controllers\Agency\AgencyAuth;
Mohammad Hosseini
  • 1,649
  • 1
  • 16
  • 31