Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

andfelzapata's avatar

AuthController $redirectPath, $loginPath overwrite.

I'm trying to implement a login using ajax. When I use the wrong credentials I get redirected to /login, with correct credentials I get redirected to /home even though I added protected $redirectPath = '/dashboard' and protected $loginPath = '/'; to my AuthController.php file. Either way my browser shows me the "NotFoundHttpException in RouteCollection.php line 143:" message because /login and /home aren't specified in my routes.php file.

Here's my AuthController:

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use Validator;
use Auth;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

class AuthController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;


    protected $redirectPath = '/dashboard';

    protected $loginPath = '/';

    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest', ['except' => 'getLogout']);
    }

    /**
     * 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|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|confirmed|min:6',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }

    public function authenticate($credentials)
    {   
        $authenticate = ( Auth::attempt($credentials) ) ? true : false;

        return $authenticate;
    }

    /**
     * [getLogin description]
     * @return \Illuminate\Http\Response Html document
     */
    public function getLogin() {
        return view('auth.login');
    }

    /**
     * Handle an ajax login request to the application
     * 
     * @param \Illuminate\Http\Request $request
     * @param \Illuminate\Http\Response
     */ 
    public function postLogin(Request $request)
    {
        $this->validate($request, [
            'username' => 'required', 'password' => 'required',
        ]);// Returns response with validation errors if any, and 422 Status Code (Unprocessable Entity)

        $credentials = $request->only('username', 'password');

        if ($this->authenticate($credentials))
        {
            return response(array('msg' => 'Login Successfull'), 200) // 200 Status Code: Standard response for successful HTTP request
              ->header('Content-Type', 'application/json');
        }

        return response(array('msg' => $this->getFailedLoginMessage()), 401) // 400 Status Code: Forbidden, needs authentication
          ->header('Content-Type', 'application/json');

    }

    /**
     * Logs user out of app
     * @return \Illuminate\Http\Response 
     */
    public function getLogout() {
        Auth::logout();
    }

    /**
     * Returns registration form view
     * @return \Illuminate\Http\Response
     */
    public function getRegister() {
        return view('auth.register');
    }

    /**
     * Registers users
     * @return \Illuminate\Http\Response
     */
    public function postRegister() {
        return response('Registration Succesful!', 200) 
              ->header('Content-Type', 'application/json');
    }



 }
0 likes
3 replies
Snapey's avatar

I don't know if the order matters? try putting the overrides before the authenticatesAndRegistersUsers trait which is where they are checked.

andfelzapata's avatar

Hi @Snapey, I just solved it, silly thing it was.

  1. I forgot to change a route name in my jQuery code.

  2. After logging in correctly I was redirect to /home, it came from the RedirectIfAuthenticated class. I changed line 38 in that class, from "return redirect('/home')" to "return redirect('/dashboard')". Don't know if this is the proper way though.

Snapey's avatar

you should avoid if at all possible changing anything in the vendor folder as it will be lost after an upgrade. I assume that is what you changed?

Please or to participate in this conversation.