Basic manual authentication in Laravel 11

75 views Asked by At

I'm trying to implement manual authentication in Laravel. However, after entering the correct username and password on my login page, it doesn't redirect to the dashboard page; instead, it returns to the login page.

Controller:

namespace App\Http\Controllers;

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

class UserController extends Controller
{
    public function login(Request $request) {
        $credentials = $request->validate([
            'username' => ['required'],
            'password' => ['required']
        ]);

        if(Auth::attempt($credentials)){
            $request->session()->regenerate();

            return redirect()->intended('dashboard');
        }
        return back()->withErrors([
            'email' => 'The provided credentials do not match our records.',
        ])->onlyInput('email');
    }
    public function register(Request $request){
        $credentials = $request->validate([
            'name' => ['required'],
            'username' => ['required'],
            'password' => ['required']
        ]);

        $user = new User();
        $user->name = $credentials['name'];
        $user->username = $credentials['username'];
        $user->password = Hash::make($credentials['password']);
    
        $user->save();
    
        return redirect('login');
        
    }
}

Route:

use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Route;

Route::get('/login', function () {
    return view('login');
})->name('login');

Route::get('/register', function () {
    return view('register'); 
})->name('register');

Route::post('/login', [UserController::class, 'login'])->name('login_process');
Route::post('/register', [UserController::class, 'register'])->name('register_process');
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;

class User extends Authenticatable
{
    use HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'username',
        'password'
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * Get the attributes that should be cast.
     *
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'email_verified_at' => 'datetime',
            'password' => 'hashed',
        ];
    }
}

I've tried creating a new model, but the problem still exists.

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class UserAuth extends Model
{
    use HasFactory;
    
    protected $fillable = [
        'name',
        'username',
        'password'
    ];
}
0

There are 0 answers