Skip to content

Commit

Permalink
Merge pull request #64 from deanblackborough/v1.08.0
Browse files Browse the repository at this point in the history
v1.08.0
  • Loading branch information
deanblackborough authored Jan 30, 2023
2 parents bf8ceaa + a6edd2e commit 3181340
Show file tree
Hide file tree
Showing 22 changed files with 381 additions and 276 deletions.
2 changes: 0 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ ITEM_SUBTYPE_ID=Q6OV9dk5dE

EXCEPTION_NOTIFICATION_EMAIL=

APP_HASH_SALT_FORGOT_PASSWORD=

LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug
Expand Down
45 changes: 45 additions & 0 deletions app/Actions/Account/CreateNewPassword.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);

namespace App\Actions\Account;

use App\Actions\Action;
use App\Api\Service;

/**
* @author Dean Blackborough <dean@g3d-development.com>
* @copyright Dean Blackborough (Costs to Expect) 2018-2023
* https://github.com/costs-to-expect/budget/blob/main/LICENSE
*/
class CreateNewPassword extends Action
{
public function __invoke(
Service $api,
array $input
): int
{
$post_response = $api->createNewPassword($input);

if ($post_response['status'] === 204) {
return 204;
}

if ($post_response['status'] === 422) {
$this->validation_errors = $post_response['fields'];
$this->parameters = [
'encrypted_token' => $input['encrypted_token'],
'email' => $input['email'],
];

return 422;
}

$this->message = $post_response['content'];
$this->parameters = [
'encrypted_token' => $input['encrypted_token'],
'email' => $input['email'],
];

return $post_response['status'];
}
}
56 changes: 56 additions & 0 deletions app/Actions/Account/CreatePassword.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
declare(strict_types=1);

namespace App\Actions\Account;

use App\Actions\Action;
use App\Api\Service;
use App\Models\PartialRegistration;
use App\Notifications\Registered;
use Illuminate\Support\Facades\Notification;

/**
* @author Dean Blackborough <dean@g3d-development.com>
* @copyright Dean Blackborough (Costs to Expect) 2018-2023
* https://github.com/costs-to-expect/budget/blob/main/LICENSE
*/
class CreatePassword extends Action
{
public function __invoke(
Service $api,
array $input
): int
{
$post_response = $api->createPassword($input);

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

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

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

return 204;
}

if ($post_response['status'] === 422) {
$this->validation_errors = $post_response['fields'];
$this->parameters = [
'token' => $input['token'],
'email' => $input['email'],
];

return 422;
}

$this->message = $post_response['content'];
$this->parameters = [
'token' => $input['token'],
'email' => $input['email'],
];

return $post_response['status'];
}
}
48 changes: 48 additions & 0 deletions app/Actions/Account/ForgotPassword.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
declare(strict_types=1);

namespace App\Actions\Account;

use App\Actions\Action;
use App\Api\Service;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Notification;

/**
* @author Dean Blackborough <dean@g3d-development.com>
* @copyright Dean Blackborough (Costs to Expect) 2018-2023
* https://github.com/costs-to-expect/budget/blob/main/LICENSE
*/
class ForgotPassword extends Action
{
public function __invoke(
Service $api,
array $input
): int
{
$post_response = $api->forgotPassword($input);

if ($post_response['status'] === 201) {

$this->parameters = $post_response['content']['uris']['create-new-password']['parameters'];

Notification::route('mail', $input['email'])
->notify(new \App\Notifications\ForgotPassword($input['email'], $this->parameters['encrypted_token']));

return 201;
}

if ($post_response['status'] === 422) {
$this->validation_errors = $post_response['fields'];

return 422;
}

$this->message = $post_response['content'];
$this->parameters = [
'email' => $input['email'],
];

return $post_response['status'];
}
}
51 changes: 51 additions & 0 deletions app/Actions/Account/Register.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
declare(strict_types=1);

namespace App\Actions\Account;

use App\Actions\Action;
use App\Api\Service;
use App\Models\PartialRegistration;
use App\Notifications\CreatePassword;
use Illuminate\Support\Facades\Notification;

/**
* @author Dean Blackborough <dean@g3d-development.com>
* @copyright Dean Blackborough (Costs to Expect) 2018-2023
* https://github.com/costs-to-expect/budget/blob/main/LICENSE
*/
class Register extends Action
{
public function __invoke(
Service $api,
array $input
): int
{
$post_response = $api->register($input);

if ($post_response['status'] === 201) {

$this->parameters = $post_response['content']['uris']['create-password']['parameters'];

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

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

return 201;
}

if ($post_response['status'] === 422) {
$this->validation_errors = $post_response['fields'];

return 422;
}

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

return $post_response['status'];
}
}
44 changes: 44 additions & 0 deletions app/Actions/Account/SignIn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);

namespace App\Actions\Account;

use App\Actions\Action;
use Illuminate\Support\Facades\Auth;

/**
* @author Dean Blackborough <dean@g3d-development.com>
* @copyright Dean Blackborough (Costs to Expect) 2018-2023
* https://github.com/costs-to-expect/budget/blob/main/LICENSE
*/
class SignIn extends Action
{
public function __invoke(
array $input
): int
{
if (array_key_exists('email', $input) === false || $input['email'] === null) {
$this->validation_errors['email']['errors'] = [
'You need to provide your email address'
];
}
if (array_key_exists('password', $input) === false || $input['password'] === null) {
$this->validation_errors['password']['errors'] = [
'You need to provide your password'
];
}

if ($this->validation_errors !== []) {
return 422;
}


if (Auth::attempt($input, array_key_exists('remember_me', $input))) {
return 204;
}

$this->validation_errors = Auth::errors();

return 422;
}
}
15 changes: 11 additions & 4 deletions app/Actions/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,20 @@ abstract class Action

protected array $validation_errors = [];

public function getValidationErrors(): array
{
return $this->validation_errors;
}
protected array $parameters = [];

public function getMessage(): string
{
return $this->message;
}

public function getParameters(): array
{
return $this->parameters;
}

public function getValidationErrors(): array
{
return $this->validation_errors;
}
}
2 changes: 1 addition & 1 deletion app/Api/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public function createPassword(array $payload): array
#[ArrayShape(['status' => "integer", 'content' => "array", 'fields' => "array"])]
public function createNewPassword(array $payload): array
{
$uri = Uri::createNewPassword($payload['token'], $payload['email']);
$uri = Uri::createNewPassword($payload['encrypted_token'], $payload['email']);

return $this->http->post(
$uri['uri'],
Expand Down
2 changes: 1 addition & 1 deletion app/Api/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static function createPassword(string $token, string $email): array
#[ArrayShape(['uri' => "string", 'name' => "string"])]
public static function createNewPassword(string $token, string $email): array
{
$uri = '/' . self::VERSION . '/auth/create-new-password?token=' .
$uri = '/' . self::VERSION . '/auth/create-new-password?encrypted_token=' .
urlencode($token) . '&email=' . urlencode($email);

return [
Expand Down
9 changes: 2 additions & 7 deletions app/Auth/Guard/Api/Guard.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,8 @@ public function validate(array $credentials = [], bool $remember_me = false): bo
return true;
}

if ($response['status'] === 422) {
$this->errors = $response['content']['fields'];
return false;
}

if ($response['status'] === 401) {
$this->errors = ['email' => [$response['content']]];
if ($response['status'] === 422 || $response['status'] === 401) {
$this->errors = $response['fields'];
return false;
}

Expand Down
Loading

0 comments on commit 3181340

Please sign in to comment.