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
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
That phone is not a valid rule for laravel, for validate the phone use regex expression.