User manage / login / register / forgot password with aws cognito
onetechasia/cognito is a Laravel package for user manage / login / register / forgot password with aws cognito.
It currently has 0 GitHub stars and 6 downloads on Packagist (latest version v0.0.3).
Install it with composer require onetechasia/cognito.
Discover more Laravel packages by onetechasia
or browse all Laravel packages to compare alternatives.
Last updated
You can install the package via composer.
composer require onetechasia/cognito
Next you can publish the config
php artisan vendor:publish --provider="Onetech\Cognito\Providers\CognitoServiceProvider"
Add config to environment file: .env
# AWS Cognito configurations
AWS_ACCESS_KEY_ID=""
AWS_SECRET_ACCESS_KEY=""
AWS_COGNITO_CLIENT_ID=""
AWS_COGNITO_CLIENT_SECRET=""
AWS_COGNITO_USER_POOL_ID=""
AWS_COGNITO_REGION="us-east-1"
AWS_COGNITO_VERSION="latest"
Last but not least you want to change the auth driver: config/auth.php
'guards' => [
'cognito-token' => [
'driver' => 'cognito-token', // This line is important for using AWS Cognito as API Driver
'provider' => 'users',
],
],
Add to middleware for authentication: app/Http/Kernel.php
protected $routeMiddleware = [
'onetech.cognito' => \Onetech\Cognito\Http\Middleware\CognitoAuthenticate::class,
];
Our package is providing you these traits you can just add to your Auth Controllers to get our package running.
use Onetech\Cognito\Auth\RegistersUsers;
use Onetech\Cognito\Auth\AuthenticatesUsers;
use Onetech\Cognito\Auth\RefreshToken;
class UserController
{
use CognitoAuthenticatesUsers, RegistersUsers, RefreshToken;
}
Using in code.
Payload: username = email or custom username, password belong to policy of cognito need validation
{
"name": "Le Duy",
"username": "[email protected]",
"email": "[email protected]",
"password": "123456",
"any attributes": "add more if needed"
}
//Registering user
$bool = $this->createCognitoUser($request);
//return boolean
Payload: username and password is required
{
"username": "[email protected]",
"password": "password",
"remember": true
}
//Login user
$check = $this->attemptLogin($request);
//Response using AccessToken for call API
//Response using RefreshToken to fetch new AccessToken
//Response using IdToken to get user information
Payload: username and refresh_token is required
{
"username": "[email protected]",
"refresh_token": "refresh token"
}
//Fetch new AccessToken and IdToken
$response = $this->refreshCoginitoToken($request);
//Same API login
Payload: username and refresh_token is required
{
"username": "[email protected]",
"password": "password"
}
$check = $this->setUserPassword($request);
API call need add header. Authorization: Bearer AccessToken
Payload: old_password and new_password is required
{
"old_password": "old password",
"new_password": "new password"
}
$accessToken = Auth::guard('cognito-token')->getTokenForRequest();
$oldPassword = $request->old_password;
$newPassword = $request->new_password;
$check = $this->changeUserPassword($accessToken, $oldPassword, $newPassword);
You can using IdToken parse user info or call api to get information
API call need add header. Authorization: Bearer AccessToken
$userInfo = Auth::guard('cognito-token')->user();
API call need add header. Authorization: Bearer AccessToken
$accessToken = Auth::guard('cognito-token')->getTokenForRequest();
$check = $this->signOut($accessToken);