Skip to content

Commit

Permalink
fix: removed ResponseInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
eliseekn committed Mar 30, 2022
1 parent 69b9eba commit 2aabbd1
Show file tree
Hide file tree
Showing 33 changed files with 307 additions and 347 deletions.
16 changes: 8 additions & 8 deletions app/Http/Controllers/Auth/EmailVerificationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Core\Support\Mail\Mail;
use App\Database\Models\Token;
use App\Mails\VerificationMail;
use Core\Http\Response\Response;
use Core\Http\Response;
use App\Http\Actions\UserActions;

/**
Expand All @@ -35,27 +35,27 @@ public function notify(Request $request, Response $response)
]);

Alert::default(__('email_verification_link_sent'))->success();
$response->redirect()->to('login')->go();
$response->redirectUrl('login')->send(302);
}

Alert::default(__('email_verification_link_not_sent'))->error();
$response->redirect()->to('signup')->go();
$response->redirectUrl('signup')->send(302);
}

public function verify(Request $request, Response $response)
{
if (!$request->hasQuery('email', 'token')) {
$response->send(data: __('bad_request'), code: 400);
$response->data(__('bad_request'))->send(400);
}

$token = Token::findBy('email', $request->email);

if (!$token || $token->token !== $request->token) {
$response->send(data: __('invalid_password_reset_link'), code: 400);
$response->data(__('invalid_password_reset_link'))->send(400);
}

if (Carbon::parse($token->expire)->lt(Carbon::now())) {
$response->send(data: __('expired_password_reset_link'), code: 400);
$response->data(__('expired_password_reset_link'))->send(400);
}

$token->delete();
Expand All @@ -64,12 +64,12 @@ public function verify(Request $request, Response $response)

if (!$user) {
Alert::default(__('account_not_found'))->error();
$response->redirect()->to('signup')->go();
$response->redirectUrl('signup')->send(400);
}

Mail::send(new WelcomeMail($user->email, $user->name));

Alert::default(__('email_verified'))->success();
$response->redirect()->to('login')->go();
$response->redirectUrl('login')->send(400);
}
}
18 changes: 9 additions & 9 deletions app/Http/Controllers/Auth/ForgotPasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use App\Mails\TokenMail;
use Core\Support\Mail\Mail;
use App\Database\Models\Token;
use Core\Http\Response\Response;
use Core\Http\Response;
use App\Http\Actions\UserActions;
use App\Http\Validators\Auth\LoginValidator;

Expand All @@ -35,31 +35,31 @@ public function notify(Request $request, Response $response)
]);

Alert::default(__('password_reset_link_sent'))->success();
$response->redirect()->back()->go();
$response->redirectBack()->send(302);
}

Alert::default(__('password_reset_link_not_sent'))->error();
$response->redirect()->back()->go();
$response->redirectBack()->send(302);
}

public function reset(Request $request, Response $response)
{
if (!$request->hasQuery('email', 'token')) {
$response->send(data: __('bad_request'), code: 400);
$response->data(__('bad_request'))->send(400);
}

$token = Token::findBy('email', $request->email);

if (!$token || $token->token !== $request->token) {
$response->send(data: __('invalid_password_reset_link'), code: 400);
$response->data(__('invalid_password_reset_link'))->send(400);
}

if (Carbon::parse($token->expire)->lt(Carbon::now())) {
$response->send(data: __('expired_password_reset_link'), code: 400);
$response->data(__('expired_password_reset_link'))->send(400);
}

$token->delete();
$response->redirect()->to("password/new?email={$request->email}")->go();
$response->redirectUrl("/password/new?email={$request->email}")->send(302);
}

public function update(Request $request, Response $response, LoginValidator $loginValidator)
Expand All @@ -69,10 +69,10 @@ public function update(Request $request, Response $response, LoginValidator $log

if (!$user) {
Alert::default(__('password_not_reset'))->error();
$response->redirect()->back()->go();
$response->redirectBack()->send(302);
}

Alert::default(__('password_reset'))->success();
$response->redirect()->to('login')->go();
$response->redirectUrl('/login')->send(302);
}
}
10 changes: 5 additions & 5 deletions app/Http/Controllers/Auth/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
use Core\Support\Auth;
use Core\Support\Alert;
use Core\Support\Session;
use Core\Http\Response\Response;
use Core\Http\Response;
use App\Http\Validators\Auth\LoginValidator;

class LoginController
{
public function index(Request $request, Response $response)
{
if (!Auth::check($request)) $response->view('auth.login');
if (!Auth::check($request)) $response->view('auth.login')->send();

$uri = !Session::has('intended') ? config('app.home') : Session::pull('intended');
$response->redirect()->to($uri)->go();
$response->redirectUrl($uri)->send(302);
}

public function authenticate(Request $request, Response $response, LoginValidator $loginValidator)
Expand All @@ -33,10 +33,10 @@ public function authenticate(Request $request, Response $response, LoginValidato
$uri = !Session::has('intended') ? config('app.home') : Session::pull('intended');

Alert::toast(__('welcome', ['name' => Auth::get('name')]))->success();
$response->redirect()->to($uri)->go();
$response->redirectUrl($uri)->send(302);
}

Alert::default(__('login_failed'))->error();
$response->redirect()->to('login')->withInputs($request->only('email', 'password'))->withErrors([__('login_failed')])->go();
$response->redirectUrl('/login')->withInputs($request->only('email', 'password'))->withErrors([__('login_failed')])->send(302);
}
}
6 changes: 3 additions & 3 deletions app/Http/Controllers/Auth/LogoutController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@

use Core\Support\Auth;
use Core\Support\Alert;
use Core\Http\Response\Response;
use Core\Http\Response;

class LogoutController
{
public function __invoke(Response $response)
{
Auth::forget();

Alert::toast(__('logged_out'))->success();
$response->redirect()->to(config('app.home'))->go();

$response->redirectUrl(config('app.home'))->send(302);
}
}
10 changes: 5 additions & 5 deletions app/Http/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@
use Core\Support\Session;
use App\Mails\WelcomeMail;
use Core\Support\Mail\Mail;
use Core\Http\Response\Response;
use Core\Http\Response;
use App\Http\Actions\UserActions;
use App\Http\Validators\Auth\RegisterValidator;

class RegisterController
{
public function index(Request $request, Response $response)
{
if (!Auth::check($request)) $response->view('auth.signup');
if (!Auth::check($request)) $response->view('auth.signup')->send();

$uri = !Session::has('intended') ? config('app.home') : Session::pull('intended');
$response->redirect()->to($uri)->go();
$response->redirectUrl($uri)->send(302);
}

public function register(Request $request, Response $response, RegisterValidator $registerValidator)
Expand All @@ -34,12 +34,12 @@ public function register(Request $request, Response $response, RegisterValidator
$user = UserActions::create($validator->validated());

if (config('security.auth.email_verification')) {
$response->redirect()->to('email/notify')->go();
$response->redirectUrl('/email/notify')->send(302);
}

Mail::send(new WelcomeMail($user->email, $user->name));
Alert::default(__('account_created'))->success();

$response->redirect()->to('login')->go();
$response->redirectUrl('/login')->send(302);
}
}
4 changes: 2 additions & 2 deletions app/Http/Middlewares/AccountPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace App\Http\Middlewares;

use Core\Http\Request;
use Core\Http\Response\Response;
use Core\Http\Response;
use Core\Support\Auth;
use Core\Support\Alert;

Expand All @@ -23,7 +23,7 @@ public function handle(Request $request, Response $response)
if (config('security.auth.email_verification')) {
if (is_null(Auth::get('email_verified'))) {
Alert::default(__('email_not_verifed'))->error();
$response->redirect()->to('login')->intended($request->fullUri())->go();
$response->redirectUrl('/login')->intended($request->fullUri())->send(302);
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions app/Http/Middlewares/ApiAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,28 @@

use Core\Support\Auth;
use Core\Http\Request;
use Core\Http\Response\JsonResponse;
use Core\Http\Response;
use Core\Support\Encryption;

/**
* Authenticate user by api token
*/
class ApiAuth
{
public function handle(Request $request, JsonResponse $response)
public function handle(Request $request, Response $response)
{
if (empty($request->getHttpAuth())) {
$response->send(data: __('auth_required'), code: 401);
$response->json([__('auth_required')])->send(401);
}

list($method, $token) = $request->getHttpAuth();

if (trim($method) !== 'Bearer') {
$response->send(data: __('invalid_auth_method'), code: 401);
$response->json([__('invalid_auth_method')])->send(401);
}

if (!Auth::checkToken(Encryption::decrypt($token), $user)) {
$response->send(data: __('invalid_credentials'), code: 401);
$response->json([__('invalid_credentials')])->send(401);
}
}
}
4 changes: 2 additions & 2 deletions app/Http/Middlewares/AuthPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use Core\Support\Auth;
use Core\Http\Request;
use Core\Http\Response\Response;
use Core\Http\Response;
use Core\Support\Alert;

/**
Expand All @@ -22,7 +22,7 @@ public function handle(Request $request, Response $response)
{
if (!Auth::check($request)) {
Alert::default(__('not_logged'))->error();
$response->redirect()->to('login')->intended($request->fullUri())->withErrors([__('not_logged')])->go();
$response->redirectUrl('login')->intended($request->fullUri())->withErrors([__('not_logged')])->send(302);
}
}
}
12 changes: 6 additions & 6 deletions app/Http/Middlewares/HttpAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,32 @@

namespace App\Http\Middlewares;

use Core\Support\Auth;
use Core\Http\Request;
use Core\Http\Response\JsonResponse;
use Core\Support\Auth;
use Core\Http\Response;

/**
* Authenticate user by http
*/
class HttpAuth
{
public function handle(Request $request, JsonResponse $response)
public function handle(Request $request, Response $response)
{
if (empty($request->getHttpAuth())) {
$response->send(data: __('auth_required'), code: 401);
$response->json([__('auth_required')])->send(401);
}

list($method, $credentials) = $request->getHttpAuth();

if (trim($method) !== 'Basic') {
$response->send(data: __('invalid_auth_method'), code: 401);
$response->json([__('invalid_auth_method')])->send(401);
}

$credentials = base64_decode($credentials);
list($email, $password) = explode(':', $credentials);

if (!Auth::checkCredentials($email, $password, $user)) {
$response->send(data: __('invalid_credentials'), code: 401);
$response->json([__('invalid_credentials')])->send(401);
}
}
}
2 changes: 1 addition & 1 deletion app/Http/Middlewares/HttpCors.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace App\Http\Middlewares;

use Core\Http\Response\Response;
use Core\Http\Response;

/**
* Manage HTTP CORS
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Validators/Auth/LoginValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class LoginValidator extends Validator
public function rules(): array
{
return [
'email' => 'required|max_len,255',
'password' => 'required|max_len,525'
'email' => 'required|valid_email|max_len,255',
'password' => 'required|max_len,255'
];
}

Expand Down
9 changes: 4 additions & 5 deletions app/Http/Validators/Auth/RegisterValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ class RegisterValidator extends Validator
{
public function __construct()
{
return $this
->addCustomRule('unique', function($field, array $input, array $params, $value) {
$data = (new Repository($params[0]))->select('*')->where($field, $value);
return !$data->exists();
}, 'This {field} is already used by another user');
$this->addCustomRule('unique', function($field, array $input, array $params, $value) {
$data = (new Repository($params[0]))->select('*')->where($field, $value);
return !$data->exists();
}, 'This {field} is already registered');
}

/**
Expand Down
4 changes: 2 additions & 2 deletions core/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Core\Routing\Router;
use Core\Support\Whoops;
use Core\Support\Exception;
use Core\Http\Response\Response;
use Core\Http\Response;

/**
* Main application
Expand All @@ -34,7 +34,7 @@ public function run()
if (config('errors.log')) save_log('Exception: ' . $e);
if (config('errors.display')) die($e);

$response->view(view: config('errors.views.500'), code: 500);
$response->view(config('errors.views.500'))->send(500);
}
}
}
4 changes: 1 addition & 3 deletions core/Database/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,7 @@ public function insert(array $items)

public function insertGetId(array $items)
{
if (!$this->insert($items)) {
return null;
}
if (!$this->insert($items)) return null;

return QueryBuilder::lastInsertedId();
}
Expand Down
Loading

0 comments on commit 2aabbd1

Please sign in to comment.