laravel5.2 Session Issues

Source: Internet
Author: User
Tags oauth openid

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.phpMethod 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.phpMethod 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

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.