PHP code example of bkstar123 / social-auth

1. Go to this page and download the library: Download bkstar123/social-auth library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bkstar123 / social-auth example snippets


$table->string('name')->nullable();
$table->string('avatar')->nullable();
$table->string('email')->unique();
$table->string('password')->nullable();
 artisan make:migration update_users_table



use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class UpdateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->string('password')->nullable()->change();
            $table->string('name')->nullable()->change();
            $table->string('avatar')->nullable();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('users', function(Blueprint $table) {
            \DB::statement('SET FOREIGN_KEY_CHECKS=0;');
            \DB::table('users')->truncate();
            $table->dropColumn('avatar');
            $table->string('name')->nullable(false)->change();
            $table->string('password')->nullable(false)->change();
            \DB::statement('SET FOREIGN_KEY_CHECKS=1;');
        });
    }
}
 artisan migrate

protected $fillable = [
    'name', 'email', 'password', 'avatar'
];

Route::get('/login/{provider}', 'Auth\LoginController@redirectToSocialProvider')
     ->name('login.social');
Route::get('/login/{provider}/callback', 'Auth\LoginController@handleSocialProviderCallback')
     ->name('login.social.callback');



use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCustomerSocialAccountsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('customer_social_accounts', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('customer_id')->unsigned()->index();
            $table->string('provider_name');
            $table->string('provider_user_id');
            $table->timestamps();
            $table->foreign('customer_id')->references('id')->on('customers')->onDelete('cascade');
            $table->unique(['provider_name', 'provider_user_id']);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('customer_social_accounts');
    }
}
 artisan vendor:publish --provider="Bkstar123\SocialAuth\SocialAuthServiceProvider"
 artisan migrate



namespace App;

use App\Models\Customer;
use Bkstar123\SocialAuth\Models\Abstracts\SocialAccountBase;

class CustomerSocialAccount extends SocialAccountBase
{
    public function getUserModelClass()
    {
    	return Customer::class;
    }
}

protected function getSocialAccountModelClass()
{
    return CustomerSocialAccount::class; // Surely, you must autoload this class 
}

protected function getUserModelClass()
{
    return Customer::class; // Surely, you must autoload this class 
}

protected function getSocialAccountModelClass()
{
    return CustomerSocialAccount::class; // Surely, you must autoload this class 
}

protected function mapUserWithSocialData($socialUser)
{
    return [
        'social_avatar' => $socialUser->getAvatar(),
        'email' => $socialUser->getEmail(),
    ];
}

protected function beforeFirstSocialLogin($user, $socialUser)
{
    if (!$user->email_verified_at) {
        $user->email_verified_at = Carbon::now();
        $user->save();
    }
}

protected function postSocialLogIn()
{
    return redirect()->route('dashboard');
}

protected function actionIfFailingToGetSocialData()
{
    return redirect()->route('customer.login');
}

protected function guard()
{
    return Auth::guard('customer');
}
config/auth.php
app/User.php
app/Http/Controllers/Auth/LoginController.php
routes/web.php
html
<!-- For Google login -->
<a href="{{ route('login.social', ['provider' => 'google']) }}" 
   class="btn btn-danger">
    <i class="fa fa-google"></i>&nbsp; Google 
</a>
app/Models/Customer.php
config/auth.php