@@ -129,12 +129,8 @@ The ``remember_me`` firewall defines the following configuration options:
129129 end user.
130130
131131``token_provider `` (default value: ``null ``)
132- Defines the service id of a token provider to use. By default, tokens are
133- stored in a cookie. For example, you might want to store the token in a
134- database, to not have a (hashed) version of the password in a cookie. The
135- DoctrineBridge comes with a
136- ``Symfony\Bridge\Doctrine\Security\RememberMe\DoctrineTokenProvider `` that
137- you can use.
132+ Defines the service id of a token provider to use. If you want to store tokens
133+ in the database, see :ref: `remember-me-token-in-database `.
138134
139135Forcing the User to Opt-Out of the Remember Me Feature
140136------------------------------------------------------
@@ -195,3 +191,116 @@ users to change their password. You can do this by leveraing a few special "role
195191
196192 // ...
197193 }
194+
195+ .. _remember-me-token-in-database :
196+
197+ Storing Remember Me Tokens in the Database
198+ ------------------------------------------
199+
200+ The token contents, including the hashed version of the user password, are
201+ stored by default in cookies. If you prefer to store them in a database, use the
202+ :class: `Symfony\\ Bridge\\ Doctrine\\ Security\\ RememberMe\\ DoctrineTokenProvider `
203+ class provided by the Doctrine Bridge.
204+
205+ First, you need to register ``DoctrineTokenProvider `` as a service:
206+
207+ .. configuration-block ::
208+
209+ .. code-block :: yaml
210+
211+ # config/services.yaml
212+ services :
213+ # ...
214+
215+ Symfony\Bridge\Doctrine\Security\RememberMe\DoctrineTokenProvider : ~
216+
217+ .. code-block :: xml
218+
219+ <!-- config/services.xml -->
220+ <?xml version =" 1.0" encoding =" UTF-8" ?>
221+ <container xmlns =" http://symfony.com/schema/dic/services"
222+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
223+ xsi : schemaLocation =" http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd" >
224+
225+ <services >
226+ <service id =" Symfony\Bridge\Doctrine\Security\RememberMe\DoctrineTokenProvider" />
227+ </services >
228+ </container >
229+
230+ .. code-block :: php
231+
232+ // config/services.php
233+ use Symfony\Bridge\Doctrine\Security\RememberMe\DoctrineTokenProvider;
234+
235+ $container->register(DoctrineTokenProvider::class);
236+
237+ Then you need to create a table with the following structure in your database
238+ so ``DoctrineTokenProvider `` can store the tokens:
239+
240+ .. code-block :: sql
241+
242+ CREATE TABLE `rememberme_token` (
243+ `series` char(88) UNIQUE PRIMARY KEY NOT NULL,
244+ `value` char(88) NOT NULL,
245+ `lastUsed` datetime NOT NULL,
246+ `class` varchar(100) NOT NULL,
247+ `username` varchar(200) NOT NULL
248+ );
249+
250+ Finally, set the ``token_provider `` option of the ``remember_me `` config to the
251+ service you just created:
252+
253+ .. configuration-block ::
254+
255+ .. code-block :: yaml
256+
257+ # config/packages/security.yaml
258+ security :
259+ # ...
260+
261+ firewalls :
262+ main :
263+ # ...
264+ remember_me :
265+ # ...
266+ token_provider : ' @Symfony\Bridge\Doctrine\Security\RememberMe\DoctrineTokenProvider'
267+
268+ .. code-block :: xml
269+
270+ <!-- config/packages/security.xml -->
271+ <?xml version =" 1.0" encoding =" UTF-8" ?>
272+ <srv : container xmlns =" http://symfony.com/schema/dic/security"
273+ xmlns : srv =" http://symfony.com/schema/dic/services"
274+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
275+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
276+ https://symfony.com/schema/dic/services/services-1.0.xsd" >
277+
278+ <config >
279+ <!-- ... -->
280+
281+ <firewall name =" main" >
282+ <!-- ... -->
283+
284+ <remember-me
285+ token_profider =" @Symfony\Bridge\Doctrine\Security\RememberMe\DoctrineTokenProvider"
286+ />
287+ </firewall >
288+ </config >
289+ </srv : container >
290+
291+ .. code-block :: php
292+
293+ // config/packages/security.php
294+ $container->loadFromExtension('security', [
295+ // ...
296+
297+ 'firewalls' => [
298+ 'main' => [
299+ // ...
300+ 'remember_me' => [
301+ // ...
302+ 'token_provider' => '@Symfony\Bridge\Doctrine\Security\RememberMe\DoctrineTokenProvider',
303+ ],
304+ ],
305+ ],
306+ ]);
0 commit comments