PHPackages                             jamessiebert/socialite-cognito - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. jamessiebert/socialite-cognito

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

jamessiebert/socialite-cognito
==============================

Allow oauth connections from AWS Cognito to Laravel Socialite. This allows laravel to log users in from your cognito user pool. Based on https://socialiteproviders.com/Laravel-Passport, the code closely follows this structure so its not too hard to change to a Laravel Passport identity provider.

05PHP

Since Sep 25Pushed 4y ago1 watchersCompare

[ Source](https://github.com/JamesSiebert/socialite-cognito)[ Packagist](https://packagist.org/packages/jamessiebert/socialite-cognito)[ RSS](/packages/jamessiebert-socialite-cognito/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Socialite / AWS Cognito User Pools
==================================

[](#socialite--aws-cognito-user-pools)

```
composer require socialiteproviders/cognito
```

\###Project Notes: This allows socialite to connect to AWS Cognito and use the user pool for auth.
This is based on Based on: [Laravel Passport Provider](https://github.com/SocialiteProviders/Laravel-Passport)
This project relies on [Manager](https://github.com/socialiteproviders/manager)

Tutorial: How to create a starter project
-----------------------------------------

[](#tutorial-how-to-create-a-starter-project)

This install is based on a fresh project (Laravel Framework v8.61.0)

#### Create and link up database in .env

[](#create-and-link-up-database-in-env)

#### Add environmental variables

[](#add-environmental-variables)

Path: `.env`

```
SERVER_PORT=8001 # Not required, will serve on a different port, good for running multiple apps
COGNITO_HOST=https://your_cognito_domain.auth.your_region.amazoncognito.com
COGNITO_CLIENT_ID=abc123
COGNITO_CLIENT_SECRET=aaabbbccc111222333
COGNITO_REDIRECT_URI=https://your-app.au.ngrok.io/oauth2/callback
COGNITO_SIGN_OUT_URL=https://logout-redirect-to-site.com
COGNITO_LOGIN_SCOPE="aws.cognito.signin.user.admin+openid+profile"
```

#### Modify AppServiceProvider

[](#modify-appserviceprovider)

Path: `app/Providers/AppServiceProvider.php`

```
use Illuminate\Support\Facades\Schema;
public function boot()
{
Schema::defaultStringLength(125);
}
```

#### Dependencies

[](#dependencies)

For simplicity we will use standard laravel auth and bootstrap.

```
composer require laravel/ui
composer require laravel/socialite
composer require socialiteproviders/cognito
```

#### Event Listener

[](#event-listener)

Path : `app/Providers/EventServiceProvider`
Add this to array

```
protected $listen = [
    Registered::class => [
        SendEmailVerificationNotification::class,
    ],
    \SocialiteProviders\Manager\SocialiteWasCalled::class => [
        // add your listeners (aka providers) here
        'SocialiteProviders\\Cognito\\CognitoExtendSocialite@handle',
    ],
];
```

#### Add configuration

[](#add-configuration)

Path: `config/services.php`

```
'cognito' => [
   'host' => env('COGNITO_HOST'),
   'client_id' => env('COGNITO_CLIENT_ID'),
   'client_secret' => env('COGNITO_CLIENT_SECRET'),
   'redirect' => env('COGNITO_REDIRECT_URI'),
],
```

#### Install Auth UI

[](#install-auth-ui)

`php artisan ui bootstrap --auth`

#### Edit Login View

[](#edit-login-view)

Path: `resources/views/auth/login.blade.php`Comment out the existing form and add this:

```

        Cognito Login

```

\####Add logout buttons Path: `resources/views/home.blade.php`

```
Home - User Dashboard

        Cognito Logout

        Switch Account

```

### Add cognito configuration

[](#add-cognito-configuration)

Path: `config/services.php`

```
'laravelpassport' => [
  'client_id' => env('LARAVELPASSPORT_CLIENT_ID'),
  'client_secret' => env('LARAVELPASSPORT_CLIENT_SECRET'),
  'redirect' => env('LARAVELPASSPORT_REDIRECT_URI')
],
```

#### Modify NavBar Links

[](#modify-navbar-links)

Path: `resources/views/layouts/app.blade.php`
\*comment out existing right 'ul' section and replace with:

```

    @guest

            Cognito Login / Register

    @else

                {{ Auth::user()->first_name }}

                Cognito Logout
                Switch Account

    @endguest

```

### Modify welcome view links

[](#modify-welcome-view-links)

Path: `resources/views/welcome.blade.php`

```
@auth
    Dashboard
@else
    Login
@endauth
```

#### Modify default user model

[](#modify-default-user-model)

Path: `app/Models/User.php`

```
protected $fillable = [
   'first_name',
   'last_name',
   'email',
   'password',
   'provider',
   'provider_id',
];
```

#### Modify User Migration

[](#modify-user-migration)

Path: `database/migrations/..._create_users_table.php`

```
Schema::create('users', function (Blueprint $table) {
   $table->id();
   $table->string('first_name');
   $table->string('last_name');
   $table->string('email');
   $table->timestamp('email_verified_at')->nullable();
   $table->string('password')->nullable();
   $table->string('provider');
   $table->string('provider_id');
   $table->rememberToken();
   $table->timestamps();
});
```

#### Run migration

[](#run-migration)

`php artisan migrate`

#### Compile assets

[](#compile-assets)

`npm install && npm run dev`

#### Add Auth Routes

[](#add-auth-routes)

Path: `routes/web.php`

```
Route::get('/', function () { return view('welcome'); })->name('welcome');

Auth::routes();

Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');

// OAuth (Cognito)
Route::get('oauth2/login', [App\Http\Controllers\Auth\LoginController::class, 'redirectToExternalAuthServer']);                                       // Login button - Post to OAuth Server
Route::get('oauth2/callback', [App\Http\Controllers\Auth\LoginController::class, 'handleExternalAuthCallback']);                                      // For OAuth2 Callback (Cognito)
Route::get('oauth2/logout', [App\Http\Controllers\Auth\LoginController::class, 'cognitoLogout'])->name('oauth-logout');                         // OAuth2 triggered logout (Cognito)
Route::get('oauth2/switch-account', [App\Http\Controllers\Auth\LoginController::class, 'cognitoSwitchAccount'])->name('oauth-switch-account');   // Logout and login to another account
```

#### Login Controller

[](#login-controller)

Path: `app/Http/Controllers/Auth/LoginController.php`

```
