Problem in Re-Registering into my application


#1

Hey I Have an application.In which when user register before entering into application.The problem is i am using otp verification.But while making otp request sometimes otp doesn’t comes and user fails to register into application and if user want to re-register he is not able to re-register as in database his details are saved and according to this code he is not able to re-request for otp.Here i am embedding my php code.Kindly help me in if otp verified then only data saved into my database.

    <?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Auth;
use App\User;
use App\Otp;
use App\Referal;
use App\Constant;

class registerController extends Controller
{
    public function apiRegister(Request $request)
    {
    	$this->Validate($request,[
            'name' => 'required|string',
            'mobile' => 'required|unique:users,mobile|min:10|max:10',
            'email' => 'required|email|unique:users,email',
            'password' => 'required|min:6',
            'refer_code' => 'nullable|exists:users,refer_code',
            'device_id' => 'required'
        ]);
    
    

    $device = User::where('device_id',$request->device_id)->first();

    if (!empty($device) && $device->otp_verified == 'n') {

        $apiToken = str_random(150);
        $otp = mt_rand(1000,9999);
        $msg = 'Your OTP is: '.$otp;
        $ot = sendSms($request->mobile,$msg);
        if($ot == true)
        {
            User::create([
                'name' => $request->name,
                'mobile' => $request->mobile,
                'email' => $request->email,
                'password' => bcrypt($request->password),
                'api_token' => $apiToken,
                'user_type' => 'u',
                'points' => 0,
                'refer_code' => refferCode(),
                'otp' => $otp,
                'ip_address' => \Request::getClientIp(true),
                'device_id' => $request->device_id
            ]);

            $user_id = User::where('mobile',$request->mobile)->select('id','refer_code')->first();

            if(isset($request->refer_code)){    // if referal code is entered by user

                $refer_user = User::where('refer_code',$request->refer_code)->first();

                if(strtoupper($refer_user->refer_code) == strtoupper($request->refer_code))
                {
                    $const = Constant::where('constant_name','referal_points')->first();

                    Referal::create([
                        'refer_from_id' => $refer_user->id,
                        'refer_to_id' => $user_id->id,
                        'referal_points' => $const->constant_value,
                        'time' => time()
                    ]);
                }   
                else
                {
                    $response['msg'] = 'Referal Code is invalid!! try with another Referal code!!';
                    return response()->json($response,400);
                }
            }

            Otp::create([
                'user_id' => $user_id->id,
                'time' => time(),
            ]);

            $response['msg'] = 'User Registered Successfully!';
            $response['refer_code'] = 'Your Referal Code :'. $user_id->refer_code;
            return response()->json($response,200);
        }
        else
        {
            $response['msg'] = 'User Registreation Unsuccessful!  Try again!!!';
            return response()->json($response,400);
        }
    }elseif(empty($device))
    {
        $apiToken = str_random(150);
        $otp = mt_rand(1000,9999);
        $msg = 'Your OTP is: '.$otp;
        $ot = sendSms($request->mobile,$msg);
        if($ot == true)
        {
            User::create([
                'name' => $request->name,
                'mobile' => $request->mobile,
                'email' => $request->email,
                'password' => bcrypt($request->password),
                'api_token' => $apiToken,
                'user_type' => 'u',
                'points' => 0,
                'refer_code' => refferCode(),
                'otp' => $otp,
                'ip_address' => \Request::getClientIp(true),
                'device_id' => $request->device_id
            ]);

            $user_id = User::where('mobile',$request->mobile)->select('id','refer_code')->first();

            if(isset($request->refer_code)){    // if referal code is entered by user

                $refer_user = User::where('refer_code',$request->refer_code)->first();
                if($refer_user->refer_code == $request->refer_code)
                {
                    $const = Constant::where('constant_name','referal_points')->first();
                    Referal::create([
                        'refer_from_id' => $refer_user->id,
                        'refer_to_id' => $user_id->id,
                        'referal_points' => $const->constant_value,
                        'time' => time(),
                    ]);
                }   
                else
                {
                    $response['msg'] = 'Referal Code is invalid!! try with another Referal code!!';
                    return response()->json($response,400);
                }
            }

            Otp::create([
                'user_id' => $user_id->id,
                'time' => time(),
            ]);

            $response['msg'] = 'User Registered Successfully!';
            $response['refer_code'] = 'Your Referal Code :'. $user_id->refer_code;
            return response()->json($response,200);
        }
        else
        {
            $response['msg'] = 'User Registreation Unsuccessful!  Try again!!!';
            return response()->json($response,400);
        }

    }
    else
    {
        $response['msg'] = 'This device has been used previously. Please try with another device!';
        return response()->json($response,400);
    }
    
}

}


#2

I’m not familiar with Opt. Is it a service, or what?