Laravel登录 – 登录成功后,不保留并返回到登录页面

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

Laravel login - After the login is successful, it does not remain and returns to the login page

问题

以下是您要翻译的代码部分:

  1. 我尝试了几种登录方式,有效的是 `Auth::login($user);`,但他无法保持登录状态,而是回到登录页面。
  2. 登录控制器:
  3. ```php
  4. public function validateCode(Request $request)
  5. {
  6. $email = $request->email;
  7. // 获取用户
  8. $user = User::where('email', $email)->first();
  9. if ($user->count() > 0) {
  10. $validateCode = $request->input('validateCode');
  11. // 检查验证码
  12. if ($validateCode == $user['codConfirm']) {
  13. Auth::login($user, true);
  14. if (Auth::check())
  15. return redirect()->route('home');
  16. else {
  17. dd('else');
  18. return redirect()->action('Auth\LoginController@index', ['validator' => ['无法登录']]);
  19. }
  20. } else {
  21. return view('confirmar-usuario', ['email' => $email]);
  22. }
  23. } else {
  24. flash('无效用户')->error();
  25. return view('login');
  26. }
  27. }
英文:

I tried several ways to login, the one that works is Auth::login($user);, but he doesn't stay logged in, he goes back to the login page.

Login Controller:

  1. public function validateCode(Request $request)
  2. {
  3. $email = $request->email;
  4. // Get user
  5. $user = User::where('email', $email)->first();
  6. if ($user->count() > 0) {
  7. $validateCode = $request->input('validateCode');
  8. // Check the codes
  9. if ($validateCode == $user['codConfirm']) {
  10. Auth::login($user, true);
  11. if(Auth::check())
  12. return redirect()->route('home');
  13. else{
  14. dd('else');
  15. return redirect()->action('Auth\LoginController@index', ['validator' => ['Não foi possível fazer o login']]);
  16. }
  17. } else {
  18. return view('confirmar-usuario', ['email' => $email]);
  19. }
  20. }
  21. else {
  22. flash('Usuário inválido')->error();
  23. return view('login');
  24. }
  25. }

I will answer my own question as I found out and the question I had thought about has been deleted. So I'm posting here to help someone.

答案1

得分: 5

没问题,问题只在于 .env 文件。有两个重要的信息必须填写正确。'SESSION_DOMAIN' 和 'APP_URL'。
URL 必须没有协议和端口,如果有的话。
例如:

  1. APP_URL=127.0.0.1
  2. SESSION_DOMAIN=127.0.0.1
英文:

It's all right, the issue is only the .env. There are two important pieces of information that must be filled in correctly. 'SESSION_DOMAIN' and 'APP_URL'.
The url must be without the protocol and without the port, if any.
Ex:

  1. APP_URL=127.0.0.1
  2. SESSION_DOMAIN=127.0.0.1

huangapple
  • 本文由 发表于 2023年3月3日 20:11:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75626921.html
匿名

发表评论

匿名网友

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

确定