skip to Main Content

I’m trying to make an Auth API with laravel 10. In the API I want a user to sign up with the email or phone number but when login in, I want to only use the phone number.

So, I used this method in the registration and login form but when I test it in postman, I get this error:

Error "BadMethodCallException: Method IlluminateValidationValidator::validatePhone does not exist."

API Controller

// Register API (POST, formdata)
public function register(Request $request){

    // data validation
    $request->validate([
        "name" => "required",
        "email" => "email|unique:users",
        "phone" => "required|phone:ML",
        "password" => "required|confirmed"
    ]);

    // Author model
    User::create([
        "name" => $request->name,
        "email" => $request->email,
        "phone" => $request->phone,
        "password" => Hash::make($request->password)
    ]);

    // Response
    return response()->json([
        "status" => true,
        "message" => "User created successfully"
    ]);
}

// Login API (POST, formdata)
public function login(Request $request){

    // Data validation
    $request->validate([
        //"email" => "required|email",
        "phone" => "required|phone:ML",
        "password" => "required"
    ]);

    // Auth Facade
    if(Auth::attempt([
        //"email" => $request->email,
        "phone" => $request->phone,
        "password" => $request->password
    ])){

        $user = Auth::user();

        $token = $user->createToken("myToken")->accessToken;

        return response()->json([
            "status" => true,
            "message" => "Login successful",
            "access_token" => $token
        ]);
    }

    return response()->json([
        "status" => false,
        "message" => "Invalid credentials"
    ]);
}

2

Answers


  1. Chosen as BEST ANSWER

    I was following a forum post for phone validation they use this

    $request->validate([ // Allow only phone numbers from the FR 'phone_number' => 'required|phone:FR', ]);

    so i tryed to integrate it with Mali code witch's ML


  2.   $request->validate([
            //"email" => "required|email",
            "phone" => "required|phone:ML",
            "password" => "required"
        ]);
    

    That phone is not a valid rule for laravel, for validate the phone use regex expression.

    'phone' => ['required', 'numeric', 'regex:/^[0-9]{10}$/',]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search