Symfony2 FosUserBundle is loosing session

356 views Asked by At

I am using FosUserBundle to register and authenticate users in my website. But i am having troubles with it cause user session is loosen very soon. Sometimes 5 minutes, sometimes 10 minutes, and then user is redirected to the login form again, causing many users to left my website.

This is what i currently have in my project:

composer.json

{"require": {
        "php": ">=5.3.3",
        "symfony/symfony": "2.6.*",
        "doctrine/orm": "~2.2,>=2.2.3,<2.5",
        "doctrine/dbal": "<2.5",
        "doctrine/doctrine-bundle": "~1.2",
        "twig/extensions": "~1.0",
        "symfony/assetic-bundle": "~2.3",
        "symfony/swiftmailer-bundle": "~2.3",
        "symfony/monolog-bundle": "~2.4",
        "symfony/yaml": "2.7.*@dev",
        "symfony/filesystem": "~2.6",
        "sensio/distribution-bundle": "~3.0,>=3.0.12",
        "sensio/framework-extra-bundle": "~3.0,>=3.0.2",
        "incenteev/composer-parameter-handler": "~2.0",
        "friendsofsymfony/user-bundle": "~2.0@dev",
        "friendsofsymfony/jsrouting-bundle": "@stable",
        "knplabs/knp-snappy-bundle": "dev-master",
        "jms/serializer-bundle": "0.13.*"
    },
    "require-dev": {
      "sensio/generator-bundle": "~2.3"
    }
}

config.yml

imports:
    - { resource: parameters.yml }
    - { resource: security.yml }
    - { resource: "@MoovityUserBundle/Resources/config/services.yml" }
    - { resource: "@MoovityBackendBundle/Resources/config/services.yml" }

framework:
    #esi:             ~
    translator:      { fallback: "%locale%" }
    secret:          "%secret%"
    router:
        resource: "%kernel.root_dir%/config/routing.yml"
        strict_requirements: ~
    form:            ~
    csrf_protection: ~
    validation:      { enable_annotations: true }
    templating:
        engines: ['twig']
        #assets_version: SomeVersionScheme
    default_locale:  "%locale%"
    trusted_hosts:   ~
    trusted_proxies: ~
    session:
        # handler_id set to null will use default session handler from php.ini
        handler_id:  ~
        cookie_lifetime: 0
        gc_maxlifetime: 14400 
    fragments:       ~
    http_method_override: true
#    translator: ~

# Twig Configuration
twig:
    debug:            "%kernel.debug%"
    strict_variables: "%kernel.debug%"

# Assetic Configuration
assetic:
    debug:          "%kernel.debug%"
    use_controller: false
    bundles:        [ MoovityBackendBundle ]
    #java: /usr/bin/java
    filters:
        cssrewrite: ~
        #closure:
        #    jar: "%kernel.root_dir%/Resources/java/compiler.jar"
        #yui_css:
        #    jar: "%kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar"

# Doctrine Configuration
doctrine:
    dbal:
        driver:   "%database_driver%"
        host:     "%database_host%"
        port:     "%database_port%"
        dbname:   "%database_name%"
        user:     "%database_user%"
        password: "%database_password%"
        charset: UTF8
        # if using pdo_sqlite as your database driver, add the path in parameters.yml
        # e.g. database_path: "%kernel.root_dir%/data/data.db3"
        # path:     "%database_path%"

    orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        auto_mapping: true

# Swiftmailer Configuration
swiftmailer:
    transport: "%mailer_transport%"
    host:      "%mailer_host%"
    username:  "%mailer_user%"
    password:  "%mailer_password%"
    spool:     { type: memory }

fos_user:
    db_driver: orm 
    firewall_name: main
    user_class: Moovity\UserBundle\Entity\User
    registration:
        confirmation:
            enabled: true
            from_email:
                address: [email protected]
                sender_name: mywebsite
            template: MoovityBackendBundle:Mail:mail_fos_user_register_template.html.twig
#            template: FOSUserBundle:Registration:email.html.twig
        form:
            type: moovity_user_registration
    resetting:
        email:
            from_email:
                address:        [email protected]
                sender_name:    My Website Reset Password
            template: FOSUserBundle:Resetting:email.html.twig
        form:
            type: moovity_user_resetting
    service:
        mailer: fos_user.mailer.twig_swift

#fos_rest:
#    routing_loader:
#        default_format: json

security.yml

security:
    encoders:
        FOS\UserBundle\Model\UserInterface: sha512

    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: ROLE_ADMIN

    providers:
        fos_userbundle:
#            id: fos_user.user_provider.username
            id: fos_user.user_provider.username_email

    firewalls:
        main:
            pattern: ^/
            form_login:
                login_path: fos_user_registration_register
                check_path: fos_user_security_check
                provider: fos_userbundle
                csrf_provider: form.csrf_provider
#                always_use_default_target_path: true
                # Redirección despues de loguear
                default_target_path: moovity_backend_homepage
            logout:       
                path: fos_user_security_logout
                target: fos_user_registration_register 
#                target: fos_user_security_login

            anonymous:    true
            security: true
            remember_me:
                key:      57361cdee0bb68f4e4b71f06b28fc07e
                lifetime: 6000

What am i doing wrong?

2

There are 2 answers

0
walolinux On BEST ANSWER

Ok, i have noticed that fosuserbundle is loosing session with Symfony 2.6, so i switched to 2.3 and now it seems to be working.

Thank you very much.

0
cRsakaWolf On

I think the problem is the translation. Got the same errors, try to remove the locale in your parameter and reset it with ~

Please post if it was the problem