LoginController.php 1.43 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
        $presenter = new WebPresenter();

        try {
            $authenticateUseCase->attempt($presenter, $this->request->all());
32
        } catch (AuthenticationException | EntityNotFoundException $e) {
33
            throw ValidationException::withMessages([
34
                'email' => [trans('auth.failed')],
35 36 37 38 39 40 41 42 43 44 45
            ]);
        }

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