Ensuring a safe and protected registration process is very important for any software. If it does not handle from the backend, it can create unexpected scenarios on the frontend. On the Laravel Breeze registration process, everything has been dealt with perfectly. Let's look at how quickly you can make a custom Registration with Laravel Breeze.
Step 1: Make registration Route
The registration route should be with “guest” middleware.
This middleware will protect the route from logged-in users. Like the Breeze Login route, this will be a Post route.
Route::post('/registration',[TestController::class,'getRegistration'])->middleware('guest');
Step 2: Add header file on the registration controller
We should add some header files on the registration controller
for obvious reasons. Most of the time code editor does not suggest adding those
headers. This is the reason I suggest people do this manually.
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rules;
Step 3: make a registration method on the registration controller
And this is the last step to make the registration method.
You just need to make a registration method. In this step, we will follow the
Laravel Breeze Registration process.
public
function getRegistration(Request $request){
$name = $request->input('name');
$email = $request->input('email');
$password = $request->input('password');
if (User::where('email', $email)->count()==0){
$validation = Validator::make($request->all(),[
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:'.User::class],
'password' => ['required', Rules\Password::defaults()],
]);
if (!$validation->fails()){
$user = User::insert([
'name' => $name,
'email' => $email,
'password' => Hash::make($password),
]);
if ($user){
$newUser = User::where('email', $email)->first();
event(new Registered($newUser));
return response()->json([
'status'=>true,
'message'=>"user Created"
]);
}}
else{return
response()->json([
'status'=>false,
'message'=>"Use a hard password"
]);
}
}
else{
return response()->json([
'status'=>false,
'message'=>"Email Already exist"
]);
}
}
Now, this is ready to use on the frontend. We support people
if they comment below and ask us about more things. Have a good time.