LoginController.php 1.57 KB
Newer Older
1 2 3 4 5 6 7
<?php


namespace App\Http\Controllers\Web\Admin;


use App\Http\Controllers\Controller;
8 9 10 11 12 13
use Illuminate\Http\RedirectResponse;
use Source\Exceptions\AuthenticationException;
use Source\Exceptions\EntityNotFoundException;
use Illuminate\Validation\ValidationException;
use Source\UseCases\Users\Authenticate\WebPresenter;
use Source\UseCases\Users\Authenticate\AuthenticateUseCase;
14 15 16

class LoginController extends Controller
{
17 18 19 20 21 22 23 24 25 26 27
    /**
     * @param \Source\UseCases\Users\Authenticate\AuthenticateUseCase $authenticateUseCase
     * @return \Illuminate\Http\RedirectResponse
     * @throws \Illuminate\Validation\ValidationException
     */
    public function login(AuthenticateUseCase $authenticateUseCase): RedirectResponse
    {
        $this->validate($this->request, [
            'email' => 'required|string|email',
            'password' => 'required|string',
        ]);
28

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
        $presenter = new WebPresenter();

        try {
            $authenticateUseCase->attempt($presenter, $this->request->all());
        } catch (AuthenticationException $e) {
            throw ValidationException::withMessages([
                'email' => [trans('auth.failed')],
            ]);
        } catch (EntityNotFoundException $e) {
            throw ValidationException::withMessages([
                'email' => [$e->getMessage()],
            ]);
        }

        return redirect(route('web.admin.home'))->withCookie(
            cookie(
                'api_token',
                $presenter->getViewModel()['value'],
                $presenter->getViewModel()['minutes']
            )
        );
    }
51
}