@@ -54,6 +54,63 @@ Don't forget to generate and run the migration:
54
54
$ php bin/console make:migration
55
55
$ php bin/console doctrine:migrations:migrate
56
56
57
+ Next, configure your "user provider" to use this new ``apiToken `` property:
58
+
59
+ .. configuration-block ::
60
+
61
+ .. code-block :: yaml
62
+
63
+ # config/packages/security.yaml
64
+ security :
65
+ # ...
66
+
67
+ providers :
68
+ your_db_provider :
69
+ entity :
70
+ class : App\Entity\User
71
+ property : apiToken
72
+
73
+ # ...
74
+
75
+ .. code-block :: xml
76
+
77
+ <!-- config/packages/security.xml -->
78
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
79
+ <srv : container xmlns =" http://symfony.com/schema/dic/security"
80
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
81
+ xmlns : srv =" http://symfony.com/schema/dic/services"
82
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
83
+ https://symfony.com/schema/dic/services/services-1.0.xsd" >
84
+
85
+ <config >
86
+ <!-- ... -->
87
+
88
+ <provider name =" your_db_provider" >
89
+ <entity class =" App\Entity\User" property =" apiToken" />
90
+ </provider >
91
+
92
+ <!-- ... -->
93
+ </config >
94
+ </srv : container >
95
+
96
+ .. code-block :: php
97
+
98
+ // config/packages/security.php
99
+ $container->loadFromExtension('security', [
100
+ // ...
101
+
102
+ 'providers' => [
103
+ 'your_db_provider' => [
104
+ 'entity' => [
105
+ 'class' => 'App\Entity\User',
106
+ 'property' => 'apiToken',
107
+ ],
108
+ ],
109
+ ],
110
+
111
+ // ...
112
+ ]);
113
+
57
114
Step 2) Create the Authenticator Class
58
115
--------------------------------------
59
116
@@ -113,10 +170,10 @@ This requires you to implement several methods::
113
170
return null;
114
171
}
115
172
116
- // if a User is returned, checkCredentials() is called
117
- return $this->em->getRepository(User::class)
118
- ->findOneBy(['apiToken' => $credentials])
119
- ;
173
+ // The "username" in this case is the apiToken, see the key `property`
174
+ // of `your_db_provider` in `security.yaml`.
175
+ // If this returns a user, checkCredentials() is called next:
176
+ return $userProvider->loadUserByUsername($apiToken) ;
120
177
}
121
178
122
179
public function checkCredentials($credentials, UserInterface $user)
0 commit comments