Laravel 10 custom login/registration does not enter the dashboard page
P粉162773626
2023-09-05 14:04:13
<p>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. </p>
<p>But I can't seem to authenticate to the dashboard page. </p>
<p>I use the if statement <code>if(Auth::check())</code> on the dashboard function to authenticate the user in the database. </p>
<p>But this doesn't work for me because I keep getting the error message from redirecting back to the login page (<strong>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 (<strong>See code further</strong>) while still in the login page. </p>
<p><strong>AuthController (dashboard): </strong></p>
<pre class="brush:php;toolbar:false;">public function dashboard(): View
{
if(Auth::check()) {
return view('auth.dashboard');
}
return view('auth.login')->with('error', 'You are not allowed to access');
}</pre>
<p><strong>AuthController (login): </strong></p>
<pre class="brush:php;toolbar:false;">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');
}</pre>
<p><strong>web.php</strong></p>
<pre class="brush:php;toolbar:false;">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');</pre>
<p>I haven't found any solution yet, so if anyone can help me I'd be grateful. </p>
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