add otp wa, email
This commit is contained in:
parent
dc4ebad5a6
commit
79a2bf7fce
|
|
@ -79,4 +79,7 @@ GOOGLE_CLIENT_SECRET=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
|
|||
GOOGLE_REDIRECT_URI=http://localhost:8000/login/google/callback
|
||||
|
||||
|
||||
XENDIT_PRIVATE_KEY=
|
||||
XENDIT_PRIVATE_KEY=
|
||||
|
||||
|
||||
WAHITS_TOKEN=
|
||||
|
|
@ -9,6 +9,8 @@ use Illuminate\Support\Facades\Validator;
|
|||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
use App\Notifications\Auth\RequestOtp;
|
||||
|
||||
class LoginEmailController extends Controller
|
||||
{
|
||||
protected $memberAuthRepository;
|
||||
|
|
@ -50,8 +52,7 @@ class LoginEmailController extends Controller
|
|||
// Use MemberAuthRepository to generate OTP
|
||||
$otp = $this->memberAuthRepository->emailOtp(['email' => $email]);
|
||||
|
||||
// TODO: Integrate with WhatsApp API to send OTP
|
||||
// For now, we'll just log it (remove in production)
|
||||
$user->notify(new RequestOtp($otp->otp, "mail"));
|
||||
Log::info("OTP for {$email}: {$otp->otp}");
|
||||
|
||||
return response()->json([
|
||||
|
|
|
|||
|
|
@ -9,6 +9,8 @@ use Illuminate\Support\Facades\Auth;
|
|||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
use App\Notifications\Auth\RequestOtp;
|
||||
|
||||
class LoginWaController extends Controller
|
||||
{
|
||||
protected $memberAuthRepository;
|
||||
|
|
@ -55,8 +57,9 @@ class LoginWaController extends Controller
|
|||
// Use MemberAuthRepository to generate OTP
|
||||
$otp = $this->memberAuthRepository->waOtp(['phone' => $identity]);
|
||||
|
||||
// TODO: Integrate with WhatsApp API to send OTP
|
||||
// For now, we'll just log it (remove in production)
|
||||
$user->notify(new RequestOtp($otp->otp));
|
||||
|
||||
|
||||
Log::info("OTP for {$identity}: {$otp->otp}");
|
||||
|
||||
return response()->json([
|
||||
|
|
|
|||
|
|
@ -0,0 +1,75 @@
|
|||
<?php
|
||||
|
||||
namespace App\Notifications\Auth;
|
||||
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Notifications\Messages\MailMessage;
|
||||
use App\Notifications\WaNotification;
|
||||
|
||||
use App\Notifications\WahitsChannel;
|
||||
use App\Notifications\WahaChannel;
|
||||
use App\Notifications\WagaChannel;
|
||||
|
||||
class RequestOtp extends WaNotification
|
||||
{
|
||||
use Queueable;
|
||||
|
||||
var $otp;
|
||||
var $prefer;
|
||||
|
||||
/**
|
||||
* Create a new notification instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($otp, $prefer = "wa")
|
||||
{
|
||||
$this->otp = $otp;
|
||||
$this->prefer = $prefer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the notification's delivery channels.
|
||||
*
|
||||
* @param mixed $notifiable
|
||||
* @return array
|
||||
*/
|
||||
public function via($notifiable)
|
||||
{
|
||||
return $this->prefer == "wa" ? [WahitsChannel::class] : ["mail"];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the mail representation of the notification.
|
||||
*
|
||||
* @param mixed $notifiable
|
||||
* @return \Illuminate\Notifications\Messages\MailMessage
|
||||
*/
|
||||
public function toMail($notifiable)
|
||||
{
|
||||
return (new MailMessage)
|
||||
->subject('Your One-Time Password (OTP)')
|
||||
->greeting('Hi '.$notifiable->name)
|
||||
->line('Your One-Time Password (OTP) is:')
|
||||
->line("**{$this->otp}**")
|
||||
->line('This code is valid for 10 minutes.')
|
||||
->line('If you did not request this, please ignore this email.')
|
||||
->salutation('Regards, Asia Golf');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the array representation of the notification.
|
||||
*
|
||||
* @param mixed $notifiable
|
||||
* @return array
|
||||
*/
|
||||
public function toWa($notifiable)
|
||||
{
|
||||
$otp = $this->otp;
|
||||
return [
|
||||
"text" => "$otp ini adalah kode OTP untuk anda login.
|
||||
JANGAN berikan kode OTP ke siapapun!"
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
namespace App\Notifications;
|
||||
|
||||
use Illuminate\Notifications\Notification;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
|
||||
abstract class WaNotification extends Notification
|
||||
{
|
||||
|
||||
abstract public function toWa($notifiable);
|
||||
}
|
||||
|
|
@ -0,0 +1,55 @@
|
|||
<?php
|
||||
|
||||
namespace App\Notifications;
|
||||
|
||||
use Illuminate\Notifications\AnonymousNotifiable;
|
||||
|
||||
class WahitsChannel
|
||||
{
|
||||
/**
|
||||
* Send the given notification.
|
||||
*/
|
||||
public function send(object $notifiable, WaNotification $notification): void
|
||||
{
|
||||
|
||||
|
||||
$phone = $notifiable instanceof AnonymousNotifiable
|
||||
? $notifiable->routeNotificationFor('wa')
|
||||
: $notifiable->phone;
|
||||
|
||||
$phones = explode("/", $phone);
|
||||
|
||||
foreach($phones as $phone){
|
||||
|
||||
$phone = trim($phone);
|
||||
$phone = substr($phone,0,1) == "0" ? "62".substr($phone,1,strlen($phone)):$phone;
|
||||
$phone = preg_replace("/[^0-9]/", "",$phone);
|
||||
|
||||
$payload = $notification->toWa($notifiable);
|
||||
$message = $payload["text"];
|
||||
$image = @$payload["image"];
|
||||
$link = @$payload["link"];
|
||||
|
||||
$token = config("wahits.token");
|
||||
|
||||
$curl = curl_init();
|
||||
curl_setopt_array($curl, array(
|
||||
CURLOPT_URL => 'https://wa.wahits.com/api/send_message',
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_ENCODING => '',
|
||||
CURLOPT_MAXREDIRS => 10,
|
||||
CURLOPT_TIMEOUT => 0,
|
||||
CURLOPT_FOLLOWLOCATION => true,
|
||||
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
|
||||
CURLOPT_CUSTOMREQUEST => 'POST',
|
||||
CURLOPT_POSTFIELDS => "token=$token&number=$phone&message=$message&link=$link",
|
||||
CURLOPT_HTTPHEADER => array(
|
||||
'Content-Type: application/x-www-form-urlencoded'
|
||||
),
|
||||
));
|
||||
$response = curl_exec($curl);
|
||||
\Log::info($response);
|
||||
curl_close($curl);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -40,9 +40,9 @@ class MemberAuthRepository
|
|||
|
||||
function generateUniqueOtp()
|
||||
{
|
||||
if (config('app.env') == 'local'){
|
||||
return 123456;
|
||||
}
|
||||
// if (config('app.env') == 'local'){
|
||||
// return 123456;
|
||||
// }
|
||||
do {
|
||||
$otp = rand(100000, 999999);
|
||||
} while (UserOtp::where('otp', $otp)->where("expired_at",">", Carbon::now())->exists());
|
||||
|
|
|
|||
|
|
@ -0,0 +1,5 @@
|
|||
<?php
|
||||
|
||||
return [
|
||||
"token" => env("WAHITS_TOKEN")
|
||||
];
|
||||
Loading…
Reference in New Issue