The Mysterious "User Provider"
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
Let's see that error again: change intercept_redirects
back to false
:
// ... lines 1 - 12 | |
web_profiler: | |
// ... line 14 | |
intercept_redirects: false | |
// ... lines 16 - 49 |
Refresh and re-post the form. Oof, there it is again:
There is no user provider for user
AppBundle\Entity\User
.
What the heck is a user provider and why do we need one?
What is a User Provider?
A user provider is one of the most misunderstood parts of Symfony's security. It's
an object that does just a few small jobs for you. For example, the user provider
is responsible for loading the User
from the session and making sure that it's
up to date. In Doctrine, we'll want our's to re-query for a fresh User
object to
make sure all the data is still up-to-date.
The user provider is also responsible for a few other minor things, like handling "remember me" functionality and a really cool feature we'll talk about later called "impersonation".
Long story short: you need a user provider, but it's not all that important. And if you're using Doctrine, it's super easy to setup.
Setting up the Entity User Provider
In security.yml
, you already have a providers
section - as in "user providers".
Delete the in_memory
stuff and replace it with our_users
: that's a totally meaningless
machine name - it could be anything. But below that, say entity
and set it to
{ class: AppBundle\Entity\User, property: email }
:
// ... lines 1 - 2 | |
security: | |
# http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers | |
providers: | |
our_users: | |
entity: { class: AppBundle\Entity\User, property: email } | |
// ... lines 9 - 28 |
The property
part is not something we care about right now, but we will use
and talk about it later.
But yea, that's it! Go back to /login
. Right now, I am not logged in. But try
logging in again.
It's alive!!! We can finally surf around the site and stay logged in. Cool.
Custom User Provider
In your app, if you're not loading users from the database, then you'll need to
create a custom user provider class that implements UserProviderInterface
. Check
out the official docs in this case. But if you have any questions, let me know.
Yo SfCasts team,
I have been learning a lot, and so far things have been going easy, but now i'm stuck/puzzled.
I made a login form, made the authenticator, and after login, I am properly authenticated and authorized with a GuardToken.
Then the redirect happens, and I still seem to be authorized, have roles, but Authenticated is "false".
Any other action after that wipes out my login and redirects me to "/".
To give to further details, the solution i'm trying to get to is a retrofit. Before Drupal did the authorization (their login form), but now we want to do it ourselves. We have a full-fledged User class, it's called 'Account', but implements UserInterface (also Serializable btw).
And we already had an entity provider in security.yml.
I'm not sure where it's going wrong, i'm thinking the serializer was not sending the correct stuff, but when removing the custom serialize (and un), after login I'm Authenticated, but my user is now unknown (blank)? Do I need a custom user provider?
Hopefully you can help, before I figure it out myself ;-)
Source: https://github.com/griidc/p...