Skip to content

Commit fc2b7b0

Browse files
committed
Support additional attributes during registration flow
1 parent be4e65a commit fc2b7b0

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed

src/Auth/RegistersUsersWithIdentity.php

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Http\Response;
88
use Illuminate\Support\Facades\Auth;
99
use Illuminate\Foundation\Auth\RedirectsUsers;
10+
use Illuminate\Support\Arr;
1011
use Illuminate\Support\Facades\DB;
1112
use Oneofftech\Identities\Facades\IdentityCrypt;
1213
use 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
}

src/View/Components/IdentityLink.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Oneofftech\Identities\View\Components;
44

5+
use Illuminate\Support\Arr;
56
use Illuminate\View\Component;
67
use InvalidArgumentException;
78

@@ -35,16 +36,24 @@ class IdentityLink extends Component
3536
*/
3637
public $label;
3738

39+
/**
40+
* Additional parameters to append on the redirect request
41+
*
42+
* @var array
43+
*/
44+
public $parameters;
45+
3846
/**
3947
* Create a new component instance.
4048
*
4149
* @param string $provider The identity provider
4250
* @param string $action The action the button should perform. Available: login, register. Default login.
4351
* @param string $label The label for the link. It will be used as string to localize.
4452
* Default null a default label in the form "$action via $provider" will be used.
53+
* @param array|null $parameter Additional parameters to append on the redirect request
4554
* @return self
4655
*/
47-
public function __construct($provider, $action = 'login', $label = null)
56+
public function __construct($provider, $action = 'login', $label = null, $parameters = null)
4857
{
4958
if (! in_array($action, self::$availableActions)) {
5059
throw new InvalidArgumentException("Specified action [$action] is not supported.");
@@ -53,6 +62,7 @@ public function __construct($provider, $action = 'login', $label = null)
5362
$this->provider = $provider;
5463
$this->action = $action;
5564
$this->label = $label ?? (self::$actionLabels[$this->action] ?? ucfirst($action));
65+
$this->parameters = Arr::wrap($parameters) ?? [];
5666
}
5767

5868
/**
@@ -63,7 +73,7 @@ public function __construct($provider, $action = 'login', $label = null)
6373
public function render()
6474
{
6575
return <<<'blade'
66-
<a href="{{ route('oneofftech::' . $action . '.provider', ['provider' => $provider]) }}" {{ $attributes }}>
76+
<a href="{{ route('oneofftech::' . $action . '.provider', array_merge($parameters, ['provider' => $provider])) }}" {{ $attributes }}>
6777
{{ __($label, ['provider' => $provider]) }}
6878
</a>
6979
blade;

0 commit comments

Comments
 (0)