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

brunoromeo's avatar

Auth Routes not Working After Login True

Guys, i'm autenticate my application but same when Auth return true i can't access home route.

How do i retrieve user information and set session true to access protected routes?

//\app\Http\Controllers\HomeController
 public function __construct()
  {
    $this->middleware('auth');
  }
//\app\Http\Models\User
public function login(Request $request)

  {
    if (Auth::attempt([ "username"=>$request->username,  "password"=> $request->password])) {

      $request->session()->regenerate();
      return redirect()->route('home');
    }

    return back()->withErrors([
      'username' => 'The provided credentials do not match our records.',
    ]);

  }
//\routes\web.php
Route::get('/', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Auth::routes();
0 likes
24 replies
tykus's avatar

The code snippet labeled //\app\Http\Models\User is what/where exactly???

brunoromeo's avatar

@tykus Name of file. The Files are HomeController.php, User.php and web.php

tykus's avatar

@brunoromeo the login method probably belongs in a Controller rather than in a Model.

Anyway, is that your entire routes/web.php file?

brunoromeo's avatar

@tykus i moved to Controller but not working, below is my web.php entire.

<?php

use Illuminate\Support\Facades\Route;

Route::get('/', [App\Http\Controllers\HomeController::class, 'index'])->name('home');

Route::post('/auth', [App\Http\Controllers\UserController::class, 'login'])->name('login.auth');

Auth::routes();

My HomeController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;

class HomeController extends Controller
{
  
  public function __construct()
  {
    $this->middleware('auth');
  }

  public function index()
  {    
    return view('home', [
      "data"=>"Test"
    ]);
  }
}

However, even though my login method is true I can't access the home route.

OBS.: I'm use custom Auth.

brunoromeo's avatar

@tykus Custom Auth, login by username and password on table "test_usuarios".

tykus's avatar

@brunoromeo is the Session started; have you made any changes to the middleware groups in app/Http/Kernel.php; or the Route groups in app/Providers/RouteServiceProvider.php?

xeno's avatar

Can you show me your User model class ??

brunoromeo's avatar

@xeno

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;

class User extends Authenticatable
{
  use HasApiTokens, HasFactory, Notifiable;  

  protected $table = 'test_usuarios';

  protected $fillable = [
    'email',
    'username',
    'password',
  ];

  protected $hidden = [
    'password',
    'remember_token',
  ];

  protected $casts = [
    'email_verified_at' => 'datetime',
  ];

  public function login(Request $request)
  {    
    if (Auth::attempt([
      "username"=>$request->username,
      "password"=> $request->password
    ])) {

      $request->session()->regenerate();
      
      return redirect()->route('home');
    }

    return back()->withErrors([
      'username' => 'The provided credentials do not match our records.',
    ]);    

  }

}


brunoromeo's avatar

@xeno i already moved to UserController and not working. The Auth::attempt() return true but i cant access the home page. I'm starting learnen Laravel and i don't know much things about Laravel.

My web.php

<?php

use Illuminate\Support\Facades\Route;

Route::get('/', [App\Http\Controllers\HomeController::class, 'index'])->name('home');

Route::post('/auth', [App\Http\Controllers\UserController::class, 'login'])->name('login.auth');

Auth::routes();

My HomeController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
{

  public function __construct()
  {
    $this->middleware('auth');
  }

  public function index()
  {    
    return view('home', [
      "data"=>'Laravel'
    ]);
  }
}

My UserController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;

class UserController extends Controller
{  
  public function username()
  {
    return 'username';
  }

  public function login(Request $request)
  {
    if (Auth::attempt([
      "username"=>$request->username,
      "password"=> $request->password
    ])) {

      $request->session()->regenerate();

      return redirect()->route('home');
    }

    return back()->withErrors([
      'username' => 'The provided credentials do not match our records.',
    ]);
  }


}

tykus's avatar

@brunoromeo what is your configured Session driver; it will be listed in the Drivers section of:

php artisan about
brunoromeo's avatar

@tykus the command php artisan about show erro: (Command "about" is not defined).

i didn't modified the file /config/session.php

xeno's avatar

@brunoromeo Is there a error when you try to access your homepage ? after you login ?

brunoromeo's avatar

@xeno no, the erro show when i use on terminal.

But when i try access home page, redirect to login page even the credentials is valid.

xeno's avatar
xeno
Best Answer
Level 1

@brunoromeo also add dd(auth()->user()); below the $request->session()->regenerate(); to check if there's authenticated is really successful.

brunoromeo's avatar

@xeno

App\Models\User {#380 ▼
  #table: "test_usuarios"
  #fillable: array:5 [▶]
  #hidden: array:3 [▶]
  #casts: array:1 [▶]
  #connection: "mysql"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  +preventsLazyLoading: false
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #escapeWhenCastingToString: false
  #attributes: array:12 [▶]
  #original: array:12 [▶]
  #changes: []
  #classCastCache: []
  #attributeCastCache: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #visible: []
  #guarded: array:1 [▼
    0 => "*"
  ]
  #rememberTokenName: "remember_token"
  #accessToken: null
}
xeno's avatar

@brunoromeo it seems that your authentication is successful. You didn't change anything to the Middleware/Authenticate.php right ?

Please or to participate in this conversation.