skip to Main Content

I am new to Laravel and using Laravel 10. I used Laravel UI for auth setup and it’s working fine but I want to use mobile auth instead of email auth. How can I do it?

<?php

namespace AppHttpControllersAuth;

use AppHttpControllersController;
use AppModelsUser;
use AppNotificationsResetPasswordNotification;
use IlluminateFoundationAuthSendsPasswordResetEmails;
use IlluminateHttpRequest;
use IlluminateSupportFacadesPassword;

class ForgotPasswordController extends Controller
{
    use SendsPasswordResetEmails;
    protected function credentials(Request $request)
    {
        return $request->only('mobile');
    }

    public function sendResetLinkEmail(Request $request)
    {
        $this->validate($request, ['mobile' => 'required|numeric']);
        $user = User::query()->where('mobile', $request->mobile)->first();
        var_dump($user);
        if (!$user) {
           alert('there is not any user');
        }
        $token = Password::broker()->createToken($user);//this function
        $user->notify(new ResetPasswordNotification($token));
        return back();
    }
}

I get this error

IlluminateAuthPasswordsPasswordBroker::createToken(): Argument #1 ($user) must be of type IlluminateContractsAuthCanResetPassword, null given, called in C:xampphtdocsprojectsyazdvakeraappHttpControllersAuthForgotPasswordController.php on line 48

2

Answers


  1. For changing the authentication method from email to mobile number in Laravel 10, for password reset functionality, you will need to make modification in the code as below,
    1.User Model- Add use Notifiable; and change the primary key to mobile:

           class User extends Authenticatable
           {
           use Notifiable;
           protected $primaryKey = 'mobile'; 
           // Rest of the model code...
           }
    

    2.Update the Migration: Update the migration to reflect the changes made to the users table.

          Schema::create('users', function (Blueprint $table) {
          $table->string('mobile')->primary();
          // Other columns...
          $table->timestamps();
          });
    

    3.Update the Validation: update the validation rule in your ‘ForgotPasswordController’ to validate the mobile number instead of the email

         $this->validate($request, ['mobile' => 'required|numeric']);
    

    4.Update the sendResetLinkEmail Method: update the method to use the mobile number for finding the user and sending the reset password notification

          public function sendResetLinkEmail(Request $request)
          {
          $this->validate($request, ['mobile' => 'required|numeric']);
          $user = User::query()->where('mobile', $request->mobile)->first();
          if (!$user) {
          // Handle case where user is not found
          }
          $token = Password::broker()->createToken($user);
          $user->notify(new ResetPasswordNotification($token));
          return back();
          }
    

    5.Update the notification class: update the ResetPasswordNotification class to use the mobile number instead of the email in the notification message.

    6.Update the routes:

         Route::post('password/email', 
         [ForgotPasswordController::class, 
        'sendResetLinkEmail'])->name('password.email');
    

    7.Update the views releated to password reset to use mobile number instead of email.
    –With these above changes the password reset using the mobile number instead of email should work

    Login or Signup to reply.
  2. In Laravel 10 sendResetLinkEmail() function need to defined credentials(), if you’re getting mobile number in request than you need to get email from user object pass manually as credentials() in function, please check the function code :

    public function sendResetLinkEmail(Request $request)
    {
        // $this->validateEmail($request);
        $this->validate($request, ['mobile' => 'required|numeric']);
        $user = User::whereMobile($request->mobile)->first();
    
        if ($user) {
            // We will send the password reset link to this user. Once we have attempted
            // to send the link, we will examine the response then see the message we
            // need to show to the user. Finally, we'll send out a proper response.
            
            // $response = $this->broker()->sendResetLink(
            //     $this->credentials($request)   // remove this because it's fatch email from request but you don't pass email in request
            // );
            // need to add maually user's email to  send reset link
            $response = $this->broker()->sendResetLink($user->email);
    
            return $response == Password::RESET_LINK_SENT
                        ? $this->sendResetLinkResponse($request, $response)
                        : $this->sendResetLinkFailedResponse($request, $response);
        }
    
        return back()
                ->withInput($request->only('mobile'))
                ->withErrors(['mobile' => "User not found with this mobile number"]);
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search