Search code examples
phplaravel-5.4laravel-socialite

ReflectionException Class LoginController does not exist


I'm not using default routes for login and logout and these custom routes I built work fine. I'm implementing a google login into my app, using socialite. These is the web.php fragment about google login:

Route::get('/auth/google', 'LoginController@redirectToGoogle')->name('googleauth')->middleware('guest');

Route::get('/auth/google/callback', 'LoginController@handleGoogleCallback')->name('googlecall');

this is what I got in login view:

<form id="googleLoginForm" name="googleLoginForm" method="GET" action="{{ route('googleauth') }}">
    {!! csrf_field() !!}
    <br> <br>
    <button id="btnGoogleLogin" name="btnGoogleLogin" type="submit" class="btn btn-default">G login</button>
</form>

this is my controller located at app/http/controllers/auth:

namespace MyApp\Http\Controllers\Auth;

use MyApp\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Socialite;

class LoginController extends Controller
{
    use AuthenticatesUsers;
    protected $redirectTo = '/home';

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

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

    public function handleGoogleCallback(){
        $user = Socialite::driver('google')->user();
        $user->token; 
    }

}

I think I have installed socialite correctly, I have also generated a secret and a client id using google api console. What's missing? I can't really understand why it doesn't work


Solution

  • You must append the namespace in the route declaration:

    Route::get('/auth/google', 'Auth\LoginController@redirectToGoogle')->name('googleauth')->middleware('guest');
    Route::get('/auth/google/callback', 'Auth\LoginController@handleGoogleCallback')->name('googlecall');
    

    Or you can group them by namespace, example:

    Route::namespace('Auth')->group(function () {
        Route::get('/auth/google', 'LoginController@redirectToGoogle');
        Route::get('/auth/google/callback', 'LoginController@handleGoogleCallback');
    });