I am trying to make my own custom laravel 10 login/registration because i didn’t want to use the breez package because i wanted to learn how do you make a login/registrasion by yourself.
But I cant seem to get past the authentication of the dashboard page.
I am using an if statment if(Auth::check())
on my dashboard function to authenticate the user in the database.
but for me this isn’t working because i keep getting the error message from the redirect back to the login page (This only happens when I register a new user into the database) but whenever I try loging in I get the success message from my login function (See code futher down) while still being 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 havn’t found any solution yet so if someone can help me it will be very appreciated.
2
Answers
hii your logout function is protected by middleware , you also need to add dashboard route a middleware you can group the routes that are required authentication middleware .
Your Routes
Your controller :
Your Login blade
Your Register page
I think This will solve your all queries