Reputation: 2725
Actually, i was following instructions from http://blog.damirmiladinov.com/laravel/laravel-5.2-socialite-facebook-login.html#.V2yhIbjJvIU
Step on which i got stuck is
Add following fields to the migration:
Schema::table('social_accounts', function (Blueprint $table) {
$table->integer('user_id');
$table->string('provider_user_id');
$table->string('provider');
$table->timestamps();
});
Run the migrations
php artisan migrate:refresh
error on running this command :
[Illuminate\Database\QueryException]
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'social _acco unts' already exists (SQL: create table
social_accounts
(id
int un signe d not null auto_increment primary key,created_at
timestamp null,u pdate d_at
timestamp null) default character set utf8 collate utf8_unicode_ ci)[PDOException]
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'social _acco unts' already exists
and following is my error on localhost after clicking on fblogin.
QueryException in Connection.php line 713: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'provider' in 'where clause' (SQL: select * from
social_accounts
whereprovider
= facebook andprovider_user_id
= 1130902766955428 limit 1)
My migration file :
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSocialAccountsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('social_accounts', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
});
Schema::table('social_accounts', function (Blueprint $table) {
$table->integer('user_id');
$table->string('provider_user_id');
$table->string('provider');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('social_accounts');
}
}
My controller file:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\SocialAccountService;
use Socialite;
class SocialAuthController extends Controller
{
public function redirect()
{
return Socialite::driver('facebook')->redirect();
}
public function callback(SocialAccountService $service)
{
$user = $service->createOrGetUser(Socialite::driver('facebook')->user());
auth()->login($user);
return redirect()->to('/home');
}
}
files in my app folder:
SocialAccount.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class SocialAccount extends Model
{
protected $fillable = ['user_id', 'provider_user_id', 'provider'];
public function user()
{
return $this->belongsTo(User::class);
}
}
SocialAccountservice.php
<?php
namespace App;
use Laravel\Socialite\Contracts\User as ProviderUser;
class SocialAccountService
{
public function createOrGetUser(ProviderUser $providerUser)
{
$account = SocialAccount::whereProvider('facebook')
->whereProviderUserId($providerUser->getId())
->first();
if ($account) {
return $account->user();
} else {
$account = new SocialAccount([
'provider_user_id' => $providerUser->getId(),
'provider' => 'facebook'
]);
$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;
}
}
}
Upvotes: 1
Views: 314
Reputation: 2725
[SOLVED]
step 1:remove the socialite references from the config/app.php
step 2: run composer update
step 3:then run composer dump-autoload
step 4:then add socialite references to config/app.php
step 5:Lastly, run composer dump-autoload
Upvotes: 0
Reputation: 1013
There is already a table named 'social_accounts' in database. First delete that table & then try to run migration.
Upvotes: 1