OTP for subscriptions

This commit is contained in:
David Bomba 2022-12-10 12:28:54 +11:00
parent 81e8997e2c
commit 9a0a55d356
4 changed files with 104 additions and 6 deletions

View File

@ -17,6 +17,7 @@ use App\Jobs\Mail\NinjaMailerJob;
use App\Jobs\Mail\NinjaMailerObject; use App\Jobs\Mail\NinjaMailerObject;
use App\Libraries\MultiDB; use App\Libraries\MultiDB;
use App\Mail\ContactPasswordlessLogin; use App\Mail\ContactPasswordlessLogin;
use App\Mail\Subscription\OtpCode;
use App\Models\Client; use App\Models\Client;
use App\Models\ClientContact; use App\Models\ClientContact;
use App\Models\Invoice; use App\Models\Invoice;
@ -229,12 +230,22 @@ class BillingPortalPurchasev2 extends Component
$this->resetValidation('login'); $this->resetValidation('login');
} }
public function handleLogin() public function handleLogin($user_code)
{ {
$this->resetErrorBag('login');
$this->resetValidation('login');
$code = Cache::get("subscriptions:otp:{$this->email}"); $code = Cache::get("subscriptions:otp:{$this->email}");
$this->validateOnly('login', ['login' => ['required',Rule::in([$code])]], ['login' => ctrans('texts.invalid_code')]); // $this->validateOnly('login', ['login' => 'required'], ['login' => ctrans('texts.invalid_code')]);
if($user_code != $code){
nlog($code);
nlog($user_code);
$errors = $this->getErrorBag();
$errors->add('login', ctrans('texts.invalid_code'));
}
$contact = ClientContact::where('email', $this->email)->first(); $contact = ClientContact::where('email', $this->email)->first();
@ -243,9 +254,13 @@ class BillingPortalPurchasev2 extends Component
$this->contact = $contact; $this->contact = $contact;
} }
else { else {
$this->createClientContact();
}
} }
public function showClientRequiredFields()
{
} }
@ -259,8 +274,25 @@ class BillingPortalPurchasev2 extends Component
Cache::put($email_hash, $rand, 120); Cache::put($email_hash, $rand, 120);
$this->emailOtpCode($rand);
} }
private function emailOtpCode($code)
{
$cc = new ClientContact();
$cc->email = $this->email;
$nmo = new NinjaMailerObject;
$nmo->mailable = new OtpCode($this->subscription->company, $this->contact, $code);
$nmo->company = $this->subscription->company;
$nmo->settings = $this->subscription->company->settings;
$nmo->to_user = $cc;
NinjaMailerJob::dispatch($nmo);
}
/** /**
* Handle a coupon being entered into the checkout * Handle a coupon being entered into the checkout
*/ */
@ -411,9 +443,9 @@ class BillingPortalPurchasev2 extends Component
$this->contact = $client->fresh()->contacts()->first(); $this->contact = $client->fresh()->contacts()->first();
Auth::guard('contact')->loginUsingId($this->contact->id, true); Auth::guard('contact')->loginUsingId($this->contact->id, true);
return $this;
} }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public function updated($propertyName) public function updated($propertyName)
{ {
@ -424,6 +456,8 @@ class BillingPortalPurchasev2 extends Component
} }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public function rules() public function rules()
{ {
$rules = [ $rules = [

View File

@ -28,7 +28,6 @@ class GenericReportRequest extends Request
public function rules() public function rules()
{ {
nlog($this->date_range);
return [ return [
'date_range' => 'bail|required|string', 'date_range' => 'bail|required|string',

View File

@ -0,0 +1,63 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Mail\Subscription;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\App;
class OtpCode extends Mailable
{
// use Queueable, SerializesModels;
public $company;
public $contact;
public $code;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($company, $contact, $code)
{
$this->company = $company;
$this->contact = $contact;
$this->code = $code;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
App::setLocale($this->company->locale());
return $this->from(config('mail.from.address'), config('mail.from.name'))
->subject(ctrans('texts.otp_code_subject'))
->text('email.admin.generic_text')
->view('email.admin.generic')
->with([
'settings' => $this->company->settings,
'logo' => $this->company->present()->logo(),
'title' => ctrans('texts.otp_code_subject'),
'content' => ctrans('texts.otp_code_body', ['code' => $this->code]),
'whitelabel' => $this->company->account->isPaid(),
]);
}
}

View File

@ -4895,7 +4895,9 @@ $LANG = array(
'delete_bank_account' => 'Delete Bank Account', 'delete_bank_account' => 'Delete Bank Account',
'archive_transaction' => 'Archive Transaction', 'archive_transaction' => 'Archive Transaction',
'delete_transaction' => 'Delete Transaction', 'delete_transaction' => 'Delete Transaction',
'otp_code_message' => 'Enter the code emailed.' 'otp_code_message' => 'Enter the code emailed.',
'otp_code_subject' => 'Your one time passcode code',
'otp_code_body' => 'Your one time passcode is :code',
); );
return $LANG; return $LANG;