PHP code example of bluefyn-international / aws-cognito
1. Go to this page and download the library: Download bluefyn-international/aws-cognito 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/ */
bluefyn-international / aws-cognito example snippets
'guards' => [
'web' => [
'driver' => 'cognito-session', // This line is important for using AWS Cognito as Web Driver
'provider' => 'users',
],
'api' => [
'driver' => 'cognito-token', // This line is important for using AWS Cognito as API Driver
'provider' => 'users',
],
],
# AWS configurations for cloud storage
AWS_ACCESS_KEY_ID="Axxxxxxxxxxxxxxxxxxxxxxxx6"
AWS_SECRET_ACCESS_KEY="mxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx+"
# AWS Cognito configurations
AWS_COGNITO_CLIENT_ID="6xxxxxxxxxxxxxxxxxxxxxxxxr"
AWS_COGNITO_CLIENT_SECRET="1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx1"
AWS_COGNITO_USER_POOL_ID="xxxxxxxxxxxxxxxxx"
AWS_COGNITO_REGION="xxxxxxxxxxx" //optional - default value is 'us-east-1'
AWS_COGNITO_VERSION="latest" //optional - default value is 'latest'
use BluefynInternational\Cognito\Auth\RegistersUsers;
class UserController extends BaseController
{
use RegistersUsers;
public function register(Request $request)
{
$validator = $request->validate([
'name' => 'ly('name', 'email', 'password'); //passing 'password' is optional.
//Register User in cognito
if ($cognitoRegistered=$this->createCognitoUser($data)) {
//If successful, create the user in local db
User::create($collection->only('name', 'email'));
} //End if
//Redirect to view
return view('login');
}
}
namespace App\Http\Controllers;
...
use BluefynInternational\Cognito\AwsCognitoClaim;
use BluefynInternational\Cognito\Auth\AuthenticatesUsers as CognitoAuthenticatesUsers;
class AuthController extends Controller
{
use CognitoAuthenticatesUsers;
/**
* Authenticate User
*
* @throws \HttpException
*
* @return mixed
*/
public function login(\Illuminate\Http\Request $request)
{
...
//Convert request to collection
$collection = collect($request->all());
//Authenticate with Cognito Package Trait (with 'web' as the auth guard)
if ($response = $this->attemptLogin($collection, 'web')) {
if ($response===true) {
return redirect(route('home'))->with('success', true);
} else if ($response===false) {
// If the login attempt was unsuccessful you may increment the number of attempts
// to login and redirect the user back to the login form. Of course, when this
// user surpasses their maximum number of attempts they will get locked out.
//
//$this->incrementLoginAttempts($request);
//
//$this->sendFailedLoginResponse($collection, null);
} else {
return $response;
} //End if
} //End if
} //Function ends
...
} //Class ends
namespace App\Api\Controller;
...
use BluefynInternational\Cognito\AwsCognitoClaim;
use BluefynInternational\Cognito\Auth\AuthenticatesUsers as CognitoAuthenticatesUsers;
class AuthApiController extends Controller
{
use CognitoAuthenticatesUsers;
/**
* Authenticate User
*
* @throws \HttpException
*
* @return mixed
*/
public function login(\Illuminate\Http\Request $request)
{
...
//Convert request to collection
$collection = collect($request->all());
//Authenticate with Cognito Package Trait (with 'api' as the auth guard)
if ($claim = $this->attemptLogin($collection, 'api', 'username', 'password', true)) {
if ($claim instanceof AwsCognitoClaim) {
return $claim->getData();
} else {
return response()->json(['status' => 'error', 'message' => $claim], 400);
} //End if
} //End if
} //Function ends
...
} //Class ends