PHP code example of shady-amir / laravel-aws-cognito-auth
1. Go to this page and download the library: Download shady-amir/laravel-aws-cognito-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/ */
shady-amir / laravel-aws-cognito-auth example snippets
'providers' => [
...
Aws\Laravel\AwsServiceProvider::class,
Pallant\LaravelAwsCognitoAuth\ServiceProvider::class,
...
]
protected $middlewareGroups = [
'web' => [
...
\Pallant\LaravelAwsCognitoAuth\AuthenticateSession::class,
...
],
];
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
...
'guards' => [
'web' => [
'driver' => 'aws-cognito',
'provider' => 'users',
],
]
'pool-id' => '<xxx-xxxxx>',
...
'apps' => [
'default' => [
'client-id' => '<xxxxxxxxxx>',
'refresh-token-expiration' => 30,
],
]
'apps' => [
'default' => [
'client-id' => '<xxxxxxxxxx>',
'refresh-token-expiration' => <num-of-days>,
],
]
'credentials' => [
'key' => <xxxxxxxxxx>,
'secret' => <xxxxxxxxxx>,
]
'credentials' => [
'key' => <xxxxxxxxxx>,
'secret' => <xxxxxxxxxx>,
],
'region' => <xx-xxxx-x>,
'version' => 'latest',
'ua_append' => [
'L5MOD/' . AwsServiceProvider::VERSION,
],
Auth::attempt([
'email' => '[email protected] ',
'password' => 'xxxxxxxxxx',
]);
Auth::attempt([
'email' => '[email protected] ',
'password' => 'xxxxxxxxxx',
], true);
Auth::confirmPassword([
'email' => '[email protected] ',
'password' => 'xxxxxxxxxx',
'session' => 'xxxxxxxxxx',
]);
Auth::user();
Auth::logout();
Auth::getCognitoAccessToken();
Auth::getCognitoIdToken();
Auth::getCognitoRefreshToken();
Auth::getCognitoTokensExpiryTime();
Auth::getCognitoRefreshTokenExpiryTime();
Auth::getCognitoAccessRoles();
Auth::getCognitoAccessGroups();
Auth::hasCognitoAccessRole($role)
Auth::hasCognitoAccessGroup($group)
use Pallant\LaravelAwsCognitoAuth\Utilities\AwsSecureRequest;
$region = env('AWS_GETWAY_API_REGION');
$version = env('AWS_GETWAY_API_VERSION');
$IdentityPoolId = env('AWS_GETWAY_API_IDENTITY_POOL_ID');
$Logins = env('AWS_GETWAY_API_LOGINS');
$awsSecureRequest = new AwsSecureRequest($region, $version, $IdentityPoolId, $Logins);
$host = 'AWS_HOST_API_GATEWAY_WITHOUT_HTTPS';
$uri = 'URI_TO_API_CALL';
$QueryString = '';
$httpRequestMethod = 'GET';
$postData = [];
$awsService = 'execute-api'
$userIdToken = 'idToken'; // Auth::getCognitoIdToken();
$debug = TRUE;
$result = $this->awsSecureRequest->SignatureAndCallAPI($host, $uri, $awsService, $httpRequestMethod, $QueryString, $postData, $userIdToken,$debug);
Auth::attempt(array $credentials, [bool $remember], [$errorHandler]);
Auth::validate(array $credentials, [$errorHandler]);
Auth::attempt([
'email' => '[email protected] ',
'password' => 'xxxxxxxxxx',
], false, AWS_COGNITO_AUTH_THROW_EXCEPTION);
Auth::validate([
'email' => '[email protected] ',
'password' => 'xxxxxxxxxx',
], AWS_COGNITO_AUTH_THROW_EXCEPTION);
try {
Auth::attempt([
'email' => '[email protected] ',
'password' => 'xxxxxxxxxx',
], false, AWS_COGNITO_AUTH_THROW_EXCEPTION);
} catch (\Pallant\LaravelAwsCognitoAuth\AuthAttemptException $e) {
$response = $e->getResponse();
// Handle error...
}
Auth::attempt([
'email' => '[email protected] ',
'password' => 'xxxxxxxxxx',
], false, AWS_COGNITO_AUTH_RETURN_ATTEMPT);
Auth::validate([
'email' => '[email protected] ',
'password' => 'xxxxxxxxxx',
], AWS_COGNITO_AUTH_RETURN_ATTEMPT);
$attempt = Auth::attempt([
'email' => '[email protected] ',
'password' => 'xxxxxxxxxx',
], false, AWS_COGNITO_AUTH_RETURN_ATTEMPT);
if ($attempt->successful()) {
// Do something...
} else {
$response = $attempt->getResponse();
// Handle error...
}
[
'exception' => CognitoIdentityProviderException {...},
]
[
'ChallengeName' => 'NEW_PASSWORD_REQUIRED',
'Session' => '...',
'ChallengeParameters' => [...],
]
Auth::attempt([
'email' => '[email protected] ',
'password' => 'xxxxxxxxxx',
], false, function (\Pallant\LaravelAwsCognitoAuth\AuthAttemptException $e) {
$response = $e->getResponse();
// Handle error...
});
Auth::validate([
'email' => '[email protected] ',
'password' => 'xxxxxxxxxx',
], function (\Pallant\LaravelAwsCognitoAuth\AuthAttemptException $e) {
$response = $e->getResponse();
// Handle error...
};
Auth::attempt([
'email' => '[email protected] ',
'password' => 'xxxxxxxxxx',
], false, \App\MyCustomErrorHandler::class);
Auth::validate([
'email' => '[email protected] ',
'password' => 'xxxxxxxxxx',
], \App\MyCustomErrorHandler::class);
namespace App;
use Pallant\LaravelAwsCognitoAuth\AuthAttemptException;
class MyCustomErrorHandler
{
public function handle(AuthAttemptException $e)
{
$response = $e->getResponse();
// Handle error...
}
}
'errors' => [
'handler' => 'AWS_COGNITO_AUTH_THROW_EXCEPTION',
],
'errors' => [
'handler' => 'AWS_COGNITO_AUTH_RETURN_ATTEMPT',
],
'errors' => [
'handler' => function (\Pallant\LaravelAwsCognitoAuth\AuthAttemptException $e) {
$e->getResponse();
// Do something...
},
],
'errors' => [
'handler' => \App\MyCustomErrorHandler::class,
],
[
'exception' => CognitoIdentityProviderException {...},
]
[
'ChallengeName' => 'NEW_PASSWORD_REQUIRED',
'Session' => '...',
'ChallengeParameters' => [...],
]
php artisan vendor:publish --provider="Pallant\LaravelAwsCognitoAuth\ServiceProvider"
php artisan vendor:publish --provider="Aws\Laravel\AwsServiceProvider"