mercredi 26 août 2020

Laravel Socialite + Ionic / Angular ( Sign up with Facebook or Google)

My website already has login with Fb or Google function and it's working fine, now I want to connect it to my ionic app. I wrote the code, and in Ionic it's connecting to Fb API successfully but not posting a request to my server (Laravel), there's no change in the DB. Can anyone figure out what is the issue? Here's my code:

Laravel - Api Social Controller:

     public function mobileProviderLogin($provider)
    {
        try{
            $userinfo = Socialite::driver($provider)->user();
        }catch (\Exception $e){
           return response()->json(array("status" => false,  "message" => "Login failed!"));        }

        Log::debug($userinfo->getId());
        $socialProvider = SocialProvider::where('provider_id',$userinfo->getId())->first();
        if(!$socialProvider){

           
                Log::debug('CONTINUE 1.1');
                $user = User::firstOrCreate(
                    ['name'=>$userinfo->getName(), 'display_name' => $userinfo->getName()] //Create With including email
                );
                
                Log::debug('CONTINUE 1.2');
                $user->socialProvider()->create([
                    'provider_id' =>$userinfo->getId(),
                    'provider' => $provider
                ]);
           
        }
        else
        {
            Log::debug('CONTINUE 2.1');
            $user=$socialProvider->user;
            Log::debug('CONTINUE 2.2');
        }
        
        if ($user != null)
        {
            Log::debug('CONTINUE 3.1');
            auth()->login($user);
        }
   
return response()->json(array("status" => true,  "message" => "Login success!"));  } 

Ionic - service.ts:

  mobileProviderLogin(res: any) {

    return this.http.post(this.env.API_URL + 'auth/mobileProviderLogin', {res:res}
    )
  }

Ionic - Login.ts:

fbLogin() 
 {
  this.fb.login(['public_profile', 'email'])
    .then(res => {
      if (res.status === 'connected') {
        this.isLoggedIn = true;
        this.getUserDetail(res.authResponse.userID);
        this.authService.mobileProviderLogin(res);
        this.route.navigate(['/home']); 
      } else {
        this.isLoggedIn = false;
      }
    }),  (error: any) => {
      console.log(error);
         }
}


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire