2
votes

I'm new in laravel and I try to build a simple Authentication. all the things works fine except when using AuthController's action to log out, it just simply doesn't work. I have a nav bar which checks for Auth::check() and it doesn't change after calling the log out action and it stocks in home page. but after making few changes like deleting this manual logout code:

public function logout()
    {
        Auth::logout();
        Session::flush();
        return redirect()->intended('login');
    }

and changing the rout from this:

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

to this:

Route::get('auth/logout', 'Auth\AuthController@logout');

it throws this error:

BindingResolutionException in Container.php line 749: Target [Illuminate\Contracts\Auth\Registrar] is not instantiable. 

I have this route inside the routes.php file:

Route::get('auth/logout', 'Auth\AuthController@logout');

and my AuthController is the controller that laravel providing us.

my SessionsController that handled logout before the changes but actually not work either is:

<?php namespace App\Http\Controllers;

use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class SessionsController extends Controller
{
    /**
     * Create a new sessions controller instance.
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    /**
     * Show the login page.
     *
     * @return \Response
     */
    public function login()
    {
        return view('auth.login');
    }

    /**
     * Perform the login.
     *
     * @param  Request  $request
     * @return \Redirect
     */
    public function postLogin(Request $request)
    {
        $this->validate($request, ['username' => 'required|exists:users', 'password' => 'required']);

        if ($this->signIn($request)) {
            flash('Welcome back!');

            return redirect()->intended('dashboard');

        }

        flash('Could not sign you in.');

        return redirect()->back();
    }

    /**
     * Destroy the user's current session.
     *
     * @return \Redirect
     */
    public function logout()
    {
        Auth::logout();
        Session::flush();
        return redirect()->intended('login');
    }

    /**
     * Attempt to sign in the user.
     *
     * @param  Request $request
     * @return boolean
     */
    protected function signIn(Request $request)
    {
        return Auth::attempt($this->getCredentials($request), $request->has('remember'));
    }

    /**
     * Get the login credentials and requirements.
     *
     * @param  Request $request
     * @return array
     */
    protected function getCredentials(Request $request)
    {
        return [
            'username' => $request->input('username'),
            'password' => $request->input('password'),
            'verified' => true
        ];
    }


}

And here is the AuthController __construct method:

public function __construct(Guard $auth, Registrar $registrar)
    {
        $this->auth = $auth;
        $this->registrar = $registrar;
        $this->middleware('guest', ['except' => ['logout', 'getLogout']]);

    }

Please help me as soon as possible. Any help would be appreciated.

3

3 Answers

1
votes

update your route by this one, this is the default logout method present in AuthController Trait

Route::get('auth/logout', 'Auth\AuthController@getLogout');

Add this line in your AuthController, this will redirect your application after Logout has done.

Note: you can change the path as per your requirement.

class AuthController extends Controller
{
   protected $redirectAfterLogout  =   '/auth/login'; // this line
1
votes

try this

public function __construct()
    {
        $this->middleware('guest', ['except' => 'logout']);
    }
1
votes

I finally found the problem and fixed it with the solution below:

I change the rout to:

Route::get('auth/logout', 'Auth\AuthController@getLogout');

and change the __construct method in AuthController to:

public function __construct()
    {
        $this->middleware('guest', ['except' => ['logout', 'getLogout']]);
    }

1.The problem was in the rout. Instead of calling method "getlogout" I call for the "logout" method in my routs.php .
2. In 'except' => ['logout', 'getLogout'] , what ever method you call in your routs.php you must add to this except in AuthController __construct method.