0
votes

I have a problem with my symfony 2.0 application. I couldn't find the right solution via google so I'm here :)

I've tried to create a login form. This is my security.yml file:

security:
encoders:
    Domicon\AdminBundle\Entity\User:
        algorithm: sha1
        encode_as_base64: false
        iterations: 10

providers:
    admin:
        entity: { class: DomiconAdminBundle:User, property: email }

role_hierarchy:
    ROLE_ADMIN:       ROLE_USER
    ROLE_SUPER_ADMIN: ROLE_ADMIN

firewalls:
    dev:
        pattern:  ^/(_(profiler|wdt)|css|images|js)/
        security: false

    admin_area:
        pattern:    ^/admin.*
        form_login:
            check_path: /admin/login_check
            login_path: /admin/login
        logout:
            path:   /admin/logout
            target: /admin
        anonymous: ~
        logout: true
        http_basic:
            realm: "Secured Admin Area"
        provider: admin 

access_control:
    - { path: ^/admin/login.*, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/admin.*, role: ROLE_ADMIN }

The code of my controller looks like the code in symfony 2.0 docs http://symfony.com/doc/current/book/security.html#using-a-traditional-login-form

My routes

DomiconAdminBundle_login:
pattern:  /login
defaults: { _controller: DomiconAdminBundle:User:login }

DomiconAdminBundle_login_check:
    pattern:   /login_check

The login form is displayed correctly, but if I submit the form the application redirects back to the login form and the url has changed to

http://localhost/Domicon/web/app_dev.php/admin/login?_username=admin&_password=pass

I got no error or anything else. I don't understand why I get no error and why the url changed to this.

1

1 Answers

2
votes

I think you forgot to give the action in <form> tag and you need to tell where to redirect if the login is success.

And another thing I notice that You had given /login in routing.yml and in security.yml you had given /admin/login.

Third one is login_path: /admin/login_check in security.yml and in routing.yml you had given

/login_check.

Change /login to /admin/login in routing.yml

Change /login_check to /admin/login_check in routing.yml

<form action="{{ path('DomiconAdminBundle_login_check') }}" method="post">
  <div>  
    <label for="username">Username:</label>
    <input type="text" id="username" name="_username" value="{{ last_username }}" />
    </div>
  <div> 
    <label for="password">Password:</label>
    <input type="password" id="password" name="_password" />
</div>
    {#
        If you want to control the URL the user is redirected to on success #}
        <input type="hidden" name="_target_path" value="/yourredirectpath" />


    <input type="submit" name="login" value="Submit" />
</form>

Hope this helps.