У меня есть некоторые проблемы с аутентификацией с использованием php artisan make: auth .Зарегистрируйтесь успешно.Но если я хочу войти.Он не может войти в личный кабинет / домашнюю страницу.Вернемся к индексу.Когда я проверяю на сетевой консоли в браузере, показывается состояние 302.
Это мой RegisterController.php
<?php
namespace App\Http\Controllers\Auth;
use App\Models\User;
use App\Models\Band;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Session;
use File;
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 = '/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
*/
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
protected function create(array $data, Request $request)
{
}
protected function showFormRegister(){
return view('auth.register');
}
protected function store(Request $request){
$this->validate($request, [
'Name' => 'required|string',
'UserName' => 'required',
'Email' =>'required|unique:users',
'Password' => 'required|min:6',
'ConfirmPassword' => 'required|same:Password',
'CategoryType' => 'required'
]);
$user = new User;
$user->username = $request->input('UserName');
$user->password = bcrypt($request->input('Password'));
$user->email = $request->input('Email');
$user->name = $request->input('Name');
$user->user_type = $request->input('CategoryType');
$user->save();
return redirect()->to('/')->with('message','register successfully');
}
}
мой просмотр страницы входа в систему
<form id="popup_login_form" method="post" action="{{ route('login') }}">
{{ csrf_field() }}
<input type="text" name="Username" id="login_username" value="username" placeholder="username">
@if ($errors->has('Username'))<span> <strong>{{ $errors->first('Username') }}</strong></span> @endif
<input type="password" name="Password" id="login_password" value="password" placeholder="password">
@if ($errors->has('Password'))<span> <strong>{{ $errors->first('Password') }}</strong></span> @endif
<input type="submit" value="Login" id="buttonLogin">
</form><!-- login form -->
Мой LoginController.php
<?php
namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Validator;
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 function redirectTo() {
if(Auth::user()->user_type == 'admin') {
return '/admin/home';
}
if(Auth::user()->user_type == 'band'){
return '/band/home';
}
}
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
public function username()
{
return 'username';
}
protected function validateLogin(Request $request){
$this->validate($request, [
'Username' => 'required',
'Password' => 'required|min:6'
]);
}
}
Моя миграция Пользователи
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('username');
$table->string('password');
$table->string('email')->unique();
$table->string('name');
$table->enum('user_type', ['customer', 'band', 'admin'])-
>default('customer');
$table->string('profile_picture')->nullable();
$table->datetime('last_login')->nullable();
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
Моя модель Пользователь
<?php
namespace App\Models;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password','username'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
protected $table = "users";
// Ambil data dari field yang bersangkutan
public function detail() {
return $this->hasOne('App\Models\\'.ucfirst($this->user_type));
}
}
Мой маршрут web.php
<?php
Route::get('/test', 'HomeController@testFunction');
Route::get('/', function () {
return view('welcome');
});
//Auth::routes();
//Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
Route::post('login', 'Auth\LoginController@login')->name('login');
Route::post('logout', 'Auth\LoginController@logout')->name('logout');
// Registration Routes...
Route::get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
Route::post('register', 'Auth\RegisterController@register');
// Password Reset Routes...
Route::get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request');
Route::post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email');
Route::get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
Route::post('password/reset', 'Auth\ResetPasswordController@reset');
Route::get('/band/home', 'HomeController@index')->name('home');
Route::group(['prefix'=>'register'], function(){
Route::get('/v1','Auth\RegisterController@showFormRegister');
Route::post('/store','Auth\RegisterController@store');
});