1
votes

I'm getting this error while trying to integrate Google login on my Laravel app:

Class 'App\Http\Controllers\Auth\User' not found

namespace App\Http\Controllers\Auth;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

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


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


public function handleGoogleCallback(Request $request)
{
    $googleUser = \Socialite::driver('google')->stateless()->user();

    $user = \User::whereGoogleId($googleUser->id)->first();

    if ($user) {
        Auth::login($user);

        return LoginController::authenticated($request, $user);
    } else {
        list($first_name, $last_name) = explode(' ', $googleUser->name);
        // signup
        $input = [
            'first_name' => $first_name,
            'last_name' => $last_name,
            'email' => $googleUser->email,
        ];

        $request->session()->flash('google_token', $googleUser->accessToken);

        return RegisterController::showRegistrationForm()->withInput($input)->with;
    }
}

also,i don't know the full namespace for USER I'm new to this .

If you could give a working tutorial,i'll appreciate. Could you please advise me on solution?

2
Use the full namespace for Useraynber
try \User::. Please edit your question and paste your code, don't link images of code. And please remove the link to your site, is not necessary.dparoli
now showing "Class 'User' not found"HALLOweEN
where is your User class file?dparoli
may i know what's the file name for User class?HALLOweEN

2 Answers

0
votes

Update

If you are intending to use the default User model provided by Laravel, the User class should be stored in the /app directory (check it first).

Given the fact that your User model is inside the app/Model directory, update your code like this:

namespace App\Http\Controllers\Auth;

// some imports..

use App\Model\User; // <------

class SocialiteController extends Controller
{

    // some code..

    public function handleGoogleCallback(Request $request)
    {

        // more code..

        $user = User::whereGoogleId($googleUser->id)->first();
//              ^^^^
        // ...
    }
}

Note: the following error shown:

400 Bad Request response:

{
    "error": "invalid_grant",
    "error_description": "Bad Request"
}

This has to do with your Google app keys. This is part of a different question.

0
votes

If the User model file is located under a folder named Model yuo have to call the fully qualified name of the User class or add a use statement to your code, i.e.:

$user = \Model\User::whereGoogleId($googleUser->id)->first();

// or this solution
use \Model\User;
....
$user = User::whereGoogleId($googleUser->id)->first();