gpt4 book ai didi

php - 如何在 Laravel 5.3 中进行自定义身份验证

转载 作者:可可西里 更新时间:2023-10-31 23:37:13 27 4
gpt4 key购买 nike

当我检查 Auth::check() 时,我在 Laravel 5.3 自定义身份验证中遇到问题想要使用我自己的函数或页面,它返回 false。这是用户 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;
}
}

登录后,我想在个人资料页面重定向,但我的情况不起作用。我的路线是:

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'));
});
});

最佳答案

这里是你的代码的一些修改

    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 功能使用

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, 一切顺利

关于php - 如何在 Laravel 5.3 中进行自定义身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41461195/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com