1

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.

2 Answers2

0

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
Maik Lowrey
  • 15,957
  • 6
  • 40
  • 79
  • I have added the "use Laravel\Socialite\Facades\Socialite;" class already . But the issue still persist after running the "config:clear" and "dump-autoload" command – Muhammad Khan Feb 08 '23 at 06:49
  • @MuhammadKhan 1) You working with sail? 2) Take a look to this Answear: https://stackoverflow.com/a/59266972/14807111 – Maik Lowrey Feb 08 '23 at 06:51
0

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

  • Can you explain why, please ? – Maik Lowrey Feb 08 '23 at 07:14
  • The stateless method may be used to disable session state verification. This is useful when adding social authentication to a stateless API that does not utilize cookie based sessions: use Laravel\Socialite\Facades\Socialite; return Socialite::driver('google')->stateless()->user(); – Muhammad Asad Feb 08 '23 at 11:10
  • Thx! But how do you know that the QuestionOwner use a statless auth? Can you please show me the place in his code, I can't find it unfortunately? Thx ;-) – Maik Lowrey Feb 08 '23 at 11:53
  • In Laravel, stateless authentication can be achieved by using tokens, which can be passed with each request to the server and verified on the server-side. Laravel Socialite, you would need to review their code and look for instances where tokens are being passed in the header of requests and verified on the server-side. Additionally, you could look for the implementation of the Passport library in Laravel, which is often used for implementing stateless authentication in Laravel applications. – Muhammad Asad Feb 09 '23 at 06:43
  • Good Morning @Muhammad Asad! Thx for answering. To be honest, I wonder how you could see that from the code above from the question. Because what you're saying sounds like you have access to all his code! That would explain it at least. – Maik Lowrey Feb 09 '23 at 06:55
  • No it's not like that i faced this issue before that way i know this much about the question he asked. – Muhammad Asad Feb 09 '23 at 07:03
  • I see. You already know each other, right. That explains it. Until next time, then. Cheers! – Maik Lowrey Feb 09 '23 at 07:07