Warm tip: This article is reproduced from stackoverflow.com, please click
laravel

My login form only reloads the page but doesn't log the user in

发布于 2020-03-27 15:39:59

My login form is broken after I took out the "email" and "name" from the form, took out "email" and "name" from protected fillable in User.php. I also made the redundant fields nullable in the database. The user should be able to login using their "username" and "password". The registration form works fine, its just the login form that is causing issue.

login.blade.php @extends('layouts.app')

@section('content')
<div class="container">
<div class="row justify-content-center">
    <div class="col-md-8">
        <div class="card">
            <div class="card-header">{{ __('Login') }}</div>

            <div class="card-body">
                <form method="POST" action="{{ route('login') }}">
                    @csrf

                    <div class="form-group row">
                        <label for="username" class="col-md-4 col-form-label text-md-right">{{ __('Username') }}</label>

                        <div class="col-md-6">
                            <input id="username" type="username" class="form-control @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>

                    <div class="form-group row">
                        <label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>

                        <div class="col-md-6">
                            <input id="password" type="password" class="form-control @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>

                    <div class="form-group row">
                        <div class="col-md-6 offset-md-4">
                            <div class="form-check">
                                <input class="form-check-input" type="checkbox" name="remember" id="remember" {{ old('remember') ? 'checked' : '' }}>

                                <label class="form-check-label" for="remember">
                                    {{ __('Remember Me') }}
                                </label>
                            </div>
                        </div>
                    </div>

                    <div class="form-group row mb-0">
                        <div class="col-md-8 offset-md-4">
                            <button type="submit" class="btn btn-primary">
                                {{ __('Login') }}
                            </button>

                            @if (Route::has('password.request'))
                                <a class="btn btn-link" href="{{ route('password.request') }}">
                                    {{ __('Forgot Your Password?') }}
                                </a>
                            @endif
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
</div>
@endsection

The model User.php

namespace App;



use App\Mail\NewUserWelcomeMail;

use Illuminate\Notifications\Notifiable;

use Illuminate\Contracts\Auth\MustVerifyEmail;

use Illuminate\Foundation\Auth\User as Authenticatable;

use Illuminate\Support\Facades\Mail;



class User extends Authenticatable

{

use Notifiable;


protected $fillable = [

    'username', 'password',

];





protected $hidden = [

    'password', 'remember_token',

];

protected $casts = [

    'email_verified_at' => 'datetime',

];



protected static function boot()

{

    parent::boot();



    static::created(function ($user) {

        $user->profile()->create([

            'title' => $user->username,

        ]);




    });

}



public function posts()

{

    return $this->hasMany(Post::class)->orderBy('created_at', 'DESC');

}



public function following()

{

    return $this->belongsToMany(Profile::class);

}



public function profile()

{

    return $this->hasOne(Profile::class);

}





public function tasks()
{
    return $this->hasMany(Task::class);
}

}

The database table

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateUsersTable extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name')->nullable();
        $table->string('email')->nullable();
        $table->string('username')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
    }


public function down()
{
    Schema::dropIfExists('users');
}
}

LoginController.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');
}
}
Questioner
jsmith
Viewed
137
hasan05 2020-01-31 17:28

Add this function to App\Http\Controller\Auth\LoginController.php.

It will change your login field email to username. which will solve your problem for now.

public function username()
{
    return 'username';
}

One suggestion for you. have a look on "how to use authentication of a framework."