Packy
Packy

Reputation: 3573

Laravel Redirect Intended does nothing

I currently have a authenticate function I hit at login from a Vue component. Right now it logs the user in but no redirect happens from the controller. I am not sure if using a Vue component is causing that. Maybe I can return the intended URL in a response if so?

public function authenticate(Request $request)
    {

        //Validate the login and log errors if any
        $this->validate($request, [
            'email'     => 'required',
            'password'  => 'required',
        ]);

        //if they have stuff posted get it
        $email      = $request->get('email');
        $password   = $request->get('password');


        //See if they are actually a user
        if (Auth::attempt(['email' => $email, 'password' => $password])) {

          return redirect()->intended('/dashboard');

        } else {
          return response()->json([
            'response' => 'error',
            'error' => 'Email or Password not correct.',
          ]);
        }
    }

Login method in my login.vue component:

login(){

             this.isLoading = true;

             this.form.post('/login')
                .then(data => {

                  this.isLoading = false

                  if(data.response == 'success'){

                     //Maybe get a url in the response and redirect here??

                  } else {
                    this.serverError= data.error
                  }

                })
                .catch(error => {
                  this.isLoading = false
                })

           }

Using Laravel 5.4

Upvotes: 0

Views: 685

Answers (2)

Kenneth Sunday
Kenneth Sunday

Reputation: 895

Instead of using the method intended, why not use redirect()->route() instead?

Or you are expecting a URL response. You should not use the redirect() method.

For your given code, you might want to consider this.

if (Auth::attempt(['email' => $email, 'password' => $password])) {

            return response()->json([
              'response' => 'success',
              'url' => Session::get('url.intended', route('route_of_your_dashboard'))
            ]);

        } else {
          return response()->json([
            'response' => 'error',
            'error' => 'Email or Password not correct.',
          ]);
        }

Upvotes: 0

Packy
Packy

Reputation: 3573

For anyone looking:

in my authenticate function:

if (Auth::attempt(['email' => $email, 'password' => $password])) {

            return response()->json([
              'response' => 'success',
              'url' => Session::get('url.intended', url('/'))
            ]);

        } else {
          return response()->json([
            'response' => 'error',
            'error' => 'Email or Password not correct.',
          ]);
        }

in my vue component login method

if(data.response == 'success'){
                    //console.log(data);
                    window.location.href = data.url
                  } else {
                    this.serverError= data.error
                  }

Upvotes: 2

Related Questions