I am trying to use Laravel 5 built in User Authentication.In this regard I would like to redirect user to a certain route/page/controller after successfully logged in. I am trying to change code of complied.php file. I am trying to change /home
of below code, but it is not working.
trait AuthenticatesAndRegistersUsers
{
protected $auth;
protected $registrar;
public function getRegister()
{
return view('auth.register');
}
public function postRegister(Request $request)
{
$validator = $this->registrar->validator($request->all());
if ($validator->fails()) {
$this->throwValidationException($request, $validator);
}
$this->auth->login($this->registrar->create($request->all()));
return redirect($this->redirectPath());
}
public function getLogin()
{
return view('auth.login');
}
public function postLogin(Request $request)
{
$this->validate($request, array('email' => 'required|email', 'password' => 'required'));
$credentials = $request->only('email', 'password');
if ($this->auth->attempt($credentials, $request->has('remember'))) {
return redirect()->intended($this->redirectPath());
}
return redirect($this->loginPath())->withInput($request->only('email', 'remember'))->withErrors(array('email' => $this->getFailedLoginMessage()));
}
protected function getFailedLoginMessage()
{
return 'These credentials do not match our records.';
}
public function getLogout()
{
$this->auth->logout();
return redirect('/home');
}
public function redirectPath()
{
if (property_exists($this, 'redirectPath'))
{
return $this->redirectPath;
}
return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
}
public function loginPath()
{
return property_exists($this, 'loginPath') ? $this->loginPath : '/auth/login';
}
}
Thanks
You are not supposed to change anything in compiled.php
In RedirectIfAuthenticated
middleware change,
return new RedirectResponse(url('/home'));
to
return new RedirectResponse(url('/'));
This basically redirects logged in user to desired path, once logged in user returns to website. so,handle
function lookes like below,
public function handle($request, Closure $next) {
if ($this->auth->check())
{
return new RedirectResponse(url('/'));
}
return $next($request);
}
after that add following in AuthController
public $redirectTo = '/';
public $redirectAfterLogout = '/';
so after successful login user wil be redirected to redirectTo
and after logout user will be redirected to redirectAfterLogout
.