我的web应用程序是laravel和我的用户没有电子邮件和密码,他们注册和登录与电话号码和验证短信代码。我的数据库是Mongodb。我如何更改Auth系统机票?
你需要实现你自己的Auth系统。首先你需要发送短信,我建议使用Twilio https://www.twilio.com/blog/create-sms-portal-laravel-php-twilio
下面是我在
之前编写的一些函数public function sendVerificationCode(VerificationCodeRequest $request)
{
$twilioService = new TwilioService() ;
$otp = random_int(1000, 9999);
$result = $twilioService->sendVerificationCode(request('mobile'), $otp );
if (!$result) {
return response()->json(["message"=>__('messages.wrong_number')],422);
}
}
$user = User::updateOrCreate(
['mobile' => request('mobile')],
['verification_code' => $otp]
);
return response()->json(["message"=>__('messages.otp_sent')],200);
}
public function login(MobileLoginRequest $request)
{
$user = User::where("mobile",request('mobile'))->firstOrFail();
if($user->verification_code==$otp){
if ( !$userToken=JWTAuth::fromUser($user)) {
return response()->json(['message' => __('messages.Unauth')], 401);
}
}else{
return response()->json(['message' => __('messages.invalid_otp')], 401);
}
$user->update(["verified"=>1,"login_type"=>"mobile"]);
return $this->respondWithToken($userToken,$user);
}
protected function respondWithToken($userToken,$user)
{
return response()->json([
'token' => $userToken,
'token_type' => 'bearer',
'expires_in' => JWTAuth::factory()->getTTL() * 60,
'profile' => $user,
], 200);
}
twilio服务文件
<?php
namespace AppHttpServices;
use IlluminateSupportFacadesLog;
use TwilioRestClient;
class TwilioService
{
public function sendVerificationCode($number,$otp){
return $this->sendMessage("your Verification Code is : $otp ",$number);
}
public function sendNotification($recipient,$body,$title){
return $this->sendMessage($body,$recipient,$title."n");
}
private function sendMessage($message, $recipient,$title="")
{
try {
$account_sid = getenv("TWILIO_SID");
$auth_token = getenv("TWILIO_AUTH_TOKEN");
$twilio_number = getenv("TWILIO_NUMBER");
$client = new Client($account_sid, $auth_token);
$client->messages->create("$recipient",
['from' => $twilio_number, 'body' => $title.$message] );
return true;
} catch (Throwable $th) {
Log::error("$th");
Log::info("-------unable to send SMS to phone $recipient -------------");
return false;
}
}