I have followed this tutorial:
http://ift.tt/2swvtN4 and http://ift.tt/2rys0QO
And I have completed both tutorials. However after trying to log in using either facebook or twitter I get "NotfoundHttpException"
The code is the same as on these tutorial but just for the sake of it I will post it here all in one:
This is using Socialize package:
SocialAuthController.php:
class SocialAuthController extends Controller
{
public function redirect($provider)
{
return Socialite::driver($provider)->redirect();
}
public function callback(SocialAccountService $service, $provider)
{
// Important change from previous post is that I'm now passing
// whole driver, not only the user. So no more ->user() part
$user = $service->createOrGetUser(Socialite::driver($provider));
auth()->login($user);
return redirect()->to('/tfgm');
}
}
Routes:
Route::get('/redirect/{provider}', 'SocialAuthController@redirect');
Route::get('/callback/{provider}', 'SocialAuthController@callback');
SocialAccountService.php ( I put this inside app folder where models are, i don't know if that is correct):
<?php
namespace App;
use Laravel\Socialite\Contracts\Provider;
class SocialAccountService
{
public function createOrGetUser(Provider $provider)
{
$providerUser = $provider->user();
$providerName = class_basename($provider);
$account = SocialAccount::whereProvider($providerName)
->whereProviderUserId($providerUser->getId())
->first();
if ($account) {
return $account->user;
} else {
$account = new SocialAccount([
'provider_user_id' => $providerUser->getId(),
'provider' => $providerName
]);
$user = User::whereEmail($providerUser->getEmail())->first();
if (!$user) {
$user = User::create([
'email' => $providerUser->getEmail(),
'name' => $providerUser->getName(),
]);
}
$account->user()->associate($user);
$account->save();
return $user;
}
}
}
from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2swLClk
via IFTTT
Aucun commentaire:
Enregistrer un commentaire