Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # https://symfony.com/doc/current/security.html#b-configuring-how-users-are-loaded
- providers:
- in_memory:
- memory: ~
- firewalls:
- # disables authentication for assets and the profiler, adapt it according to your needs
- dev:
- pattern: ^/(_(profiler|wdt)|css|images|js)/
- security: false
- main:
- anonymous: ~
- form_login:
- login_path: login
- check_path: login
- # activate different ways to authenticate
- # https://symfony.com/doc/current/security.html#a-configuring-how-your-users-will-authenticate
- #http_basic: ~
- # https://symfony.com/doc/current/security/form_login_setup.html
- #form_login: ~
- encoders:
- AppBundleEntityAdminUser: bcrypt
- class RegistrationController extends Controller
- {
- /**
- * @Route("/register", name="user_registration")
- */
- public function registerAction(Request $request)
- {
- // 1) build the form
- $user = new AdminUser();
- $form = $this->createForm(new RegistrationType(), $user);
- // 2) handle the submit (will only happen on POST)
- $form->handleRequest($request);
- if ($form->isSubmitted() && $form->isValid()) {
- // 3) Encode the password (you could also do this via Doctrine listener)
- $password = $this->get('security.password_encoder')
- ->encodePassword($user, $user->getPlainPassword());
- $user->setPassword($password);
- // 4) save the User!
- $em = $this->getDoctrine()->getManager();
- $em->persist($user);
- $em->flush();
- // ... do any other work - like sending them an email, etc
- // maybe set a "flash" success message for the user
- return $this->redirectToRoute('replace_with_some_route');
- }
- return $this->render(
- 'registration/register.html.twig',
- array('form' => $form->createView())
- );
- }
- /**
- * @Route("/login", name="login")
- */
- public function loginAction(Request $request)
- {
- $authenticationUtils = $this->get('security.authentication_utils');
- //echo "<pre>";
- //print_r($authenticationUtils);
- // get the login error if there is one
- $error = $authenticationUtils->getLastAuthenticationError();
- $user=$this->getUser();
- if(isset($user)){
- echo "<pre>";
- print_r($user);
- }
- // last username entered by the user
- $lastUsername = $authenticationUtils->getLastUsername();
- return $this->render('default/login.html.twig', array(
- 'last_username' => $lastUsername,
- 'error' => $error,
- ));
- }
- }
- {% if error %}
- <div>{{ error.messageKey|trans(error.messageData, 'security') }}</div>
- {% endif %}
- <form action="{{ path('login') }}" method="post">
- <label for="username">Username:</label>
- <input type="text" id="username" name="_username" value="{{ last_username }}" />
- <label for="password">Password:</label>
- <input type="password" id="password" name="_password" />
- {#
- If you want to control the URL the user
- is redirected to on success (more details below)
- <input type="hidden" name="_target_path" value="/account" />
- #}
- <button type="submit">login</button>
- </form>
Add Comment
Please, Sign In to add comment