I'm using php artisan bootstrap --auth
to generate views for my authentication.
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.
<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>
<?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');
}
}
<?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,
];
.env
on productionDB_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.
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" 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!
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;
}