0

I'm using Laravel sanctum to use Rest API's for Laravel Token based authentication and when I send the request to server using axios to the '/register' route it returns a 422 error code .

const handleRegister = async (e) => {
    e.preventDefault()

    axios.defaults.withCredentials = true
    axios.get(`${hostName}/sanctum/csrf-cookie`).then((res) => {
      axios
        .post(`${hostName}/register`, {
          name: 'admin',
          email: '[email protected]',
          password: 'user',
        })
        .then((res) => {
          console.log('account created')
          //history.push('/dashboard')
        })
    })
  }

and the Register Controller is by using the laravel ui --auth package and it looks like this

class RegisterController extends Controller
{
    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, [
            'name' => ['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([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
        ]);
    }
}

also for more information in my CORS.php i allowed origins and updated the paths

   'paths' => ['api/*', 'sanctum/csrf-cookie', '/login', '/logout', '/register'],
   'supports_credentials' => true,

i managed to create a user with tinker and i can log in and logout but it returns a 422 when i try to register a new user

1
  • remove ` 'min:8', 'confirmed'` from your validation Commented Feb 25, 2021 at 0:04

1 Answer 1

1

422 error code usually means you have invalid fields, in this case, your validation is failing, if you don't have a password confirmation field in your form, then remove the 'confirmed' part of the validator.

Not the answer you're looking for? Browse other questions tagged or ask your own question.