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


namespace App\Http\Controllers\Web\Admin;

use App\Http\Controllers\Controller;
7
use Illuminate\Http\RedirectResponse;
Jacob Priddy's avatar
Jacob Priddy committed
8
use Illuminate\Validation\ValidationException;
9 10 11 12
use Source\Exceptions\AuthenticationException;
use Source\Exceptions\EntityNotFoundException;
use Source\UseCases\Users\Authenticate\WebPresenter;
use Source\UseCases\Users\Authenticate\AuthenticateUseCase;
13 14 15

class LoginController extends Controller
{
16 17 18 19 20 21 22 23 24 25 26
    /**
     * @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',
        ]);
27

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
        $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']
            )
        );
    }
50
}