Christophe Christophe - 18 days ago 7
PHP Question

FOS user bundle doesn't keep logged session

I have integrated the FOS user bundle just as the quick guide tells you to do. it seem to work except that once I log in and redirects it loses the session for some reason and I'm back as anonymous user.

This is what i get when I log in before I'm redirect back:
enter image description here

As you can see I'm successfully logged in and should be redirect to the homepage. However when I'm on the homepage I'm redirected back to the login because I check if the user is logged in or not. So somehow it does not remember that I logged in.

This is my configuration for security.xml

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

role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
# http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers
providers:
in_memory:
memory: ~
fos_userbundle:
id: fos_user.user_provider.username

firewalls:
main:
pattern: ^/
form_login:
check_path: /login_check
login_path: /login
provider: fos_userbundle
default_target_path: /
logout: true
anonymous: true

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


config.yml

fos_user:
db_driver: orm # other valid values are 'mongodb', 'couchdb' and 'propel'
firewall_name: main
user_class: UserBundle\Entity\User


I obviously added the bundle to the app kernel and created the user entity. when i run the doctrine command it successfully creates the user table, etc...

I have this working fine in another project, the only difference is that for this project I'm using docker. Would this cause a problem?

Edit:
This is the code I use to check if the user is logged in:

if(!$this->container->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY') ){
return $this->redirect($this->generateUrl('fos_user_security_login'));
}


And the toolbar shows me as anonymous after the login_check redirects.

enter image description here

Answer

It was brought to my attention that if mysave_path is under /var/www/project which is mounted on my local machine it would not work.

So in config.yml I commented out the handler_id and changed thesave_path value to ~

Comments