Laravel 10自定义登录/注册未跳转到仪表板页面。

huangapple go评论106阅读模式
英文:

Laravel 10 custom login/registration not going to dashboard page

问题

我正在尝试创建自己的自定义 Laravel 10 登录/注册功能,因为我不想使用 Breez 包,我想学习如何自己制作登录/注册功能。

但我似乎无法通过仪表板页面的身份验证。我在我的仪表板函数中使用了一个 if 语句 if(Auth::check()) 来在数据库中验证用户。

但对我来说,这不起作用,因为我在从重定向回登录页面时不断收到错误消息(只有在我将新用户注册到数据库时才会发生这种情况),但每当我尝试登录时,我仍然在登录页面上获得来自我的登录函数的成功消息(请参见下面的代码)。

AuthController(仪表板):

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

AuthController(登录):

  1. public function loginPost(Request $request): RedirectResponse
  2. {
  3. $request->validate([
  4. 'email' => 'required',
  5. 'password' => 'required'
  6. ]);
  7. $credentials = $request->only('email', 'password');
  8. if(Auth::attempt($credentials)) {
  9. $request->session()->regenerate();
  10. return redirect()->intended(route('dashboard'))->with('success', 'You have successfully logged in');
  11. }
  12. return redirect(route('login'))->with('error', 'Oppes! You have entered invalid credentials');
  13. }

web.php:

  1. Route::get('/register', [AuthController::class, 'register'])->name('register');
  2. Route::post('/register', [AuthController::class, 'registerPost'])->name('register.post');
  3. Route::get('/login', [AuthController::class, 'login'])->name('login');
  4. Route::post('/login', [AuthController::class, 'loginPost'])->name('login.post');
  5. Route::get('/dashboard', [AuthController::class, 'dashboard'])->name('dashboard');
  6. Route::post('/logout', [AuthController::class, 'logout'])->middleware('auth')->name('logout');

我尚未找到任何解决方案,所以如果有人可以帮助我,将不胜感激。

英文:

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):

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

AuthController (Login):

  1. public function loginPost(Request $request): RedirectResponse
  2. {
  3. $request->validate([
  4. 'email' => 'required',
  5. 'password' => 'required'
  6. ]);
  7. $credentials = $request->only('email', 'password');
  8. if(Auth::attempt($credentials)) {
  9. $request->session()->regenerate();
  10. return redirect()->intended(route('dashboard'))->with('success', 'You have successfully logged in');
  11. }
  12. return redirect(route('login'))->with('error', 'Oppes! You have entered invalid credentials');
  13. }

web.php

  1. Route::get('/register', [AuthController::class, 'register'])->name('register');
  2. Route::post('/register', [AuthController::class, 'registerPost'])->name('register.post');
  3. Route::get('/login', [AuthController::class, 'login'])->name('login');
  4. Route::post('/login', [AuthController::class, 'loginPost'])->name('login.post');
  5. Route::get('/dashboard', [AuthController::class, 'dashboard'])->name('dashboard');
  6. 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.

答案1

得分: 0

你的注销功能受到中间件的保护,你还需要为仪表板路由添加一个中间件,你可以将需要身份验证中间件的路由分组。

  1. Route::middleware('auth')->group(function () {
  2. Route::get('/dashboard', [AuthController::class, 'dashboard'])->name('dashboard');
  3. Route::post('/logout', [AuthController::class, 'logout'])->name('logout');
  4. });
英文:

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 .

  1. Route::middleware('auth')->group(function () {
  2. Route::get('/dashboard', [AuthController::class, 'dashboard'])->name('dashboard');
  3. Route::post('/logout', [AuthController::class, 'logout'])->name('logout');
  4. });

答案2

得分: 0

您的路由

  1. Route::get('login', [FrontendAuthController::class, 'loginGet'])->name('login');
  2. Route::post('login', [FrontendAuthController::class, 'loginPost']);
  3. Route::post('logout', [FrontendAuthController::class, 'logout'])->name('logout');
  4. Route::get('register', [FrontendAuthController::class, 'registerGet'])->name('register');
  5. Route::post('register', [FrontendAuthController::class, 'registerPost']);

您的控制器:

  1. <?php
  2. namespace App\Http\Controllers\Frontend;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\User;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\Auth;
  7. use Illuminate\Support\Facades\Hash;
  8. use Illuminate\Support\Facades\Session;
  9. class FrontendAuthController extends Controller
  10. {
  11. public function loginGet(Request $request)
  12. {
  13. return view('front.auth.login');
  14. }
  15. public function loginPost(Request $request)
  16. {
  17. $request->validate([
  18. 'email' => 'required|email',
  19. 'password' => 'required',
  20. ], [
  21. 'email.required' => '邮箱字段必填。',
  22. 'email.email' => '请输入有效的电子邮件地址。',
  23. 'password.required' => '密码字段必填。',
  24. ]);
  25. $credentials = $request->only('email', 'password');
  26. if (Auth::attempt($credentials)) {
  27. return redirect()->intended('/');
  28. } else {
  29. return redirect()->back()->withErrors(['email' => '这些凭据与我们的记录不匹配。']);
  30. }
  31. }
  32. public function logout()
  33. {
  34. Auth::logout();
  35. Session::flush(); // 清除所有会话数据
  36. Session::regenerate(); // 重新生成会话ID
  37. return redirect()->route('login');
  38. }
  39. public function registerGet()
  40. {
  41. return view('front.auth.register');
  42. }
  43. public function registerPost(Request $request)
  44. {
  45. $request->validate([
  46. 'name' => 'required|string|max:255',
  47. 'email' => 'required|email|unique:users,email',
  48. 'password' => 'required|min:8|confirmed',
  49. ], [
  50. 'name.required' => '姓名字段必填。',
  51. 'email.required' => '邮箱字段必填。',
  52. 'email.email' => '请输入有效的电子邮件地址。',
  53. 'email.unique' => '此电子邮件地址已注册。',
  54. 'password.required' => '密码字段必填。',
  55. 'password.min' => '密码必须至少包含8个字符。',
  56. 'password.confirmed' => '密码确认不匹配。',
  57. ]);
  58. // 创建新用户记录
  59. $user = new User();
  60. $user->name = $request->input('name');
  61. $user->email = $request->input('email');
  62. $user->password = Hash::make($request->input('password'));
  63. $user->save();
  64. // 登录新注册的用户
  65. Auth::login($user);
  66. // 将用户重定向到主页或其他所需页面
  67. return redirect()->intended('/');
  68. }
  69. }

您的登录视图:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>登录</title>
  8. <link rel="stylesheet" href="{{ url('frontend/css/style.css') }}">
  9. </head>
  10. <body>
  11. <div class="main">
  12. <section class="signup">
  13. <div class="container">
  14. <div class="signup-content">
  15. <form id="signup-form" class="signup-form" method="POST" action="{{ url('login') }}">
  16. @csrf
  17. <h2 class="form-title">登录</h2>
  18. <div class="form-group">
  19. <input type="email" class="form-input" value="{{ old('email') }}" name="email"
  20. id="email" placeholder="您的电子邮件" />
  21. </div>
  22. <div class="form-group">
  23. <input type="text" class="form-input" name="password" id="password"
  24. placeholder="密码" />
  25. <span toggle="#password" class="zmdi zmdi-eye field-icon toggle-password"></span>
  26. </div>
  27. @error('email')
  28. <div style="color: red">{{ $message }}</div>
  29. @enderror
  30. @error('password')
  31. <div style="color: red">{{ $message }}</div>
  32. @enderror
  33. <div class="form-group">
  34. <input type="submit" name="submit" id="submit" class="form-submit" value="登录" />
  35. </div>
  36. </form>
  37. <p class="loginhere">
  38. 新用户? <a href="reg-form.html" class="loginhere-link">在这里注册</a>
  39. </p>
  40. </div>
  41. </div>
  42. </section>
  43. </div>
  44. </body>
  45. </html>

您的注册视图:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>注册</title>
  8. <link rel="stylesheet" href="{{ url('frontend/css/style.css') }}">
  9. </head>
  10. <body>
  11. <div class="main">
  12. <section class="signup">
  13. <div class="container">
  14. <div class="signup-content">
  15. <form id="signup-form" class="signup-form" method="POST" action="{{ route('register') }}">
  16. @csrf
  17. <h2 class="form-title">注册</h2>
  18. <div class="form-group">
  19. <input type="text" class="form-input" value="{{ old('name') }}" name="name" id="name"
  20. placeholder="您的姓名" />
  21. </div>
  22. <div class="form-group">
  23. <input type="email" class="form-input" value="{{ old('email') }}" name="email"
  24. id="email" placeholder="您的电子邮件" />
  25. </div>
  26. <div class="form-group">
  27. <input type="password" class="form-input" name="password" id="password"
  28. placeholder="密码" />
  29. <span toggle="#password" class="zmdi zmdi-eye field-icon toggle-password"></span>
  30. </div>
  31. <div class="form-group">
  32. <input type="password" class="form-input" name="password_confirmation"
  33. id="password_confirmation" placeholder="确认密码" />
  34. </div>
  35. @error('name')
  36. <div style="color: red">{{ $message }}</div>
  37. @enderror
  38. @error('email')
  39. <div style="color: red">{{ $message }}</div>
  40. @enderror
  41. @error('password')
  42. <div style="color: red">{{ $message }}</div>
  43. @enderror
  44. <div
  45. <details>
  46. <summary>英文:</summary>
  47. Your Routes
  48. Route::get(&#39;login&#39;, [FrontendAuthController::class, &#39;loginGet&#39;])-&gt;name(&#39;login&#39;);
  49. Route::post(&#39;login&#39;, [FrontendAuthController::class, &#39;loginPost&#39;]);
  50. Route::post(&#39;logout&#39;, [FrontendAuthController::class, &#39;logout&#39;])-&gt;name(&#39;logout&#39;);
  51. Route::get(&#39;register&#39;, [FrontendAuthController::class, &#39;registerGet&#39;])-&gt;name(&#39;register&#39;);
  52. Route::post(&#39;register&#39;, [FrontendAuthController::class, &#39;registerPost&#39;]);
  53. Your controller :
  54. &lt;?php
  55. namespace App\Http\Controllers\Frontend;
  56. use App\Http\Controllers\Controller;
  57. use App\Models\User;
  58. use Illuminate\Http\Request;
  59. use Illuminate\Support\Facades\Auth;
  60. use Illuminate\Support\Facades\Hash;
  61. use Illuminate\Support\Facades\Session;
  62. class FrontendAuthController extends Controller
  63. {
  64. public function loginGet(Request $request)
  65. {
  66. return view(&#39;front.auth.login&#39;);
  67. }
  68. public function loginPost(Request $request)
  69. {
  70. $request-&gt;validate([
  71. &#39;email&#39; =&gt; &#39;required|email&#39;,
  72. &#39;password&#39; =&gt; &#39;required&#39;,
  73. ], [
  74. &#39;email.required&#39; =&gt; &#39;The email field is required.&#39;,
  75. &#39;email.email&#39; =&gt; &#39;Please enter a valid email address.&#39;,
  76. &#39;password.required&#39; =&gt; &#39;The password field is required.&#39;,
  77. ]);
  78. $credentials = $request-&gt;only(&#39;email&#39;, &#39;password&#39;);
  79. if (Auth::attempt($credentials)) {
  80. return redirect()-&gt;intended(&#39;/&#39;);
  81. } else {
  82. return redirect()-&gt;back()-&gt;withErrors([&#39;email&#39; =&gt; &#39;These credentials do not match our records.&#39;]);
  83. }
  84. }
  85. public function logout()
  86. {
  87. Auth::logout();
  88. Session::flush(); // Clear all session data
  89. Session::regenerate(); // Regenerate the session ID
  90. return redirect()-&gt;route(&#39;login&#39;);
  91. }
  92. public function registerGet()
  93. {
  94. return view(&#39;front.auth.register&#39;);
  95. }
  96. public function registerPost(Request $request)
  97. {
  98. $request-&gt;validate([
  99. &#39;name&#39; =&gt; &#39;required|string|max:255&#39;,
  100. &#39;email&#39; =&gt; &#39;required|email|unique:users,email&#39;,
  101. &#39;password&#39; =&gt; &#39;required|min:8|confirmed&#39;,
  102. ], [
  103. &#39;name.required&#39; =&gt; &#39;The name field is required.&#39;,
  104. &#39;email.required&#39; =&gt; &#39;The email field is required.&#39;,
  105. &#39;email.email&#39; =&gt; &#39;Please enter a valid email address.&#39;,
  106. &#39;email.unique&#39; =&gt; &#39;This email address is already registered.&#39;,
  107. &#39;password.required&#39; =&gt; &#39;The password field is required.&#39;,
  108. &#39;password.min&#39; =&gt; &#39;The password must be at least 8 characters.&#39;,
  109. &#39;password.confirmed&#39; =&gt; &#39;The password confirmation does not match.&#39;,
  110. ]);
  111. // Create a new user record
  112. $user = new User();
  113. $user-&gt;name = $request-&gt;input(&#39;name&#39;);
  114. $user-&gt;email = $request-&gt;input(&#39;email&#39;);
  115. $user-&gt;password = Hash::make($request-&gt;input(&#39;password&#39;));
  116. $user-&gt;save();
  117. // Log in the newly registered user
  118. Auth::login($user);
  119. // Redirect the user to the home page or any other desired page
  120. return redirect()-&gt;intended(&#39;/&#39;);
  121. }
  122. }
  123. Your Login blade
  124. &lt;!DOCTYPE html&gt;
  125. &lt;html&gt;
  126. &lt;head&gt;
  127. &lt;meta charset=&quot;UTF-8&quot;&gt;
  128. &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  129. &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;ie=edge&quot;&gt;
  130. &lt;title&gt;Sign In&lt;/title&gt;
  131. &lt;link rel=&quot;stylesheet&quot; href=&quot;{{ url(&#39;frontend/css/style.css&#39;) }}&quot;&gt;
  132. &lt;/head&gt;
  133. &lt;body&gt;
  134. &lt;div class=&quot;main&quot;&gt;
  135. &lt;section class=&quot;signup&quot;&gt;
  136. &lt;div class=&quot;container&quot;&gt;
  137. &lt;div class=&quot;signup-content&quot;&gt;
  138. &lt;form id=&quot;signup-form&quot; class=&quot;signup-form&quot; method=&quot;POST&quot; action=&quot;{{ url(&#39;login&#39;) }}&quot;&gt;
  139. @csrf
  140. &lt;!-- &lt;img src=&quot;images/logo.svg&quot; alt=&quot;&quot;&gt;--&gt;
  141. &lt;h2 class=&quot;form-title&quot;&gt;Login Here&lt;/h2&gt;
  142. &lt;div class=&quot;form-group&quot;&gt;
  143. &lt;input type=&quot;email&quot; class=&quot;form-input&quot; value=&quot;{{ old(&#39;email&#39;) }}&quot; name=&quot;email&quot;
  144. id=&quot;email&quot; placeholder=&quot;Your Email&quot; /&gt;
  145. &lt;/div&gt;
  146. &lt;div class=&quot;form-group&quot;&gt;
  147. &lt;input type=&quot;text&quot; class=&quot;form-input&quot; name=&quot;password&quot; id=&quot;password&quot;
  148. placeholder=&quot;Password&quot; /&gt;
  149. &lt;span toggle=&quot;#password&quot; class=&quot;zmdi zmdi-eye field-icon toggle-password&quot;&gt;&lt;/span&gt;
  150. &lt;/div&gt;
  151. @error(&#39;email&#39;)
  152. &lt;div style=&quot;color: red&quot;&gt;{{ $message }}&lt;/div&gt;
  153. @enderror
  154. @error(&#39;password&#39;)
  155. &lt;div style=&quot;color: red&quot;&gt;{{ $message }}&lt;/div&gt;
  156. @enderror
  157. &lt;div class=&quot;form-group&quot;&gt;
  158. &lt;input type=&quot;submit&quot; name=&quot;submit&quot; id=&quot;submit&quot; class=&quot;form-submit&quot; value=&quot;Sign in&quot; /&gt;
  159. &lt;/div&gt;
  160. &lt;/form&gt;
  161. &lt;p class=&quot;loginhere&quot;&gt;
  162. New User? &lt;a href=&quot;reg-form.html&quot; class=&quot;loginhere-link&quot;&gt;Register here&lt;/a&gt;
  163. &lt;/p&gt;
  164. &lt;/div&gt;
  165. &lt;/div&gt;
  166. &lt;/section&gt;
  167. &lt;/div&gt;
  168. &lt;/body&gt;
  169. &lt;/html&gt;
  170. Your Register page
  171. &lt;!DOCTYPE html&gt;
  172. &lt;html&gt;
  173. &lt;head&gt;
  174. &lt;meta charset=&quot;UTF-8&quot;&gt;
  175. &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  176. &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;ie=edge&quot;&gt;
  177. &lt;title&gt;Register&lt;/title&gt;
  178. &lt;link rel=&quot;stylesheet&quot; href=&quot;{{ url(&#39;frontend/css/style.css&#39;) }}&quot;&gt;
  179. &lt;/head&gt;
  180. &lt;body&gt;
  181. &lt;div class=&quot;main&quot;&gt;
  182. &lt;section class=&quot;signup&quot;&gt;
  183. &lt;div class=&quot;container&quot;&gt;
  184. &lt;div class=&quot;signup-content&quot;&gt;
  185. &lt;form id=&quot;signup-form&quot; class=&quot;signup-form&quot; method=&quot;POST&quot; action=&quot;{{ route(&#39;register&#39;) }}&quot;&gt;
  186. @csrf
  187. &lt;h2 class=&quot;form-title&quot;&gt;Register Here&lt;/h2&gt;
  188. &lt;div class=&quot;form-group&quot;&gt;
  189. &lt;input type=&quot;text&quot; class=&quot;form-input&quot; value=&quot;{{ old(&#39;name&#39;) }}&quot; name=&quot;name&quot; id=&quot;name&quot;
  190. placeholder=&quot;Your Name&quot; /&gt;
  191. &lt;/div&gt;
  192. &lt;div class=&quot;form-group&quot;&gt;
  193. &lt;input type=&quot;email&quot; class=&quot;form-input&quot; value=&quot;{{ old(&#39;email&#39;) }}&quot; name=&quot;email&quot;
  194. id=&quot;email&quot; placeholder=&quot;Your Email&quot; /&gt;
  195. &lt;/div&gt;
  196. &lt;div class=&quot;form-group&quot;&gt;
  197. &lt;input type=&quot;password&quot; class=&quot;form-input&quot; name=&quot;password&quot; id=&quot;password&quot;
  198. placeholder=&quot;Password&quot; /&gt;
  199. &lt;span toggle=&quot;#password&quot; class=&quot;zmdi zmdi-eye field-icon toggle-password&quot;&gt;&lt;/span&gt;
  200. &lt;/div&gt;
  201. &lt;div class=&quot;form-group&quot;&gt;
  202. &lt;input type=&quot;password&quot; class=&quot;form-input&quot; name=&quot;password_confirmation&quot;
  203. id=&quot;password_confirmation&quot; placeholder=&quot;Confirm Password&quot; /&gt;
  204. &lt;/div&gt;
  205. @error(&#39;name&#39;)
  206. &lt;div style=&quot;color: red&quot;&gt;{{ $message }}&lt;/div&gt;
  207. @enderror
  208. @error(&#39;email&#39;)
  209. &lt;div style=&quot;color: red&quot;&gt;{{ $message }}&lt;/div&gt;
  210. @enderror
  211. @error(&#39;password&#39;)
  212. &lt;div style=&quot;color: red&quot;&gt;{{ $message }}&lt;/div&gt;
  213. @enderror
  214. &lt;div class=&quot;form-group&quot;&gt;
  215. &lt;input type=&quot;submit&quot; name=&quot;submit&quot; id=&quot;submit&quot; class=&quot;form-submit&quot; value=&quot;Register&quot; /&gt;
  216. &lt;/div&gt;
  217. &lt;/form&gt;
  218. &lt;p class=&quot;loginhere&quot;&gt;
  219. Already have an account? &lt;a href=&quot;{{ route(&#39;login&#39;) }}&quot; class=&quot;loginhere-link&quot;&gt;Login here&lt;/a&gt;
  220. &lt;/p&gt;
  221. &lt;/div&gt;
  222. &lt;/div&gt;
  223. &lt;/section&gt;
  224. &lt;/div&gt;
  225. &lt;/body&gt;
  226. &lt;/html&gt;
  227. I think This will solve your all queries
  228. </details>

huangapple
  • 本文由 发表于 2023年7月17日 17:26:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76703084.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定