简体   繁体   中英

Symfony Authentification Users Form Login

I have symfony with FOSUser and HWIO Bundle. And before I have authentication only HWIO(only social network), but now I need with form authentication, with email and password. I create form registration and authentication action, and when user registration, by form, user have email and password (sha1), begin user enter email and password in form login

The controller must return a response (null given). Did you forget to add a return statement somewhere in your controller?

this is my security, I create encoders and create providers 'chain_provider' he is use 'user_dev', create admin_secured_area for 'chain_provider' and I know understand why not work, help please. what I need to do for registration user by email and password ? What am I doing wrong?

security:
 encoders:
    FOS\UserBundle\Model\UserInterface: sha512
    UserBundle\Entity\User:
        algorithm:        sha1
        encode_as_base64: false
        iterations:       1
    Symfony\Component\Security\Core\User\User: plaintext

role_hierarchy:
    ROLE_ADMIN:       ROLE_USER
    ROLE_SUPER_ADMIN: ROLE_ADMIN

providers:
    fos_userbundle:
        id: fos_user.user_provider.username
    my_custom_hwi_provider:
        id: app.provider.user_provider
    chain_provider:
        chain:
            providers: [user_dev]
    user_dev:
        entity: { class: UserBundle\Entity\User, property: email }


firewalls:

    main:
        pattern: ^/
        form_login:
            provider: fos_userbundle
            csrf_provider: form.csrf_provider
        oauth:
            resource_owners:
                facebook:           "/login/check-facebook"
                vkontakte:             "/login/check-vkontakte"

            login_path:        /login
            failure_path:      /login

            oauth_user_provider:
                #this is my custom user provider, created from FOSUBUserProvider - will manage the
                #automatic user registration on your site, with data from the provider (facebook. google, etc.)
                service: app.provider.user_provider

        logout:       true
        anonymous:    true

    admin_secured_area:
        pattern:    ^/auth/
        anonymous: ~
        form_login:
            provider: chain_provider
            login_path: /auth/login
            check_path: /auth/login_check
        logout:
            path:   /auth/logout
            target: /
            invalidate_session: false

access_control:
    - { path: ^/auth/login$, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/admin/, role: ROLE_ADMIN }

this is my SecurityController:

  /**
   * @Route("/auth")
   */
  class SecurityController extends Controller
  {
/**
 * @Route("/login", name="login_route")
 * @Template()
 */
public function loginAction()
{
    $request = $this->getRequest();
    $session = $request->getSession();

    $securityContext = $this->container->get('security.context');
    if ( $securityContext->isGranted('IS_AUTHENTICATED_FULLY') ) {
        return $this->redirect($this->generateUrl('get_all_posts'));
    }

    if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
        $error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
    } else {
        $error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
        $session->remove(SecurityContext::AUTHENTICATION_ERROR);
    }

    return array(
        '_last' => $session->get(SecurityContext::LAST_USERNAME),
        'error'         => $error,
    );
}

/**
 * @Route("/login_check", name="login_check")
 */
public function loginCheckAction()
{
    // this controller will not be executed,
    // as the route is handled by the Security system
}

this is my template

<h2>LOG IN</h2>
    <form id="artel-list" method="post" action="{{ path('login_check') }}">
        <div>
            <ul>
                <li>
                    <div><label for="username">Email</label></div>
                    <div class="indent"><input type="text" name="_username" id="username" value="{{ _last }}"/></div>
                </li>
                <li>
                    <div><label for="password">Password</label></div>
                    <div class="indent"><input type="password" name="_password" id="password" value=""/></div>
                    <a class="popup-link-1" href="">Forgot your password?</a>
                </li>
                <li class="next">
                    <button type="submit" id="sign-in" class="artel-button">Log in</button>
                </li>
            </ul>
        </div>
    </form>

help please

You don't appear to actually be rendering the login form. I haven't used this specific bundle, but I have implemented my own, and I return:

    return $this->render(
        'MySecurityBundle:Security:login.html.twig',
        array(
            // last username entered by the user
            'last_username' => $session->get(SecurityContextInterface::LAST_USERNAME),
            'error' => $error,
        )
    );

Try returning an actual render out of the loginAction function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM