77use Illuminate \Http \Response ;
88use Illuminate \Support \Facades \Auth ;
99use Illuminate \Foundation \Auth \RedirectsUsers ;
10+ use Illuminate \Support \Arr ;
1011use Illuminate \Support \Facades \DB ;
1112use Oneofftech \Identities \Facades \IdentityCrypt ;
1213use Illuminate \Validation \ValidationException ;
@@ -29,9 +30,11 @@ public function redirect(Request $request, $provider)
2930 // probably have the referrer header set
3031 // and in case of validation errors the
3132 // referrer has precedence over _previous.url
32- // $request->session()->put('_oot.identities.previous_url', url()->previous());
3333 $ this ->savePreviousUrl ();
3434
35+ // get additional user defined attributes
36+ $ this ->pushAttributes ($ request );
37+
3538 return Identity::driver ($ provider )
3639 ->redirectUrl (route ('oneofftech::register.callback ' , ['provider ' => $ provider ]))
3740 ->redirect ();
@@ -65,7 +68,7 @@ public function register(Request $request, $provider)
6568 /**
6669 * @var \Illuminate\Contracts\Validation\Validator
6770 */
68- $ validator = $ this ->validator ($ this ->map ($ oauthUser ));
71+ $ validator = $ this ->validator ($ this ->map ($ request , $ oauthUser ));
6972
7073 if ($ validator ->fails ()) {
7174
@@ -102,13 +105,15 @@ public function register(Request $request, $provider)
102105 * @param SocialiteUser $oauthUser
103106 * @return array
104107 */
105- protected function map (SocialiteUser $ oauthUser )
108+ protected function map (Request $ request , SocialiteUser $ oauthUser )
106109 {
107- return [
110+ $ user = [
108111 'name ' => $ oauthUser ->getName () ?? $ oauthUser ->getNickname (),
109112 'email ' => $ oauthUser ->getEmail (),
110113 'avatar ' => $ oauthUser ->getAvatar (),
111114 ];
115+
116+ return array_merge ($ user , $ this ->pullAttributes ($ request ));
112117 }
113118
114119 protected function createIdentity ($ user , $ provider , $ oauthUser )
@@ -158,4 +163,41 @@ protected function guard()
158163 {
159164 return Auth::guard ();
160165 }
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+ }
161203}
0 commit comments