Install the Package
composer require n30/socialiteproviders-amazon
Ensure your project meets the PHP ≥ 7.2 requirement and has one of the supported Socialite packages (socialiteproviders/manager, joelbutcher/socialstream, or laravel/socialite).
Configure Amazon Provider
Add the provider to your config/services.php:
'amazon' => [
'client_id' => env('AMAZON_CLIENT_ID'),
'client_secret' => env('AMAZON_CLIENT_SECRET'),
'redirect' => env('AMAZON_REDIRECT_URI'),
],
Add these to your .env:
AMAZON_CLIENT_ID=your_amazon_client_id
AMAZON_CLIENT_SECRET=your_amazon_client_secret
AMAZON_REDIRECT_URI=http://your-app.test/auth/amazon/callback
Register the Provider
If using socialiteproviders/manager or socialstream, add this to AppServiceProvider:
use N30\SocialiteProviders\Amazon\AmazonExtendSocialite;
public function boot()
{
$socialite = $this->app->make(Socialite::class);
$socialite->extend('amazon', function ($app) use ($socialite) {
return AmazonExtendSocialite::socialite($app, $socialite);
});
}
Add Routes
Route::get('/auth/amazon', [AuthController::class, 'redirectToAmazon'])->name('amazon.login');
Route::get('/auth/amazon/callback', [AuthController::class, 'handleAmazonCallback']);
First Use Case Implement a controller method to handle the OAuth flow:
public function redirectToAmazon()
{
return Socialite::driver('amazon')->redirect();
}
public function handleAmazonCallback()
{
try {
$user = Socialite::driver('amazon')->user();
// Map Amazon user to your app's user model (e.g., create/update).
} catch (\Exception $e) {
return redirect('/login')->with('error', 'Amazon login failed.');
}
}
User Authentication Flow
Socialite::driver('amazon')->redirect().Socialite::driver('amazon')->user() to fetch user data (e.g., id, email, name).create() or firstOrCreate()).Integration with SocialStream (JetStream)
socialstream, extend the provider in app/Providers/SocialStreamServiceProvider:
SocialStream::extend('amazon', function () {
return new \N30\SocialiteProviders\Amazon\AmazonExtendSocialite();
});
config/socialstream.php to include Amazon:
'providers' => [
'amazon' => [
'client_id' => env('AMAZON_CLIENT_ID'),
'client_secret' => env('AMAZON_CLIENT_SECRET'),
'redirect' => env('AMAZON_REDIRECT_URI'),
],
],
Standalone Socialite Usage
socialstream or manager, manually extend Socialite in AppServiceProvider (as shown in Getting Started).$amazonUser = Socialite::driver('amazon')->user();
User Data Mapping
user() method returns an array with keys like id, email, name, avatar, etc. Customize how you map this to your user model:
$user = User::firstOrCreate(
['email' => $amazonUser->email],
[
'name' => $amazonUser->name,
'amazon_id' => $amazonUser->id,
'avatar' => $amazonUser->avatar ?? null,
]
);
Error Handling
try-catch blocks to handle:
Exception).email).Amazon Developer Console Setup
redirect_uri in your .env matches exactly (including http vs. https) what’s registered in the Amazon Developer Console.https://www.amazon.com/ap/oa). Switch to production (https://www.amazon.com/ap/oa) only after approval.client_secret in your codebase. Use .env and ensure it’s in your .gitignore.Missing User Fields
email or name if not requested in the scope. Add these to your redirect() call:
Socialite::driver('amazon')->scopes(['profile', 'email'])->redirect();
email is missing, fall back to a generic placeholder (e.g., amazon_{user_id}@yourdomain.com).SocialStream-Specific Issues
socialstream, ensure the provider is listed in config/socialstream.php before booting the service provider./login and /callback in isolation.Deprecation Risks
socialiteproviders/manager directly.Debugging Tips
AppServiceProvider to debug OAuth responses:
Socialite::setClient(new \GuzzleHttp\Client([
'debug' => true,
]));
Socialite::driver('amazon')->user() to verify expected fields:
dd($amazonUser->toArray());
Custom User Mapping
AmazonExtendSocialite:
class CustomAmazonExtendSocialite extends \N30\SocialiteProviders\Amazon\AmazonExtendSocialite
{
public function user()
{
$user = parent::user();
// Custom logic (e.g., parse custom claims).
return $user;
}
}
AppServiceProvider:
$socialite->extend('amazon', function ($app) {
return new CustomAmazonExtendSocialite($app['request']);
});
Adding Scopes Dynamically
Socialite::driver('amazon')->scopes(['profile', 'email', 'custom_scope'])->redirect();
Localization
resources/views/vendor/socialite-amazon.Testing
Socialite::shouldReceive('driver:amazon')->andReturnSelf()
->shouldReceive('user')->andReturn((new \N30\SocialiteProviders\Amazon\User)->setRaw([
'id' => '123',
'email' => 'user@example.com',
]));
How can I help you explore Laravel packages today?