The first one.
Route::get('test', function () { session(['wechat.oauth_user'=>1]); dd(session('wechat.oauth_user')); });
Output 1, and then I put session(['wechat.oauth_user'=>1]);
a comment
Route::get('test', function () { //session(['wechat.oauth_user'=>1]); dd(session('wechat.oauth_user')); });
Refresh the page, output null, which is why?
A second question
I used the Overtrue/wechat plug-in, the middleware with the plugin to successfully obtain the user information, and then the user route also output the session
public function handle($request, Closure $next, $guard = null) { $wechat = app('EasyWeChat\\Foundation\\Application'); if (!session('wechat.oauth_user')) { if ($request->has('state') && $request->has('code')) { session(['wechat.oauth_user' => $wechat->oauth->user()]); return redirect()->to(url($request->url(), array_except($request->query(), ['code', 'state']))); } return $wechat->oauth->redirect($request->fullUrl()); } return $next($request); }
Route::get('user', function () { dd(session('wechat.oauth_user')); });
On this basis, I have built my own middlewareUserMiddleware
public function handle($request, Closure $next) { if(Auth::guard('web')->guest()){ if (!session('wechat.oauth_user')) { return redirect('user'); } else{ User::CreateOrLogin(session('wechat.oauth_user')); } } return $next($request); }
User.php
Method CreateOrLogin
is
public static function CreateOrLogin($data){ $user=self::getByOpenid($data['id']); if(!$user){ $create=['openid'=>$data['id']]; $user=self::create($create); } Auth::guard('web')->login($user); } public static function getByOpenid($openid){ return self::where('openid','=',$openid)->first(); }
Everything is OK, but each refresh page will be executed to get user information interface, and then commented out, found Auth::guard('web')->login($user);
that the comment dropped, refresh the page will not jump to, plus to jump to get user information interface, it feels like login will clear out the session
Question Three
Route::group(['middleware' => ['web']], function () { Route::get('test', function () { dd(auth()->user()); }); Route::get('test2', function () { dd(auth()->loginUsingId(1)); });});
First visit test2, login successfully, then access test, Output null, why ...
Question one and question two and question three are not related, seek the great God solution
Reply content:
The first one.
Route::get('test', function () { session(['wechat.oauth_user'=>1]); dd(session('wechat.oauth_user')); });
Output 1, and then I put session(['wechat.oauth_user'=>1]);
a comment
Route::get('test', function () { //session(['wechat.oauth_user'=>1]); dd(session('wechat.oauth_user')); });
Refresh the page, output null, which is why?
A second question
I used the Overtrue/wechat plug-in, the middleware with the plugin to successfully obtain the user information, and then the user route also output the session
public function handle($request, Closure $next, $guard = null) { $wechat = app('EasyWeChat\\Foundation\\Application'); if (!session('wechat.oauth_user')) { if ($request->has('state') && $request->has('code')) { session(['wechat.oauth_user' => $wechat->oauth->user()]); return redirect()->to(url($request->url(), array_except($request->query(), ['code', 'state']))); } return $wechat->oauth->redirect($request->fullUrl()); } return $next($request); }
Route::get('user', function () { dd(session('wechat.oauth_user')); });
On this basis, I have built my own middlewareUserMiddleware
public function handle($request, Closure $next) { if(Auth::guard('web')->guest()){ if (!session('wechat.oauth_user')) { return redirect('user'); } else{ User::CreateOrLogin(session('wechat.oauth_user')); } } return $next($request); }
User.php
Method CreateOrLogin
is
public static function CreateOrLogin($data){ $user=self::getByOpenid($data['id']); if(!$user){ $create=['openid'=>$data['id']]; $user=self::create($create); } Auth::guard('web')->login($user); } public static function getByOpenid($openid){ return self::where('openid','=',$openid)->first(); }
Everything is OK, but each refresh page will be executed to get user information interface, and then commented out, found Auth::guard('web')->login($user);
that the comment dropped, refresh the page will not jump to, plus to jump to get user information interface, it feels like login will clear out the session
Question Three
Route::group(['middleware' => ['web']], function () { Route::get('test', function () { dd(auth()->user()); }); Route::get('test2', function () { dd(auth()->loginUsingId(1)); });});
First visit test2, login successfully, then access test, Output null, why ...
Question one and question two and question three are not related, seek the great God solution
Check if the middleware is loaded web