Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/cgauge/laravel-cognito-provider

AWS Cognito provider for Laravel Authentication
https://github.com/cgauge/laravel-cognito-provider

Last synced: 16 days ago
JSON representation

AWS Cognito provider for Laravel Authentication

Awesome Lists containing this project

README

        

![Build](https://github.com/cgauge/laravel-cognito-provider/workflows/Tests/badge.svg)
[![Code Coverage](https://scrutinizer-ci.com/g/cgauge/laravel-cognito-provider/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/cgauge/laravel-cognito-provider/?branch=master)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/cgauge/laravel-cognito-provider/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/cgauge/laravel-cognito-provider/?branch=master)

# Laravel Cognito Provider 🔑

This library provides a CognitoUserProvider for Laravel.

# Installation

```bash
composer require customergauge/cognito
```

# Usage

### Auth configuration

In the `auth.php` file, add the following settings:

Default Guard

```php
'defaults' => [
'guard' => 'cognito-token',
'passwords' => 'users',
],
```

The new Guard configuration
```php
'guards' => [
'cognito-token' => [
'driver' => 'token',
'provider' => 'cognito-provider',
'storage_key' => 'cognito_token',
'hash' => false,
],
],
```

The User Provider configuration
```php

'providers' => [
'cognito-provider' => [
'driver' => \CustomerGauge\Cognito\CognitoUserProvider::class,
],
],
```

Cognito Environment Variables
```php
/*
|--------------------------------------------------------------------------
| Cognito Custom Configuration
|--------------------------------------------------------------------------
|
| The following configuration is not part of standard Laravel application.
| We use it to configure the CognitoUserProvider process so that we can
| properly validate the JWT token provided by AWS Cognito.
|
*/

'cognito' => [
'pool' => env('AWS_COGNITO_USER_POOL_ID'),
'region' => env('AWS_COGNITO_USER_POOL_REGION'),
],
```

### Auth Middleware

Configure the `auth` middleware at `App\Http\Kernel` with `'auth:cognito-token'`

### UserFactory

The last thing you'll need is to provide your own implementation of `UserFactory` and register it in a ServiceProvider.

```
final class CognitoUserFactory implements UserFactory
{
public function make(array $payload): ?Authenticatable
{
return new MyUserObject(
$payload['username'],
$payload['custom:my_custom_cognito_attribute'],
);
}
}
```

In the provider:
```
$this->app->bind(CustomerGauge\Cognito\Contracts\UserFactory, App\Auth\CognitoUserFactory::class);
```