I'm trying to make my own custom Laravel 10 login/registration as I don't want to use the breez package as I want to learn how to do the login/registration myself.
But I can't seem to authenticate to the dashboard page.
I use the if statement if(Auth::check())
on the dashboard function to authenticate the user in the database.
But this doesn't work for me because I keep getting the error message from redirecting back to the login page (This only happens when I register a new user into the database strong>), but whenever I try to log in I receive a success message from the login function (See code further) while still in the login page.
AuthController (dashboard):
public function dashboard(): View { if(Auth::check()) { return view('auth.dashboard'); } return view('auth.login')->with('error', 'You are not allowed to access'); }
AuthController (login):
public function loginPost(Request $request): RedirectResponse { $request->validate([ 'email' => 'required', 'password' => 'required' ]); $credentials = $request->only('email', 'password'); if(Auth::attempt($credentials)) { $request->session()->regenerate(); return redirect()->intended(route('dashboard'))->with('success', 'You have successfully logged in'); } return redirect(route('login'))->with('error', 'Oppes! You have entered invalid credentials'); }
web.php
Route::get('/register', [AuthController::class, 'register'])->name('register'); Route::post('/register', [AuthController::class, 'registerPost'])->name('register.post'); Route::get('/login', [AuthController::class, 'login'])->name('login'); Route::post('/login', [AuthController::class, 'loginPost'])->name('login.post'); Route::get('/dashboard', [AuthController::class, 'dashboard'])->name('dashboard'); Route::post('/logout', [AuthController::class, 'logout'])->middleware('auth')->name('logout');
I haven't found any solution yet, so if anyone can help me I'd be grateful.
hii, your logout functionality is protected by middleware, you also need to add dashboard routing middleware, you can group routes that require authentication middleware.
Your route
Your controller:
Your login blade
Your registration page
I think this will solve all your questions