phplaravellaravel-7

Laravel logout route doesn't work correctly


in My web application i have simple custom login and logout routes which logout route doesn't work correctly and after when i want to switch to this route, that redirect to home route as /:

Route:

Route::namespace('Auth')->prefix('page')->group(function () {
    Route::get('login', 'LoginController@show')->name('login');
    Route::post('login', 'LoginController@login');

    // logout route when i use conteoller doesn't work
    Route::get('logout', 'LoginController@logoutUser')->name('logout');
});

Layout:

<a href="{{ route('logout') }}" class="nav-link">
    <i class="icon-switch2"></i>
    <span>LOGOUT</span>
</a>

LoginController:

class LoginController extends Controller
{
    use AuthenticatesUsers;

    protected $redirectTo = '/';

    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    public function login(Request $request)
    {
        $this->validateLogin($request);

        if ($this->hasTooManyLoginAttempts($request)) {
            $this->fireLockoutEvent($request);

            return $this->sendLockoutResponse($request);
        }

        if (auth()->validate($request->only('password'))) {
            $user = User::whereUsername($request->username)->first();

            if ($user->active == 0) {
                $checkActivationCode = $user->activationCode()->where('expire', '>=', Carbon::now())->latest()->first();

                if ($checkActivationCode != null) {
                    if ($checkActivationCode->expire > Carbon::now()) {
                        $this->incrementLoginAttempts($request);

                        Session::flash('message', 'Please active your account');
                        return back();
                    }
                }
            }
        }

        if ($this->attemptLogin($request)) {
            return $this->sendLoginResponse($request);
        }

        $this->incrementLoginAttempts($request);

        return $this->sendFailedLoginResponse($request);
    }

    public function logoutUser(Request $request)
    {
        dd($request->all());
        //auth()->logout();
        //$this->logout($request);
    }

    public function redirectToProvider()
    {
        return Socialite::driver('google')->redirect();
    }

    public function handleProviderCallback()
    {
        $socialUser = Socialite::driver('google')->stateless()->user();
        $user = User::whereEmail($socialUser->getEmail())->first();

        //dd($socialUser->getAvatar());

        if (!$user) {
            $data = [
                'name' => $socialUser->getName(),
                'email' => $socialUser->getEmail(),
                'avatar' => str_replace('sz=50', 'sz=150', $socialUser->getAvatar()),
                'mobileNumber' => '',
                'loginType'=>'google',
                'password' => bcrypt($socialUser->getId()),
            ];

            //dd($data);
            $user = User::create($data);
        }

        if ($user->active == 0) {
            $user->update([
                'active' => 1
            ]);
        }

        auth()->loginUsingId($user->id);
        return redirect('/system/UserLoginWithGoogle');
    }

    public function show()
    {
        return view('auth.login');
    }

    protected function validateLogin(Request $request)
    {
        $this->validate($request, [
            $this->username() => 'required|string',
            'password' => 'required|string',
            'g-recaptcha-response', 'recaptcha'
        ]);
    }
}

Solution

  • I think you have error in your contructor. There is "->except('login')", but I think it's not related to route name, but to a method, so it needs to be "->except('logoutUser')". Let me know if it works.