收到错误:“未定义类型 ‘Laravel\Socialite\Facades\Socialite’。intelephense(1009)”

huangapple go评论46阅读模式
英文:

Getting Error "Undefined type 'Laravel\Socialite\Facades\Socialite'.intelephense(1009)"

问题

我遇到了一个奇怪的错误。虽然我已经执行了安装“Socialite”包所需的步骤,例如我已经编辑了“config/app.php”中的“providers”和“aliases”。问题是我正在现有项目中使用它,但在我创建新副本时它可以正常工作。

这是我的控制器代码(我在这里遇到了错误)

// Google Registration 

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

public function loginWithGoogle()
{
    try
    {
        $user = Socialite::driver('google')->user();
        $existingUser = User::where('google_id', $user->id)->first();

        if($existingUser)
        {
            Auth::login($existingUser);
            return redirect('/home');
        }
  
        else{
            $createUser = User::create([
                $uuid = Str::uuid()->toString(),
                'name'      =>  $user->name,
                'email'     =>  $user->email,
                'google_id' =>  $user->id,
            ]);
       
            Auth::login($createUser);
            return redirect('/timeline');
        }
    }
    catch(\Throwable $th){
        throw $th;
    }
}

PS* 我已经在顶部导入了所需的包

use Illuminate\Support\Facades\Auth;
use Laravel\Socialite\Facades\Socialite;

我已经遵循了安装Socialite包的所有必需步骤,唯一的问题是在控制器中遇到以下错误:

Undefined type 'Laravel\Socialite\Facades\Socialite'.intelephense(1009)

PS* 我正在使用 Laravel 9。

英文:

I'm getting this weird error. Although i have performed the required steps of installing the "Socialite" package , for instance i have edit the "providers" and "aliases" in "config/app.php". Thing is I'm using this in existing project, but when i created in fresh copy it was working fine.

Undefined type 'Laravel\Socialite\Facades\Socialite'.intelephense(1009)

Here is my controller code (Where i'm getting this error)

   `// Google Registration 

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

   public function loginWithGoogle()
   {
        try
       {
         $user = Socialite::driver('google')->user();
         $existingUser = User::where('google_id',$user->id)->first();

         if($existingUser)
         {
                 Auth::login($existingUser);
                 return redirect('/home');
         }
  
         else{
                $createUser = User::create([
                        $uuid = Str::uuid()->toString(),
                        'name'		=>	$user->name,
                        'email'		=>	$user->email,
                        'google_id'	=>	$user->id,
                ]);
       
               Auth::login($createUser);
               return redirect('/timeline');
           }
       }
   catch(\Throwable $th){
   throw $th;
   }
   }

`

PS* I have already imported the required package on the top

use Illuminate\Support\Facades\Auth;
use Laravel\Socialite\Facades\Socialite;

I have followed all the required steps to install the socialite package, only problem that i'm facing is getting following error in controller:

Undefined type 'Laravel\Socialite\Facades\Socialite'.intelephense(1009)

PS* I'm using Laravel 9.

答案1

得分: 0

主要原因可能是你忘记在你的类中包含这些类。但是你已经包含了。然后我会尝试这两个命令:

1. php artisan config:clear
2. composer dump-autoload
英文:

The main reason would that you forgot to include the classes in your class. But you did. Then i would try this two commands:

1. php artisan config:clear
2. composer dump-autoload

答案2

得分: 0

我审查您的代码使用Socialite::driver('google')->stateless()->user();而不是Socialite::driver('google')->user();

英文:

i review your code use Socialite::driver('google')->stateless()->user(); this instead of Socialite::driver('google')->user();

huangapple
  • 本文由 发表于 2023年2月8日 14:39:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75382166.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定