简体   繁体   English

laravel 登录不适用于生产中提供的凭据。 实际上它什么都不返回并再次重定向到登录页面

[英]laravel login doesn't work with provided credentials in production. In fact it returns nothing and redirects to the login page again

I'm using php artisan bootstrap --auth to generate views for my authentication.我正在使用php artisan bootstrap --auth为我的身份验证生成视图。

Problem问题

However, I'm not being able to login using the login page provided by the code.但是,我无法使用代码提供的登录页面登录。 Everything works fine in local but in production server just redirects to login page despite providing correct or incorrect credentials.在本地一切正常,但在生产服务器中,尽管提供了正确或不正确的凭据,但仍会重定向到登录页面。

login.blade.php登录.刀片.php


<form method="POST" action="{{ route('login') }}" class="row g-3">

    @csrf

    <div class="col-12">

      <label for="username" class="form-label">{{ __('username') }}</label>

      <input id="username" type="username" class="form-control form-control-lg @error('username') is-invalid @enderror"

        name="username" value="{{ old('username') }}" required autocomplete="username" autofocus>

      @error('username')

        <span class="invalid-feedback" role="alert">

          <strong>{{ $message }}</strong>

        </span>

      @enderror

    </div>

    <div class="col-12">

      <label for="password" class="form-label">{{ __('password') }}</label>

      <input id="password" type="password" class="form-control form-control-lg @error('password') is-invalid @enderror"

        name="password" required autocomplete="current-password">

      @error('password')

        <span class="invalid-feedback" role="alert">

          <strong>{{ $message }}</strong>

        </span>

      @enderror

    </div>

    <div class="col-12">

      <div class="form-check d-flex align-items-center">

        <input class="form-check-input me-3" type="checkbox" name="remember" id="remember"

          {{ old('remember') ? 'checked' : '' }}>

        <label class="form-check-label" for="remember">

          {{ __('مرا به خاطر بسپار') }}

        </label>

      </div>

    </div>

    <div class="col-12">

      <button type="submit" class="btn btn-lg btn-primary me-3">

        {{ __('login') }}

      </button>

      <a href="{{ route('register') }}" class="btn btn-lg btn-ghost-pink">

signup

      </a>

    </div>

  </form>

LoginController.php登录控制器.php

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

class LoginController extends Controller
{
    use AuthenticatesUsers;
    protected $redirectTo = RouteServiceProvider::HOME;
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
}

config/auth.php配置/auth.php

<?php

return [


    'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],


    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
    ],
    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Models\User::class,
        ],

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],
    'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
            'throttle' => 60,
        ],
    ],

    'password_timeout' => 10800,

];

provided .env on production在生产环境中提供.env

DB_CONNECTION=***
DB_HOST=***
DB_DATABASE=***
DB_USERNAME=***
DB_PASSWORD=***
DB_PORT=***
APP_ENV=production
APP_DEBUG=false
APP_KEY=base64:JFFvM6+apX5aMU6VOXaLhSiim49nob5m3Rf6zpUM/mc
LOG_CHANNEL=errorlog
APP_URL=https://charbanshimi.iran.liara.run
SESSION_DRIVER=file
SESSION_LIFETIME=1440
SESSION_DOMAIN=charbanshimi.iran.liara.run
SESSION_SECURE_COOKIE=false
  • I've tried what's been mentioned here as the correct answer so far.到目前为止,我已经尝试过这里提到的正确答案。 But it didn't solve the problem.但这并没有解决问题。

  • I've tested the req/res cycle with the browser already.我已经用浏览器测试了 req/res 周期。 But the login form submission seems not working even with invalid credentials and the server just redirects to the login page and I found no cause for this behavior.但是即使使用无效凭据,登录表单提交似乎也无法正常工作,服务器只是重定向到登录页面,我没有发现导致此行为的原因。

Laravel by default doesn't come shipped with usernames for login, they accept only emails, but you can make it accept a username or email. Laravel 默认不附带登录用户名,它们只接受电子邮件,但您可以让它接受用户名或 email。

Requirement , you must have a username field in the users table!要求users表中必须有username段!


Change in your blade the field as follows: I simply added to check for errors in the username or email fields, and to return the old value of username or email field.如下更改刀片字段:我只是添加了检查用户名或 email 字段中的错误,并返回用户名或 email 字段的旧值。

<input id="username" type="text" class="form-control form-control-lg @error('username', 'email') is-invalid @enderror " name="username" value="{{ old('username') ?: old('email') }}" required autocomplete="username" autofocus>

In your LoginController change and add the following!在您的 LoginController 中更改并添加以下内容!

public function __construct()
{
    $this->middleware('guest')->except('logout');
    $this->username = $this->useUsername();
}

public function useUsername()
{
    $username = request()->input('username');
 
    $fieldType = filter_var($username, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
 
    request()->merge([$fieldType => $username]);
 
    return $fieldType;
}
 

public function username()
{
    return $this->username;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM