PHP code example of draidel / laravel-supabase-auth
1. Go to this page and download the library: Download draidel/laravel-supabase-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/ */
use Supabase\LaravelAuth\Facades\SupabaseAuth;
// Using the facade
$response = SupabaseAuth::signUp('[email protected]', 'password123', [
'name' => 'John Doe',
'timezone' => 'America/New_York',
]);
// Using API endpoint
POST /auth/supabase/register
{
"email": "[email protected]",
"password": "password123",
"name": "John Doe"
}
use Illuminate\Support\Facades\Auth;
// Using Laravel's Auth facade (recommended)
if (Auth::attempt(['email' => $email, 'password' => $password])) {
$user = Auth::user();
// User is authenticated
}
// Using API endpoint
POST /auth/supabase/login
{
"email": "[email protected]",
"password": "password123",
"remember": true
}
$user = Auth::user();
// Access Supabase-specific data
$supabaseData = $user->getSupabaseData();
$userId = $user->getSupabaseUserId();
$metadata = $user->getSupabaseUserMetadata();
// User attributes and methods
$user->hasVerifiedEmail();
$user->isAdmin();
$user->hasRole('premium');
$user->getTimezone();
$user->getPreferences();
// Update user profile
$user->updateSupabaseProfile(['name' => 'New Name']);
// Change password
$user->changeSupabasePassword('new-password');
use Supabase\LaravelAuth\Http\Middleware\EnsureTokenIsValid;
// Ensure token is valid and auto-refresh if needed
Route::middleware(EnsureTokenIsValid::class)->group(function () {
Route::get('/api/user', [ApiController::class, 'user']);
});
use Supabase\LaravelAuth\Traits\HasSupabaseAuth;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use HasSupabaseAuth;
// Your custom implementation
protected $fillable = [
'id', 'email', 'name', 'company_id', 'role',
];
public function company()
{
return $this->belongsTo(Company::class);
}
}
use Illuminate\Auth\Events\Login;
use Illuminate\Auth\Events\Logout;
Event::listen(Login::class, function (Login $event) {
// User logged in
$user = $event->user;
$guard = $event->guard; // 'supabase'
});
Event::listen(Logout::class, function (Logout $event) {
// User logged out
});
interface SupabaseAuthInterface
{
public function signUp(string $email, string $password, array $data = []): array;
public function signIn(string $email, string $password): array;
public function signOut(string $accessToken): array;
public function refreshToken(string $refreshToken): array;
public function getUser(string $accessToken): array;
public function updateUser(string $accessToken, array $data): array;
public function resetPasswordForEmail(string $email, ?string $redirectTo = null): array;
public function updatePassword(string $accessToken, string $newPassword): array;
public function verifyOtp(string $email, string $token, string $type = 'email'): array;
public function resendOtp(string $email, string $type = 'signup'): array;
public function signInWithOAuth(string $provider, array $options = []): string;
public function verifyToken(string $token): array;
public function getUserById(string $userId): array;
public function deleteUser(string $userId): array;
}
use Supabase\LaravelAuth\Exceptions\AuthenticationException;
use Supabase\LaravelAuth\Exceptions\ConfigurationException;
use Supabase\LaravelAuth\Exceptions\CircuitBreakerException;
try {
SupabaseAuth::signIn($email, $password);
} catch (AuthenticationException $e) {
// Handle authentication errors
if ($e->getCode() === 401) {
return response()->json(['error' => 'Invalid credentials'], 401);
}
} catch (CircuitBreakerException $e) {
// Handle service unavailable
return response()->json(['error' => 'Service temporarily unavailable'], 503);
}