Ejemplo n.º 1
0
            $form->get('email')->addError(new FormError('Email is already registered by another user'));
        } catch (\Exception $e) {
            $form->addError(new FormError('There was an error, please get in touch with us'));
        }
    }
    return $app['twig']->render('signin.html.twig', ['form' => $form->createView()]);
})->bind('signin');
// Login
$app->get('/login', function () use($app) {
    return $app['twig']->render('login.html.twig');
})->bind('login');
$app->post('/login', function (Request $request) use($app) {
    $userRepository = $app['user_repository'];
    $session = $app['session'];
    $authentifier = new \Lw\Infrastructure\Domain\SessionAuthentifier($userRepository, $session);
    $service = new \Lw\Application\Service\User\LogInUserService($authentifier);
    $result = $service->execute($request->get('email'), $request->get('password'));
    return $result ? $app->redirect('/dashboard') : $app->redirect('/login');
});
// Logout
$app->get('/logout', function () use($app) {
    $userRepository = $app['user_repository'];
    $session = $app['session'];
    $authentifier = new \Lw\Infrastructure\Domain\SessionAuthentifier($userRepository, $session);
    $service = new \Lw\Application\Service\User\LogOutUserService($authentifier);
    $service->execute();
    return $app->redirect('/login');
})->bind('logout');
$app->get('/dashboard', function () use($app) {
    $userSecurityToken = $app['session']->get('user');
    if (!$userSecurityToken) {
Ejemplo n.º 2
0
    }
    return $app['twig']->render('signin.html.twig', ['form' => $form->createView()]);
})->bind('signin');
// Login
$app->match('/login', function (Request $request) use($app) {
    /**
     * @var Form
     */
    $form = $app['log_in_form'];
    $form->handleRequest($request);
    if ($form->isValid()) {
        $data = $form->getData();
        try {
            $userRepository = $app['user_repository'];
            $session = $app['session'];
            $service = new \Lw\Application\Service\User\LogInUserService(new \Lw\Infrastructure\Domain\SessionAuthentifier($userRepository, $session));
            $result = $service->execute($data['email'], $data['password']);
            if ($result) {
                return $app->redirect('/dashboard');
            }
        } catch (UserAlreadyExistsException $e) {
            $form->get('email')->addError(new FormError('Email is already registered by another user'));
        } catch (\Exception $e) {
            $form->addError(new FormError('There was an error, please get in touch with us'));
        }
    }
    return $app['twig']->render('login.html.twig', ['form' => $form->createView()]);
})->bind('login');
// Logout
$app->get('/logout', function () use($app) {
    $userRepository = $app['user_repository'];