Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
645 views
in Technique[技术] by (71.8m points)

How to login after submitting register form using ajax and laravel fortify

I'm trying to create a simple register form using ajax validation and laravel fortify. My problem is whenever I submit the form, it inserts all values in database as I wanted, but it won't redirect to the user profile as it was supposed to and gives an error "register:1 Failed to load resource: the server responded with a status of 500 (Internal Server Error)".

With Fortify I only needed to return the $user on create method in CreateNewUser controller to automatically login the user after registering, but if I return it with a response, it won't work. I've been struggling with this for many hours and I don't have any ideia how to solve this.

Here's my form:

<form class="multisteps-form__form" id="wizard" method="POST" action="{{route('register')}}">
    @csrf
    <div class="step-content-field">    
    <div class="form-inner-area">
        <label>First Name:</label>
        <input type="text" name="firstname" id="firstname" class="form-control "  placeholder="First Name" required>
        <div class="text-danger error" data-error="firstname"></div>
    </div>
    <div class="form-inner-area">
        <label>Last Name:</label>
        <input type="text" name="lastname" id="lastname" class="form-control "  placeholder="Last Name" required>
        <div class="text-danger error" data-error="lastname"></div>
    </div>
    <div class="form-inner-area">
        <label>Email:</label>
        <input type="text" name="email" class="form-control "  id="email" placeholder="Email" required>
        <div class="text-danger error" data-error="email"></div>
    </div>
    <div class="form-inner-area">
        <label>Mobile:</label>
        <input type="text" name="mobile" class="form-control " id="mobile" placeholder="Mobile" required>
        <div class="text-danger error" data-error="mobile"></div>
    </div>
    <div class="form-inner-area">
        <label>Country:</label>
        <input type="text" name="country" class="form-control "  id="country" placeholder="Country" required>
        <div class="text-danger error" data-error="country"></div>
    </div>
    <div class="form-inner-area">
        <label>Password:</label>
        <input type="password" name="password" class="form-control "  id="password" placeholder="Password" required>
    </div>
    <div class="form-inner-area">
        <label>Confirm password:</label>
        <input type="password" name="password_confirmation" class="form-control"  id="password_confirm" placeholder="Confirm password" required>
        
    </div>
    <span>The password must be at least 8 characters and contain at least one uppercase character, one number, and one special character.</span>
    </div>
   <div class="actions">
      <ul>
       <li><span class="js-btn-prev" title="BACK"><i class="fa fa-arrow- 
       left"></i> BACK </span></li>
      <li><button type="submit" title="SUBMIT">SUBMIT <i class="fa fa- 
      arrow-right"></i></button></li>
       </ul>
    </div>
</form>

The ajax script:

<script>
    $('#wizard').on('submit', function(e){
    e.preventDefault();
    $('.error').html('');
    $.ajax({
    url: $(this).attr('action'),
    method: $(this).attr('method'),
    data: $(this).serialize(),
    dataType: 'json',
    success: function(response) {
    window.location.href = '/user' 
    },
    error(error)
    {
    let errors = error.responseJSON.errors
    for(let key in errors)
    {
        let errorDiv = $(`.error[data-error="${key}"]`);
        if(errorDiv.length )
        {
            errorDiv.text(errors[key][0]);
        }
        }
    }
});
});
    </script>

And the CreateNewUser controller from fortify:

class CreateNewUser implements CreatesNewUsers
{
    use PasswordValidationRules;

    public function create(array $input)
    {
        Validator::make($input, [
            'firstname' => ['required', 'string', 'max:255'],
            'lastname' => ['required', 'string', 'max:255'],
            'email' => [
                'required',
                'string',
                'email',
                'max:255',
                Rule::unique(User::class),
            ],
            'mobile' => ['required', 'int'],
            'country' => ['required', 'string', 'max:255'],
            'password' => $this->passwordRules(),
        ])->validate();

        $user = User::create([
            'firstname' => $input['firstname'],
            'lastname' => $input['lastname'],
            'email' => $input['email'],
            'mobile' => $input['mobile'],
            'country' => $input['email'],
            'password' => Hash::make($input['password']),
            
        ]);
            $user->notify(new WelcomeEmailNotification());

            return response()->json($user);
    }
}

question from:https://stackoverflow.com/questions/65832210/how-to-login-after-submitting-register-form-using-ajax-and-laravel-fortify

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...