Laravel 10 custom login/registration does not enter the dashboard page
P粉162773626
P粉162773626 2023-09-05 14:04:13
0
2
404

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), 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.

P粉162773626
P粉162773626

reply all (2)
P粉006540600

hii, your logout functionality is protected by middleware, you also need to add dashboard routing middleware, you can group routes that require authentication middleware.

Route::middleware('auth')->group(function () { Route::get('/dashboard', [AuthController::class, 'dashboard'])->name('dashboard'); Route::post('/logout', [AuthController::class, 'logout'])->name('logout'); });
    P粉676588738

    Your route

    Route::get('login', [FrontendAuthController::class, 'loginGet'])->name('login'); Route::post('login', [FrontendAuthController::class, 'loginPost']); Route::post('logout', [FrontendAuthController::class, 'logout'])->name('logout'); Route::get('register', [FrontendAuthController::class, 'registerGet'])->name('register'); Route::post('register', [FrontendAuthController::class, 'registerPost']);

    Your controller:

    validate([ 'email' => 'required|email', 'password' => 'required', ], [ 'email.required' => 'The email field is required.', 'email.email' => 'Please enter a valid email address.', 'password.required' => 'The password field is required.', ]); $credentials = $request->only('email', 'password'); if (Auth::attempt($credentials)) { return redirect()->intended('/'); } else { return redirect()->back()->withErrors(['email' => 'These credentials do not match our records.']); } } public function logout() { Auth::logout(); Session::flush(); // Clear all session data Session::regenerate(); // Regenerate the session ID return redirect()->route('login'); } public function registerGet() { return view('front.auth.register'); } public function registerPost(Request $request) { $request->validate([ 'name' => 'required|string|max:255', 'email' => 'required|email|unique:users,email', 'password' => 'required|min:8|confirmed', ], [ 'name.required' => 'The name field is required.', 'email.required' => 'The email field is required.', 'email.email' => 'Please enter a valid email address.', 'email.unique' => 'This email address is already registered.', 'password.required' => 'The password field is required.', 'password.min' => 'The password must be at least 8 characters.', 'password.confirmed' => 'The password confirmation does not match.', ]); // Create a new user record $user = new User(); $user->name = $request->input('name'); $user->email = $request->input('email'); $user->password = Hash::make($request->input('password')); $user->save(); // Log in the newly registered user Auth::login($user); // Redirect the user to the home page or any other desired page return redirect()->intended('/'); } }

    Your login blade

          Sign In  
    

    Your registration page

          Register  
    

    I think this will solve all your questions

      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!