Home > Software design >  Laravel 9 rewrite auth to AJAX | Where is the register() function?
Laravel 9 rewrite auth to AJAX | Where is the register() function?

Time:06-13

I am using Laravel 9 and I use their auth system, e.g. register/login etc. I am currently trying to understand how exactly it works and I am stuck here:

If I observe all the auth routes, then I will see that the function which registers a user is called "register" and can be found in Auth\RegisterController:

POST register .......... Auth\RegisterController@register

Now in their RegisterController there is no function "register". That is their whole class:

use RegistersUsers;

/**
 * Where to redirect users after registration.
 *
 * @var string
 */
protected $redirectTo = RouteServiceProvider::HOME;

/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct() {
    $this->middleware('guest');
}

/**
 * Get a validator for an incoming registration request.
 *
 * @param  array  $data
 * @return \Illuminate\Contracts\Validation\Validator
 */
protected function validator(array $data) {
    return Validator::make($data, [
        'username' => ['required', 'string', 'max:255'],
        'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
        'password' => ['required', 'string', 'min:8', 'confirmed'],
    ]);
}

/**
 * Create a new user instance after a valid registration.
 *
 * @param  array  $data
 * @return \App\Models\User
 */
protected function create(array $data) {
    return User::create([
        'username' => $data['username'],
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
        'role' => 'user',
        'created_at' => Carbon::create(date("Y-m-d H:i:s")),
    ]);
}

I have already an AJAX function which works great. It creates a user. I could also redirect via javascript and thats it, but I would like to understand how the system works.

I also do not need the validators on the server as I do the validation on the frontend and the DB handles the rest.

Also I want to see in case of an error the JSON response so I can display a message to the user, instead I get 422 (Unprocessable Content)

CodePudding user response:

Yo can not find that method in that class because it is on

RegisterUsers Trait.

Notice at top : Use RegistersUser

Here you are the source code of that file in which that method live. RegistersUsers.php

  • Related