skip to Main Content

In my Laravel 10 application, I’m trying to send an OTP to a mobile phone using Firebase. I have integrated Firebase into my project and followed the relevant documentation for sending OTPs. However, when I try to send the OTP, I encounter the error message ‘Invalid target type ‘number’, valid types: condition, token, topic, unknown’. I have checked my code and made sure that I am using the correct target type. Could you please help me understand what might be causing this error and how I can successfully send the OTP using Firebase?

Error message:

"message": "Invalid target type 'phone_number', valid types: condition, token, topic, unknown",
    "exception": "Kreait\Firebase\Exception\InvalidArgumentException",
    "file": "C:\xampp\htdocs\daralaman-api\server\vendor\kreait\firebase-php\src\Firebase\Messaging\MessageTarget.php",
    "line": 58,
    "trace": [

otpRegistercontroller

`class OtpRegisterController extends Controller
{

    public function register(Request $request)
    {

        // Validate the request data
        $validator = Validator::make($request->all(), [
            'name' => 'required',
            'phone_number' => 'required|numeric',
        ]);

        if ($validator->fails()) {
            return response()->json(['errors' => $validator->errors()], 400);
        }

        // Generate a random OTP
        $otp = mt_rand(100000, 999999);

        // try {
        // Create a new user in the users table
        $user = User::create([
            'name' => $request->input('name'),
            'phone_no' => $request->input('phone_number'),
        ]);

        // Calculate the expiration time
        $expiresAt = Carbon::now()->addMinutes(2); // Expiration time set to 2 minutes from now 

        // Create a new OTP entry in the user_otps table
        UserOtp::create([
            'user_id' => $user->id,
            'otp' => $otp,
            'expires_at' => $expiresAt,
        ]);

        // Send the OTP to the user's mobile number using Firebase
        $factory = (new Factory)->withServiceAccount(storage_path('firebase/serviceAccountKey.json'));
        $messaging = $factory->createMessaging();

        $message = CloudMessage::withTarget('phone_number', $request->input('phone_number'))
            ->withNotification(Notification::create('OTP', "Your OTP is: $otp"))
            ->withData(['otp' => $otp]);

`
        $messaging->send($message);

        // Return a response indicating success
        return response()->json(['message' => 'OTP sent successfully']);
        // } catch (Exception $e) {
        //     // Return an error response
        //     return response()->json(['error' => 'Failed to register user'], 500);
        // }
    }

}

2

Answers


  1. Firebase Cloud Messaging delivers messages to devices through its own cloud protocol. You cannot use the Firebase Cloud Messaging API to send SMS messages or to otherwise deliver messages to a phone number, as you are trying to do.

    If you are trying to sign your users in with Firebase Authentication through SMS, follow the documentation for signing in with a phone number for that product. Note that you will need to write code that runs on the client device for this. There is no option in Firebase to trigger this type of OTP from the server.

    If you want to send your own SMS messages, Firebase does not have an API for that and you will have to use another provider.

    Login or Signup to reply.
  2. Firebase Cloud Messaging delivers messages to devices through its own cloud protocol. You cannot use the Firebase Cloud Messaging API to send SMS messages or to otherwise deliver messages to a phone number, as you are trying to do.

    If you are trying to sign your users in with Firebase Authentication through SMS, follow the documentation for signing in with a phone number for that product. Note that you will need to write code that runs on the client device for this. There is no option in Firebase to trigger this type of OTP from the server.

    If you want to send your own SMS messages, Firebase does not have an API for that and you will have to use another provider.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search