Answer the question
In order to leave comments, you need to log in
Why might "Missing authorization code" error occur when using socialite laravel?
When authorizing on the site through fb I get an error:
Client error: `POST https://graph.facebook.com/v3.3/oauth/access_token` resulted in a `400 Bad Request` response: {"error":{"message":"Missing authorization code","type":"OAuthException","code":1
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\SocialAccount;
use App\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Laravel\Socialite\Facades\Socialite;
class SocialController extends Controller
{
public function redirectToProvider($provider)
{
return Socialite::driver($provider)->redirect();
}
public function handleProviderCallback($provider)
{
$socialiteUser = Socialite::driver($provider)->user();
$user = $this->findOrCreateUser($provider, $socialiteUser);
auth()->login($user, true);
return redirect('/');
}
public function findOrCreateUser($provider, $socialiteUser)
{
if ($user = $this->findUserBySocialId($provider, $socialiteUser->getId())) {
return $user;
}
if ($user = $this->findUserByEmail($provider, $socialiteUser->getEmail())) {
$this->addSocialAccount($provider, $user, $socialiteUser);
return $user;
}
$user = User::create([
'name' => $socialiteUser->getName(),
'email' => $socialiteUser->getEmail(),
'password' => Hash::make(Str::random()),
]);
$this->addSocialAccount($provider, $user, $socialiteUser);
return $user;
}
public function findUserBySocialId($provider, $id)
{
$socialAccount = SocialAccount::where('provider', $provider)
->where('provider_id', $id)
->first();
return $socialAccount ? $socialAccount->user : false;
}
public function findUserByEmail($provider, $email)
{
return !$email ? null : User::where('email', $email)->first();
}
public function addSocialAccount($provider, $user, $socialiteUser)
{
SocialAccount::create([
'user_id' => $user->id,
'provider' => $provider,
'provider_id' => $socialiteUser->getId(),
'token' => $socialiteUser->token,
]);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question