Utilisation du module d'authentification de Laravel pour l'enregistrement et la connexion des utilisateurs.
Les inscriptions sont actuellement disponibles. Il y a des données dans MySQL.
Mais cela a échoué lors de la connexion. Revenez à la page de connexion.
Veuillez me dire comment le résoudre.
La connexion actuelle échoue, donc Auth::user() pour obtenir les informations d'authentification de l'utilisateur est NULL
Voici mon code :
Route::group(['middleware' => ['web']], function () {
//认证路由
Route::get('auth/login','Auth\AuthController@getLogin');
Route::post('auth/login','Auth\AuthController@postLogin');
Route::get('auth/logout','Auth\AuthController@getLogout');
//注册路由
Route::get('auth/register','Auth\AuthController@getRegister');
Route::post('auth/register','Auth\AuthController@postRegister');
});
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
class AuthController extends Controller
{
/*
|--------------------------------------------------------------------------
| Registration & Login Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users, as well as the
| authentication of existing users. By default, this controller uses
| a simple trait to add these behaviors. Why don't you explore it?
|
*/
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
/**
* Where to redirect users after login / registration.
*
* @var string
*/
protected $redirectTo = '/photo';
/**
* Create a new authentication controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware($this->guestMiddleware(), ['except' => 'logout']);
}
/**
* 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, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
}
Page d'inscription
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
{{Form::open([
'url'=>'/auth/register',
'method'=>'POST'
])}}
{!! csrf_field() !!}
<p>
Name
<input type="text" name="name" value="{{ old('name') }}">
</p>
<p>
Email
<input type="email" name="email" value="{{ old('email') }}">
</p>
<p>
Password
<input type="password" name="password">
</p>
<p>
Confirm Password
<input type="password" name="password_confirmation">
</p>
<p>
<button type="submit">Register</button>
</p>
{{ Form::close() }}
</body>
</html>
Page de connexion
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
{{ Form::open([
'url'=>'/auth/login',
'method'=>'POST'
]) }}
{{ csrf_field() }}
<p>
Email:
<input type="email" name="name" value="{{ old('email') }}">
</p>
<p>
Password:
<input type="password" name="password" id="password"/>
</p>
<p>
<input type="checkbox" name="remember">Remember Me
</p>
<p>
<button type="submit">Login</button>
</p>
{{Form::close()}}
</body>
</html>
Description du lien
Regardez mon article :