Skip to content

Commit

Permalink
Merge pull request #41 from deanblackborough/auth
Browse files Browse the repository at this point in the history
Delete account
  • Loading branch information
deanblackborough authored Aug 26, 2022
2 parents 192258f + cbc1e5a commit c89a162
Show file tree
Hide file tree
Showing 29 changed files with 1,001 additions and 113 deletions.
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ API_URL_DEV=http://costs.api.app
ITEM_TYPE_ID=
ITEM_SUBTYPE_ID=

ERROR_EMAIL=

LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug
Expand Down Expand Up @@ -50,6 +52,10 @@ MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="hello@example.com"
MAIL_FROM_NAME="${APP_NAME}"

MAILGUN_DOMAIN=
MAILGUN_SECRET=
MAILGUN_ENDPOINT=

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
Expand Down
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

The complete changelog for the Costs to Expect REST API, our changelog follows the format defined at https://keepachangelog.com/en/1.0.0/

## [1.06.0] - [2022-08-26]
### Added
- Added a `Registered` email, thanks the player for registering etc.
- Added an account page, shows account details and displays all the details for the two delete account options.
- Added "Delete my account", deletes all Yahtzee data and signs out the user.
- Added a `Bye` email which gets sent after the account has been deleted.
- Added error notifications when the App fails to negotiate with the API.
### Changed
- The `CreatePassword` email is delayed and only sent when the password has not been created during the sign-in process.
- The logo in the navbar goes to either home or / dependent on authentication status.

## [1.05.1] - [2022-08-25]
### Changed
- Minor change to the width of the authentication forms.
Expand Down
31 changes: 31 additions & 0 deletions app/Actions/Account/DeleteYahtzeeAccount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);

namespace App\Actions\Account;

/**
* @author Dean Blackborough <dean@g3d-development.com>
* @copyright Dean Blackborough (Costs to Expect) 2018-2022
* https://github.com/costs-to-expect/yahtzee/blob/main/LICENSE
*/
class DeleteYahtzeeAccount
{
public function __invoke(
string $bearer_token,
string $resource_type_id,
string $resource_id,
string $user_id,
string $email
): bool
{
\App\Jobs\DeleteYahtzeeAccount::dispatch(
$bearer_token,
$resource_type_id,
$resource_id,
$user_id,
$email
)->delay(now()->addSeconds(5));

return true;
}
}
2 changes: 2 additions & 0 deletions app/Actions/Game/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public function __invoke(
return 201;
}

$this->message = $create_message_response['content'];

return $create_message_response['status'];
}
}
22 changes: 19 additions & 3 deletions app/Api/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function authSignIn(string $email, string $password): array
}

#[ArrayShape(['status' => "integer", 'content' => "array"])]
public function authUser(): array
public function getAuthUser(): array
{
$uri = Uri::authUser();

Expand Down Expand Up @@ -244,6 +244,17 @@ public function deletePlayerScoreSheet(
return $this->http->delete($uri['uri']);
}

#[ArrayShape(['status' => "integer", 'content' => "array"])]
public function deleteResource(
string $resource_type_id,
string $resource_id
): array
{
$uri = Uri::resource($resource_type_id, $resource_id);

return $this->http->delete($uri['uri']);
}

#[ArrayShape(['status' => "integer", 'content' => "array"])]
public function getGame(
string $resource_type_id,
Expand Down Expand Up @@ -274,12 +285,17 @@ public function getAssignedGamePlayers(
public function getGames(
string $resource_type_id,
string $resource_id,
array $parameters = []
array $parameters = [],
bool $skip_cache = false,
): array
{
if ($skip_cache === true || (array_key_exists('complete', $parameters) && $parameters['complete'] === 1)) {
$skip_cache = true;
}

$uri = Uri::games($resource_type_id, $resource_id, $parameters);

return $this->http->get($uri['uri'], (array_key_exists('complete', $parameters) && $parameters['complete'] === 1));
return $this->http->get($uri['uri'], $skip_cache);
}

#[ArrayShape(['status' => "integer", 'content' => "array"])]
Expand Down
14 changes: 14 additions & 0 deletions app/Api/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,20 @@ public static function register(): array
];
}

#[ArrayShape(['uri' => "string", 'name' => "string"])]
public static function resource(string $resource_type_id, string $resource_id, array $parameters = []): array
{
$uri = '/' . self::VERSION . '/resource-types/' . $resource_type_id . '/resources/' . $resource_id;
if (count($parameters) > 0) {
$uri .= '?' . http_build_query($parameters);
}

return [
'uri' => $uri,
'name' => 'Resource'
];
}

#[ArrayShape(['uri' => "string", 'name' => "string"])]
public static function resources(string $resource_type_id, array $parameters = []): array
{
Expand Down
2 changes: 1 addition & 1 deletion app/Auth/Guard/Api/UserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function retrieveById($identifier): ?Authenticatable
{
$api = new Service($this->config['cookie_bearer']);

$user_response = $api->authUser();
$user_response = $api->getAuthUser();
if ($user_response['status'] === 200) {
$user = new User();
$user->id = $user_response['content']['id'];
Expand Down
101 changes: 100 additions & 1 deletion app/Http/Controllers/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@

namespace App\Http\Controllers;

use App\Actions\Account\DeleteYahtzeeAccount;
use App\Api\Service;
use App\Models\PartialRegistration;
use App\Notifications\CreatePassword;
use App\Notifications\Registered;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
Expand All @@ -17,6 +20,66 @@
*/
class Authentication extends Controller
{
public function account(Request $request)
{
$this->bootstrap($request);

$user = $this->api->getAuthUser();

if ($user['status'] !== 200) {
abort(404, 'Unable to fetch your account from the API');
}

$job = $request->query('job');
if ($job !== null) {
Auth::guard()->logout();
}

return view(
'account',
[
'user' => $user['content'],
'job' => $job
]
);
}

public function confirmDeleteYahtzeeAccount(Request $request)
{
$this->bootstrap($request);

$user = $this->api->getAuthUser();

if ($user['status'] !== 200) {
abort(404, 'Unable to fetch your account from the API');
}

return view(
'confirm-delete-yahtzee-account',
[
'user' => $user['content']
]
);
}

public function confirmDeleteAccount(Request $request)
{
$this->bootstrap($request);

$user = $this->api->getAuthUser();

if ($user['status'] !== 200) {
abort(404, 'Unable to fetch your account from the API');
}

return view(
'confirm-delete-account',
[
'user' => $user['content']
]
);
}

public function createPassword(Request $request)
{
$token = null;
Expand Down Expand Up @@ -56,6 +119,14 @@ public function createPasswordProcess(Request $request)
);

if ($response['status'] === 204) {

PartialRegistration::query()
->where('token', '=', $request->input('token'))
->delete();

Notification::route('mail', $request->input('email'))
->notify(new Registered());

return redirect()->route('registration-complete');
}

Expand All @@ -79,6 +150,27 @@ public function createPasswordProcess(Request $request)
->with('authentication.failed', $response['content']);
}

public function deleteYahtzeeAccount(Request $request, DeleteYahtzeeAccount $action)
{
$this->bootstrap($request);

$user = $this->api->getAuthUser();

if ($user['status'] !== 200) {
abort(404, 'Unable to fetch your account from the API');
}

$action(
$request->cookie($this->config['cookie_bearer']),
$this->resource_type_id,
$this->resource_id,
$user['content']['id'],
$user['content']['email']
);

return redirect()->route('account', ['job'=>'delete-yahtzee-account']);
}

public function register()
{
return view(
Expand All @@ -101,8 +193,15 @@ public function registerProcess(Request $request)
if ($response['status'] === 201) {
$parameters = $response['content']['uris']['create-password']['parameters'];

$model = new PartialRegistration();
$model->token = $parameters['token'];
$model->email = $parameters['email'];
$model->save();

Notification::route('mail', $request->input('email'))
->notify(new CreatePassword($parameters['email'], $parameters['token']));
->notify(
(new CreatePassword($parameters['email'], $parameters['token']))->delay(now()->addMinutes(5))
);

return redirect()->route('create-password.view')
->with('authentication.parameters', $parameters);
Expand Down
19 changes: 17 additions & 2 deletions app/Http/Controllers/Game.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
use App\Actions\Game\DeletePlayer;
use App\Actions\Game\Log;
use App\Models\ShareToken;
use App\Notifications\ApiError;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Notification;

/**
* @author Dean Blackborough <dean@g3d-development.com>
Expand Down Expand Up @@ -553,7 +556,13 @@ public function scoreUpper(Request $request)
);

if ($log_action_result !== 201) {
// @todo - Log an error
$config = Config::get('app.config');

Notification::route('mail', $config[['error_email']])
->notify(new ApiError(
'Unable to log the score for the upper section',
$log_action->getMessage()
));
}

return $this->score(
Expand Down Expand Up @@ -618,7 +627,13 @@ public function scoreLower(Request $request)
);

if ($log_action_result !== 201) {
// @todo - Log an error
$config = Config::get('app.config');

Notification::route('mail', $config['error_email'])
->notify(new ApiError(
'Unable to log the score for the lower section',
$log_action->getMessage()
));
}

return $this->score(
Expand Down
19 changes: 17 additions & 2 deletions app/Http/Controllers/Share.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
use App\Actions\Game\Log;
use App\Api\Service;
use App\Models\ShareToken;
use App\Notifications\ApiError;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Notification;
use JetBrains\PhpStorm\ArrayShape;

/**
Expand Down Expand Up @@ -180,7 +183,13 @@ public function scoreUpper(Request $request, $token)
);

if ($log_action_result !== 201) {
// @todo - Log an error
$config = Config::get('app.config');

Notification::route('mail', $config['error_email'])
->notify(new ApiError(
'Unable to log the score for the upper section',
$log_action->getMessage()
));
}

return $this->score(
Expand Down Expand Up @@ -236,7 +245,13 @@ public function scoreLower(Request $request, $token)
);

if ($log_action_result !== 201) {
// @todo - Log an error
$config = Config::get('app.config');

Notification::route('mail', $config['error_email'])
->notify(new ApiError(
'Unable to log the score for the lower section',
$log_action->getMessage()
));
}

return $this->score(
Expand Down
Loading

0 comments on commit c89a162

Please sign in to comment.