登录我使用laravel进行用户凭证的外部验证,在Auth :: user()中存储的重定向数据不会持久且Auth :: check()= false,但是会话数据保存正确。登录后,如果我进入仪表板,我将被重定向到登录。 routes.php文件
Route::group(['middleware' => ['web']], function () {
Route::get('/test', [
'as' => 'test',
function () {
echo "Session------>";
var_dump(\Illuminate\Support\Facades\Session::get('user'));//Returns user data
echo "User------>";
var_dump(\Illuminate\Support\Facades\Auth::user());// Returns null
echo "\ncheck user.------->";
var_dump(\Illuminate\Support\Facades\Auth::check());//Returns false
}
]);
});
//Login
Route::group(['middleware' =>[ 'web']], function () {
Route::get('/login', ['as' => 'login', 'uses' => 'AuthController@login']);
Route::post('/login', ['as' => 'login', 'uses' => 'AuthController@loginpost']);
Route::get('/logout', ['as' => 'logout', 'uses' => 'AuthController@logout']);
});
Route::group(['middleware' => ['auth', 'web']], function () {
Route::get('/', ['as' => 'home', 'uses' => 'DashboardController@index']);
//Dashboard
Route::get("/dashboard", ['as' => 'dashboard', 'uses' => 'DashboardController@index']);
}
AuthController.php
class AuthController extends Controller
{
public function login()
{
return view('auth/login');
}
public function loginpost(LoginRequest $request)
{
$credentials = $request->only('username', 'password');
$loginRequest = new LoginUserRequest($credentials['username'], md5($credentials['password']));
$userRepo = new MongoUserRepository();
$service = new GetUserByCredentialsService($userRepo, $loginRequest);
$authUser = $service->handle(new UserObjectPresenter());
var_dump($authUser);
if (isset($authUser)) {
$us = new User();
$us->id = $authUser->id;
$us->email = $authUser->email;
$us->fullname = $authUser->fullname;
Auth::login($us);
$user = Auth::user();
Session::put('user', $user);
Session::save();
echo "----saved user------";
var_dump(Auth::user()); //Returns correct user data
echo "Auth::check()->";
var_dump(Auth::check()); //Returns true
return redirect()->route('test');
} else {
return redirect()->route('login')->withInput()->withErrors(['Invalid email or password.']);
}
}
}
$ service-> handle(new UserObjectPresenter());如果登录正确,则返回null或用户数据。
答案 0 :(得分:0)
使用Auth ::尝试登录后我们实际开始你的会话,否则Auth :: check总是返回false, 使用以下代码
{{1}}
答案 1 :(得分:0)
你没有通过现有的'用户记录到Auth::login
。它没有ID,因此无法在下一次请求时撤回。
保存该用户记录或使用现有记录传递到Auth::login