Laravel 10 自定义登录/注册不会进入仪表板页面
P粉162773626
P粉162773626 2023-09-05 14:04:13
0
2
428

我正在尝试制作自己的自定义 Laravel 10 登录/注册,因为我不想使用 breez 包,因为我想了解如何自己进行登录/注册。

但我似乎无法通过仪表板页面的身份验证。

我在仪表板函数上使用 if 语句 if(Auth::check()) 来对数据库中的用户进行身份验证。

但对我来说这不起作用,因为我不断收到从重定向回登录页面的错误消息(这只在我将新用户注册到数据库中时才会发生),但每当我尝试登录 我从登录功能中收到成功消息(进一步查看代码),同时仍在登录页面中。

AuthController(仪表板):

public function dashboard(): View { if(Auth::check()) { return view('auth.dashboard'); } return view('auth.login')->with('error', 'You are not allowed to access'); }

AuthController(登录):

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');

我还没有找到任何解决方案,因此如果有人可以帮助我,我将不胜感激。

P粉162773626
P粉162773626

全部回复 (2)
P粉006540600

hii,您的注销功能受到中间件的保护,您还需要添加仪表板路由中间件,您可以对需要身份验证中间件的路由进行分组。

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

    您的路线

    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']);

    您的控制器:

    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('/'); } }

    您的登录刀片

          Sign In  
    

    您的注册页面

          Register  
    

    我想这会解决您的所有疑问

      最新下载
      更多>
      网站特效
      网站源码
      网站素材
      前端模板
      关于我们 免责声明 Sitemap
      PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!