mohsen
mohsen

Reputation: 29

Auth system laravel just with phone number and verification sms code

my web app is laravel and my users do not have email and password and they register and login with phone number and verification sms code. My db is Mongodb. How to I change Auth system laravel?

Upvotes: 1

Views: 2067

Answers (1)

Mohammed Naguib
Mohammed Naguib

Reputation: 594

you will need to implement your own Auth system .First you will need to send SMS I recommend using Twilio https://www.twilio.com/blog/create-sms-portal-laravel-php-twilio

Here is some functions I made before

    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);
    }

the twilio service file

<?php

namespace App\Http\Services;

use Illuminate\Support\Facades\Log;
use Twilio\Rest\Client;


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;
        }

    }

Upvotes: 2

Related Questions