Skip to main content

ErugoOSS/Erugo

738 stars
2 code files
View ErugoOSS/Erugo on GitHub

database/migrations/2023_08_15_000000_create_user_auth_provider_table.php

Open in GitHub
public function up(): void
{
Schema::create('user_auth_provider', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained()->onDelete('cascade');
$table->foreignId('auth_provider_id')->constrained()->onDelete('cascade');
$table->string('provider_user_id'); // User ID from the external provider
$table->string('provider_email')->nullable(); // Email from the external provider
$table->text('access_token')->nullable(); // OAuth access token if needed
$table->text('refresh_token')->nullable(); // OAuth refresh token if needed
$table->timestamp('token_expires_at')->nullable(); // Token expiration timestamp
$table->json('provider_data')->nullable(); // Additional data from provider
$table->timestamps();
 
// Ensure a user can only link to a provider once
$table->unique(['user_id', 'auth_provider_id']);
// Ensure provider_user_id is unique per auth_provider_id
$table->unique(['auth_provider_id', 'provider_user_id']);
});
}

app/Models/AuthProvider.php

Open in GitHub
class AuthProvider extends Model
{
// ...
 
public function users()
{
return $this->belongsToMany(User::class, 'user_auth_provider')
->withPivot(['provider_user_id', 'provider_email', 'access_token',
'refresh_token', 'token_expires_at', 'provider_data'])
->withTimestamps();
}
}

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.