如何在Laravel 5.3中进行自定义身份验证 [英] How to make custom auth in laravel 5.3
问题描述
我在Laravel 5.3自定义身份验证中遇到问题,当我检查Auth::check()
并返回false
时想使用我自己的函数或页面.
这是用户控制器:
I'm facing problem in my Laravel 5.3 custom auth want to use my own functions or pages when I check Auth::check()
it returns false
.
Here is User controller:
namespace App\Http\Controllers;
use App\User;
use Illuminate\Support\Facades\Session;
use validatior;
use Auth;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function postSignUp(Request $request)
{
$validation = \Validator::make($request->all(), [
'email' => 'email|required|unique:users',
'password' => 'required|min:4'
]);
if ($validation->fails()) {
return redirect()->back()->withErrors($validation->errors());
} else {
$user = new User();
$user->email = $request->get("email");
$user->password = bcrypt($request->get['password']);
$user->save();
}
return redirect('signupPage');
}
public function postSignIn(Request $request)
{
$this->validate($request, [
'email' => 'required',
'password' => 'required',
]);
if (Auth::attempt(['email' => $request['email'], 'password' => $request['password']])) {
return redirect('users/profile');
}
dd(Auth::check());
exit;
}
}
登录后,我想在个人资料页面上重定向,但我的情况不起作用.我的路线是:
After sign in I want to redirect at profile page but my condition is not working. My routes are:
Route::group(['prefix' => 'users'], function(){
Route::group(['middleware' => 'guest'], function(){
Route::get('/signupPage','UserController@getSignUp');
Route::post('/register',array ('as'=>'signup', 'uses' =>'UserController@postSignUp', ));
Route::get('signInPage', array('as' => 'signInPage', 'uses' => 'UserController@getSignIn'));
Route::post('/postLogin', array('as' => 'postLogin', 'uses' => 'UserController@postSignIn'));
});
Route::group(['middleware' => 'auth'], function(){
Route::get('/profile', array('as' => 'profile', 'uses' => 'UserController@getProfile'));
Route::get('/logout', array('as'=>'logout', 'uses'=> 'UserController@logout'));
});
});
推荐答案
以下是对代码的一些修改
Here are some Modifications of your code
public function postSignUp(Request $request)
{
$email = $request->input('email');
$pass = $request->input('password');
$validation = \Validator::make($request->all(), [
'email' => 'email|required|unique:users',
'password' => 'required|min:4'
]);
if ($validation->fails()) {
return redirect()->back()->withErrors($validation->errors());
} else {
$pas = bcrypt($pass);
$user = new User();
$user->email = $email;
$user->password = $pas;
$user->save();
}
return redirect('users/signInPage');
}
在注册用户时请使用bcrypt,因为Auth
Attempt
函数默认使用此功能
Do use bcrypt while registering your User as this function is by default used by Auth
Attempt
function
public function postSignIn(Request $request)
{
$this->validate($request, [
'email' => 'required',
'password' => 'required',
]);
$email= $request->input['email'];
$password= $request->input['password'];
if (Auth::attempt(['email'=>$email,'password'=>$password']))
{
return redirect('users/profile');
}
return redirect('users/signInPage')->withErrors(['failure' => 'The credentials you entered did not match our records. Try again!',]);
}
最后,在使用默认中间件时,这将给您太多重定向错误,因为您的逻辑现在不同于默认中间件,这就是为什么您必须编写自定义中间件的原因,为此请运行此命令
php artisan make:middleware CutomAuth
这将创建您的自定义中间件,现在在您的逻辑中并在app/Http/Kernel.php
中注册为\ n后在其中编写代码.
'customauth' => \Illuminate\Auth\Middleware\CutomAuth::class,
你很好
finally this will give you too many redirects error as you using default middlewares, because your logic is now differ from the default one that's why you have to write your custom middleware, for this run this command
php artisan make:middleware CutomAuth
this will create your custom middleware now write code in it by your logic and after registering in app/Http/Kernel.php
as \n
'customauth' => \Illuminate\Auth\Middleware\CutomAuth::class,
you are good to go
这篇关于如何在Laravel 5.3中进行自定义身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!