XDProgrammer
XDProgrammer

Reputation: 861

User Registration Laravel 5.1

Im trying to implement the built user Authentication/Registration in laravel 5.1 but it keeps redirecting to /home.

Heres the route:

Route::post('sign_up' , 'Auth\AuthController@postRegister');

Form

 <form method="POST" action="http://localhost:8000/sign_up" accept-charset="UTF-8" class="form-horizontal">

The postRegister function is commented out so its should not go anywhere:

public function postRegister(Request $request)
    {
//        $validator = $this->validator($request->all());
//
//        if ($validator->fails()) {
//            $this->throwValidationException(
//                $request, $validator
//            );
//        }
//
//        Auth::login($this->create($request->all()));
//
//        return redirect($this->redirectPath());
    }
}

Upvotes: 1

Views: 219

Answers (1)

Mohamed Mo Kawsara
Mohamed Mo Kawsara

Reputation: 4688

Please note that the real logic is happening in AuthenticatesUsers trait which is placed in Illuminate\Foundation\Auth namespace.

And if you open that you will notice the responsible method loginPath() of checking the existence of a loginPath attribute which is responsible of redirection back to login route/view.

To change the redirection process add this property to your AuthController.php :

protected $loginPath = "/"; // redirect to where you want

Or perhaps you want to redirect within your postRegister() upon role or some other condition, you may do it like code below, which is btw the dirty road or let's say not taking advantage of Laravel 5.1 new authorization feature:

/**
 * Handle a registration request for the application.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function postRegister(Request $request)
{
    $validator = $this->validator($request->all());

    if ($validator->fails()) {
        $this->throwValidationException(
            $request, $validator
        );
    }

    // Change your loginPath here if you want: $this->loginPath = "/";

    Auth::login($this->create($request->all()));

    return redirect($this->redirectPath());
}

And it is worth mentioning that the other parallel property is $redirectTo which it's used to redirect to after successful login attempt, you may add it to your authController.php, i.e: again you should use policies to redirect admins only to dashboard and maybe normal users to profile or home!

protected $redirectTo = "dashboard";

Minor tweak: in your case

you might want to change links to dynamic so please form header make it like this:

<form method="POST" action="{{ action('Auth\AuthController@postRegister') }}" accept-charset="UTF-8" class="form-horizontal">

Upvotes: 1

Related Questions