mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
Scaffold registration concept for register-or-login
This commit is contained in:
parent
74add716ad
commit
fcd9205b33
@ -12,6 +12,7 @@
|
|||||||
|
|
||||||
namespace App\Livewire\BillingPortal\Authentication;
|
namespace App\Livewire\BillingPortal\Authentication;
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Validator;
|
||||||
use Livewire\Component;
|
use Livewire\Component;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
use App\Models\Subscription;
|
use App\Models\Subscription;
|
||||||
@ -41,9 +42,12 @@ class RegisterOrLogin extends Component
|
|||||||
'otp' => false, // Use as preference. E-mail/password or OTP.
|
'otp' => false, // Use as preference. E-mail/password or OTP.
|
||||||
'login_form' => false,
|
'login_form' => false,
|
||||||
'otp_form' => false,
|
'otp_form' => false,
|
||||||
|
'register_form' => false,
|
||||||
'initial_completed' => false,
|
'initial_completed' => false,
|
||||||
];
|
];
|
||||||
|
|
||||||
|
public array $registration_fields = [];
|
||||||
|
|
||||||
public function initial()
|
public function initial()
|
||||||
{
|
{
|
||||||
$this->validateOnly('email', ['email' => 'required|bail|email:rfc']);
|
$this->validateOnly('email', ['email' => 'required|bail|email:rfc']);
|
||||||
@ -68,13 +72,7 @@ class RegisterOrLogin extends Component
|
|||||||
}
|
}
|
||||||
|
|
||||||
$this->state['login_form'] = false;
|
$this->state['login_form'] = false;
|
||||||
|
$this->state['register_form'] = true;
|
||||||
$contact = $this->createClientContact();
|
|
||||||
|
|
||||||
auth()->guard('contact')->loginUsingId($contact->id, true);
|
|
||||||
|
|
||||||
$this->dispatch('purchase.context', property: 'contact', value: $contact);
|
|
||||||
$this->dispatch('purchase.next');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function handlePassword()
|
public function handlePassword()
|
||||||
@ -150,16 +148,17 @@ class RegisterOrLogin extends Component
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$contact = $this->createClientContact();
|
$this->state['otp_form'] = false;
|
||||||
|
$this->state['register_form'] = true;
|
||||||
auth()->guard('contact')->loginUsingId($contact->id, true);
|
|
||||||
|
|
||||||
$this->dispatch('purchase.context', property: 'contact', value: $contact);
|
|
||||||
$this->dispatch('purchase.next');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function createClientContact()
|
private function createClientContact(array $data)
|
||||||
{
|
{
|
||||||
|
$fields = [
|
||||||
|
'client' => collect($data)->filter(fn($value, $key) => str_starts_with($key, 'client_'))->mapWithKeys(fn($value, $key) => [str_replace('client_', '', $key) => $value])->toArray(),
|
||||||
|
'contact' => collect($data)->filter(fn($value, $key) => str_starts_with($key, 'contact_'))->mapWithKeys(fn($value, $key) => [str_replace('contact_', '', $key) => $value])->toArray(),
|
||||||
|
];
|
||||||
|
|
||||||
$company = $this->subscription->company;
|
$company = $this->subscription->company;
|
||||||
$user = $this->subscription->user;
|
$user = $this->subscription->user;
|
||||||
$user->setCompany($company);
|
$user->setCompany($company);
|
||||||
@ -169,10 +168,11 @@ class RegisterOrLogin extends Component
|
|||||||
'name' => '',
|
'name' => '',
|
||||||
'group_settings_id' => $this->subscription->group_id,
|
'group_settings_id' => $this->subscription->group_id,
|
||||||
'contacts' => [
|
'contacts' => [
|
||||||
['email' => $this->email],
|
['email' => $this->email, ...$fields['contact']],
|
||||||
],
|
],
|
||||||
'client_hash' => Str::random(40),
|
'client_hash' => Str::random(40),
|
||||||
'settings' => ClientSettings::defaults(),
|
'settings' => ClientSettings::defaults(),
|
||||||
|
...$fields['client'],
|
||||||
];
|
];
|
||||||
|
|
||||||
$client = $client_repo->save($data, ClientFactory::create($company->id, $user->id));
|
$client = $client_repo->save($data, ClientFactory::create($company->id, $user->id));
|
||||||
@ -182,6 +182,56 @@ class RegisterOrLogin extends Component
|
|||||||
return $contact;
|
return $contact;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function register(array $data)
|
||||||
|
{
|
||||||
|
|
||||||
|
$rules = collect($this->registrationFields() ?? [])->mapWithKeys(function ($field, $value) {
|
||||||
|
return [$field['key'] => $field['required'] ? ['required'] : []];
|
||||||
|
})->toArray();
|
||||||
|
|
||||||
|
$data = Validator::make($data, $rules)->validate();
|
||||||
|
|
||||||
|
$contact = $this->createClientContact($data);
|
||||||
|
|
||||||
|
auth()->guard('contact')->loginUsingId($contact->id, true);
|
||||||
|
|
||||||
|
$this->dispatch('purchase.context', property: 'contact', value: $contact);
|
||||||
|
$this->dispatch('purchase.next');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function registrationFields()
|
||||||
|
{
|
||||||
|
$contact = ['first_name', 'last_name', 'email'];
|
||||||
|
|
||||||
|
$defaults = [
|
||||||
|
'contact_email' => $this->email,
|
||||||
|
];
|
||||||
|
|
||||||
|
return collect($this->subscription->company->client_registration_fields)
|
||||||
|
->filter(fn($field) => $field['visible'] || $field['required'])
|
||||||
|
->map(function ($field) use ($contact) {
|
||||||
|
return [
|
||||||
|
'key' => in_array($field['key'], $contact) ? "contact_{$field['key']}" : "client_{$field['key']}",
|
||||||
|
'label' => ctrans("texts.{$field['key']}"),
|
||||||
|
'defaultValue' => null,
|
||||||
|
'required' => $field['required'],
|
||||||
|
'type' => str_ends_with($field['key'], 'email')
|
||||||
|
? 'email' : (str_ends_with($field['key'], 'phone')
|
||||||
|
? 'tel' : (str_ends_with($field['key'], 'password')
|
||||||
|
? 'password'
|
||||||
|
: 'text')
|
||||||
|
),
|
||||||
|
];
|
||||||
|
})
|
||||||
|
->mapWithKeys(fn($field) => [
|
||||||
|
$field['key'] => [
|
||||||
|
...$field,
|
||||||
|
'defaultValue' => $defaults[$field['key']] ?? null,
|
||||||
|
]
|
||||||
|
])
|
||||||
|
->toArray();
|
||||||
|
}
|
||||||
|
|
||||||
public function mount()
|
public function mount()
|
||||||
{
|
{
|
||||||
if (auth()->guard('contact')->check()) {
|
if (auth()->guard('contact')->check()) {
|
||||||
|
@ -88,4 +88,29 @@
|
|||||||
</button>
|
</button>
|
||||||
</form>
|
</form>
|
||||||
@endif
|
@endif
|
||||||
|
|
||||||
|
@if($state['register_form'])
|
||||||
|
<form wire:submit="register(Object.fromEntries(new FormData($event.target)))" class="space-y-3">
|
||||||
|
@csrf
|
||||||
|
|
||||||
|
@foreach($this->registrationFields() as $field)
|
||||||
|
<div>
|
||||||
|
<span class="input-label">{{ $field['label'] }}</span>
|
||||||
|
<input type="{{ $field['type'] }}" name="{{ $field['key'] }}" value="{{ $field['defaultValue'] }}" class="input w-full" />
|
||||||
|
|
||||||
|
@error($field['key'])
|
||||||
|
<p class="validation validation-fail block w-full" role="alert">
|
||||||
|
{{ $message }}
|
||||||
|
</p>
|
||||||
|
@enderror
|
||||||
|
</div>
|
||||||
|
@endforeach
|
||||||
|
|
||||||
|
<button
|
||||||
|
type="submit"
|
||||||
|
class="button button-block bg-primary text-white mt-4">
|
||||||
|
{{ ctrans('texts.next') }}
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
@endif
|
||||||
</div>
|
</div>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user