Home > front end >  laravel login doesn't work with provided credentials in production. In fact it returns nothing
laravel login doesn't work with provided credentials in production. In fact it returns nothing

Time:01-24

I'm using php artisan bootstrap --auth to generate views for my authentication.

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


<form method="POST" action="{{ route('login') }}" >

    @csrf

    <div >

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

      <input id="username" type="username" 

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

      @error('username')

        <span  role="alert">

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

        </span>

      @enderror

    </div>

    <div >

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

      <input id="password" type="password" 

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

      @error('password')

        <span  role="alert">

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

        </span>

      @enderror

    </div>

    <div >

      <div >

        <input  type="checkbox" name="remember" id="remember"

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

        <label  for="remember">

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

        </label>

      </div>

    </div>

    <div >

      <button type="submit" >

        {{ __('login') }}

      </button>

      <a href="{{ route('register') }}" >

signup

      </a>

    </div>

  </form>

LoginController.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

<?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

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

CodePudding user response:

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.

Requirement, you must have a username field in the users table!


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.

<input id="username" type="text"  name="username" value="{{ old('username') ?: old('email') }}" required autocomplete="username" autofocus>

In your LoginController change and add the following!

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;
}
  • Related