7
7
use Illuminate \Http \Response ;
8
8
use Illuminate \Support \Facades \Auth ;
9
9
use Illuminate \Foundation \Auth \RedirectsUsers ;
10
+ use Illuminate \Support \Arr ;
10
11
use Illuminate \Support \Facades \DB ;
11
12
use Oneofftech \Identities \Facades \IdentityCrypt ;
12
13
use Illuminate \Validation \ValidationException ;
@@ -29,9 +30,11 @@ public function redirect(Request $request, $provider)
29
30
// probably have the referrer header set
30
31
// and in case of validation errors the
31
32
// referrer has precedence over _previous.url
32
- // $request->session()->put('_oot.identities.previous_url', url()->previous());
33
33
$ this ->savePreviousUrl ();
34
34
35
+ // get additional user defined attributes
36
+ $ this ->pushAttributes ($ request );
37
+
35
38
return Identity::driver ($ provider )
36
39
->redirectUrl (route ('oneofftech::register.callback ' , ['provider ' => $ provider ]))
37
40
->redirect ();
@@ -65,7 +68,7 @@ public function register(Request $request, $provider)
65
68
/**
66
69
* @var \Illuminate\Contracts\Validation\Validator
67
70
*/
68
- $ validator = $ this ->validator ($ this ->map ($ oauthUser ));
71
+ $ validator = $ this ->validator ($ this ->map ($ request , $ oauthUser ));
69
72
70
73
if ($ validator ->fails ()) {
71
74
@@ -102,13 +105,15 @@ public function register(Request $request, $provider)
102
105
* @param SocialiteUser $oauthUser
103
106
* @return array
104
107
*/
105
- protected function map (SocialiteUser $ oauthUser )
108
+ protected function map (Request $ request , SocialiteUser $ oauthUser )
106
109
{
107
- return [
110
+ $ user = [
108
111
'name ' => $ oauthUser ->getName () ?? $ oauthUser ->getNickname (),
109
112
'email ' => $ oauthUser ->getEmail (),
110
113
'avatar ' => $ oauthUser ->getAvatar (),
111
114
];
115
+
116
+ return array_merge ($ user , $ this ->pullAttributes ($ request ));
112
117
}
113
118
114
119
protected function createIdentity ($ user , $ provider , $ oauthUser )
@@ -158,4 +163,41 @@ protected function guard()
158
163
{
159
164
return Auth::guard ();
160
165
}
166
+
167
+ /**
168
+ * The attributes that should be retrieved from
169
+ * the request to append to the redirect
170
+ *
171
+ * @var array
172
+ */
173
+ protected function redirectAttributes ()
174
+ {
175
+ if (method_exists ($ this , 'attributes ' )) {
176
+ return $ this ->attributes ();
177
+ }
178
+
179
+ return property_exists ($ this , 'attributes ' ) ? $ this ->attributes : [];
180
+ }
181
+
182
+ protected function pushAttributes ($ request )
183
+ {
184
+ $ attributes = $ this ->redirectAttributes () ?? [];
185
+
186
+ if (empty ($ attributes )) {
187
+ return ;
188
+ }
189
+
190
+ $ request ->session ()->put ('_oot.identities.attributes ' , json_encode ($ request ->only ($ attributes )));
191
+ }
192
+
193
+ protected function pullAttributes ($ request )
194
+ {
195
+ $ attributes = $ this ->redirectAttributes () ?? [];
196
+
197
+ if (empty ($ attributes )) {
198
+ return [];
199
+ }
200
+
201
+ return json_decode ($ request ->session ()->pull ('_oot.identities.attributes ' ), true );
202
+ }
161
203
}
0 commit comments