我正在使用 Laravel 8 制作一個網路應用程式,但我的身份驗證有點奇怪。
- 我使用
php artisan ui:auth命令創建了身份驗證。 - 注冊作業正常:將新用戶添加到資料庫,登錄用戶,重定向到主頁。
問題是,當我現在嘗試登錄到同一用戶時,表單只會重繪 并且不會讓用戶登錄。
任何幫助深表感謝。如果您需要更多資訊,請告訴我!
相關代碼
網頁.php :
筆記:
- 在 PHPStorm 中,
Auth下劃線為“未定義的類身份驗證” /adminregister這是我現在注冊管理員帳戶的方式。
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('home');
});
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('/adminregister', function () {
return view('auth/adminregister');
});
Route::get('/getlocations', [App\Http\Controllers\LocationController::class, 'index']);
Route::post('/addlocation', [App\Http\Controllers\LocationController::class, 'store']);
用戶.php :
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var string[]
*/
protected $fillable = [
'username',
'password',
'role'
];
/**
* The attributes that should be hidden for serialization.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function locations()
{
return $this->hasMany(Location::class);
}
public function reviews()
{
return $this->hasMany(Review::class);
}
}
登錄控制器.php :
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = RouteServiceProvider::HOME;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
}
登錄.blade.php :
@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 hljs-string">">
<label for="username" hljs-number">4 col-form-label text-md-right">Username</label>
<div hljs-number">6">
<input id="username" type="text" hljs-built_in">error('username') is-invalid @enderror" name="username" value="{{ old('username') }}" required autocomplete="username" autofocus>
@error('username')
<span hljs-string">" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div hljs-string">">
<label for="password" hljs-number">4 col-form-label text-md-right">{{ __('Password') }}</label>
<div hljs-number">6">
<input id="password" type="password" hljs-built_in">error('password') is-invalid @enderror" name="password" required autocomplete="current-password">
@error('password')
<span hljs-string">" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div hljs-string">">
<div hljs-number">6 offset-md-4">
<div hljs-string">">
<input hljs-string">" type="checkbox" name="remember" id="remember" {{ old('remember') ? 'checked' : '' }}>
<label hljs-string">" for="remember">
{{ __('Remember Me') }}
</label>
</div>
</div>
</div>
<div hljs-number">0">
<div hljs-number">8 offset-md-4">
<button type="submit" hljs-string">">
{{ __('Login') }}
</button>
@if (Route::has('password.request'))
<a hljs-string">" href="{{ route('password.request') }}">
{{ __('Forgot Your Password?') }}
</a>
@endif
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
注冊控制器.php :
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use App\Models\User;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* @var string
*/
protected $redirectTo = RouteServiceProvider::HOME;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'username' => ['required', 'string', 'max:255'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\Models\User
*/
protected function create(array $data)
{
return User::create([
'username' => $data['username'],
'password' => Hash::make($data['password']),
'role' => $data['role']
]);
}
}
注冊.blade.php
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Register') }}</div>
<div class="card-body">
<form method="POST" action="{{ route('register') }}">
@csrf
<div hljs-string">">
<input name="role" type="hidden" value="0">
<label for="username" hljs-number">4 col-form-label text-md-right">{{ __('Username') }}</label>
<div hljs-number">6">
<input id="username" type="text" hljs-built_in">error('username') is-invalid @enderror" name="username" value="{{ old('username') }}" required autocomplete="username" autofocus>
@error('username')
<span hljs-string">" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
{{-- <div hljs-string">">--}}
{{-- <label for="email" hljs-number">4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>--}}
{{-- <div hljs-number">6">--}}
{{-- <input id="email" type="email" hljs-built_in">error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" required autocomplete="email">--}}
{{-- @error('email')--}}
{{-- <span hljs-string">" role="alert">--}}
{{-- <strong>{{ $message }}</strong>--}}
{{-- </span>--}}
{{-- @enderror--}}
{{-- </div>--}}
{{-- </div>--}}
<div hljs-string">">
<label for="password" hljs-number">4 col-form-label text-md-right">{{ __('Password') }}</label>
<div hljs-number">6">
<input id="password" type="password" hljs-built_in">error('password') is-invalid @enderror" name="password" required autocomplete="new-password">
@error('password')
<span hljs-string">" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div hljs-string">">
<label for="password-confirm" hljs-number">4 col-form-label text-md-right">{{ __('Confirm Password') }}</label>
<div hljs-number">6">
<input id="password-confirm" type="password" hljs-string">" name="password_confirmation" required autocomplete="new-password">
</div>
</div>
<div hljs-number">0">
<div hljs-number">6 offset-md-4">
<button type="submit" hljs-string">">
{{ __('Register') }}
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
用戶遷移:
<?php
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->id();
$table->string('username')->unique();
$table->string('password');
$table->tinyInteger('role');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
我嘗試過的事情
- 在網上搜索,發現一個看起來很有希望的執行緒,但解決方案是針對與我使用的身份驗證版本不同的版本:Laravel 5.2 login does not work, no redirect and no errors。
- 添加到達 后重定向回 home 的路由
/login,但這不會讓用戶登錄。 - 洗掉遷移并使用
php artisan migrate:fresh. - 其他一些事情現在讓我心煩意亂,但都無濟于事。
uj5u.com熱心網友回復:
默認情況下,laravel 使用電子郵件進行身份驗證,但如果您想使用用戶名(或其他名稱)登錄。您需要將 username() 方法覆寫到 LoginController 類中。您不需要更改 AuthenticatesUsers 中的內容,因為在我看來,這樣做不是一件好事。
/**
* Get the login username to be used by the controller.
*
* @return string
*/
public function username()
{
return 'username';
}
同樣,您也可以覆寫其他方法,但是,如果您使用電子郵件進行身份驗證,則不需要這樣做。
uj5u.com熱心網友回復:
我觀察到您正在使用username登錄 而不是email默認情況下。也許您可以嘗試更改以下內容AuthenticatesUsers?
從
public function username()
{
return 'email';
}
到
public function username()
{
return 'username';
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/363462.html
