Laravel 重定向 CustomUser 后 Auth guard 不会持续存在
Laravel Auth guard does not persist after redirect CustomUser
从 LoginController 重定向后 Auth::guard('user')->user() returns null
public function userPostLogin(array $collection)
{
return Auth::guard('web')->attempt($collection);
}
登录消费者
public function userLoginAuthentication(Request $request)
{
if ($request->get('phoneNumber') != null && $request->get('password') != null) {
$login = new LoginDTO($request->get('regionCode'). ' ' .$request->get('phoneNumber'), $request->get('password'), $request->get('role'));
$mapper = new LoginMapper();
return $this->userLoginConsumer->userPostLogin((array)$mapper->objectToCollection($login));}
登录服务
public function loginAuthentication(Request $request)
{
$this->userLoginService->userLoginAuthentication($request);
return redirect()->route('user-home');
登录控制器
Auth::guard('web')->user() returns 每一层中的正确用户。但是在重定向到 route('user-home') (或任何路由)时,相同的 Auth::guard('web')->user() returns null
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false,
],
'admin' => [
'driver' => 'session',
'provider' => 'admins'
],
'admin-api' => [
'driver' => 'token',
'provider' => 'admins'
],
'kitchen' => [
'driver' => 'session',
'provider' => 'kitchens'
],
'kitchen-api' => [
'driver' => 'token',
'provider' => 'kitchens'
]
],
'providers' => [
'users' => [
'driver' => 'jsonresponse',
'model' => CustomUser::class,
],
'admins' => [
'driver' => 'jsonresponse',
'model' => CustomUser::class,
],
'kitchens' => [
'driver' => 'jsonresponse',
'model' => CustomUser::class,
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
'throttle' => 60,
],
],
'password_timeout' => 10800,
auth.php
你可以像这样在你的授权过程之后得到这样的用户:
Auth::user();
你也可以这样检查:
if (Auth::check()) {
$user = Auth::user();
} else {
echo "We do not have a authenticated user";
}
并且您应该将 auth
中间件应用于这些路由
问题是我在实现 Illuminate\Contracts\Auth\UserProvider
来自
的对象时没有 return
public function retrieveById($identifier)
从 LoginController 重定向后 Auth::guard('user')->user() returns null
public function userPostLogin(array $collection)
{
return Auth::guard('web')->attempt($collection);
}
登录消费者
public function userLoginAuthentication(Request $request)
{
if ($request->get('phoneNumber') != null && $request->get('password') != null) {
$login = new LoginDTO($request->get('regionCode'). ' ' .$request->get('phoneNumber'), $request->get('password'), $request->get('role'));
$mapper = new LoginMapper();
return $this->userLoginConsumer->userPostLogin((array)$mapper->objectToCollection($login));}
登录服务
public function loginAuthentication(Request $request)
{
$this->userLoginService->userLoginAuthentication($request);
return redirect()->route('user-home');
登录控制器
Auth::guard('web')->user() returns 每一层中的正确用户。但是在重定向到 route('user-home') (或任何路由)时,相同的 Auth::guard('web')->user() returns null
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false,
],
'admin' => [
'driver' => 'session',
'provider' => 'admins'
],
'admin-api' => [
'driver' => 'token',
'provider' => 'admins'
],
'kitchen' => [
'driver' => 'session',
'provider' => 'kitchens'
],
'kitchen-api' => [
'driver' => 'token',
'provider' => 'kitchens'
]
],
'providers' => [
'users' => [
'driver' => 'jsonresponse',
'model' => CustomUser::class,
],
'admins' => [
'driver' => 'jsonresponse',
'model' => CustomUser::class,
],
'kitchens' => [
'driver' => 'jsonresponse',
'model' => CustomUser::class,
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
'throttle' => 60,
],
],
'password_timeout' => 10800,
auth.php
你可以像这样在你的授权过程之后得到这样的用户:
Auth::user();
你也可以这样检查:
if (Auth::check()) {
$user = Auth::user();
} else {
echo "We do not have a authenticated user";
}
并且您应该将 auth
中间件应用于这些路由
问题是我在实现 Illuminate\Contracts\Auth\UserProvider
来自
public function retrieveById($identifier)