douxie3625 2018-07-05 03:54
浏览 33
已采纳

如何以编程方式登录然后使用Laravel 5.5进行重定向?

I am using Laravel 5.5 and PHP 7.1 I am using the standard built-in Auth and middleware, I have not customized anything.

I am wanting to create a route in the api.php routes file that would be something like this:

Route::group(array('prefix' => 'mobile'), function () {
      Route::get('requestaccess', ['uses' => 'MobileController@RequestAccess']);
});

Then in my MobileController I would have this code:

public function RequestAccess() {
    try {
        $input = request()->all();
        $token = $input['token'];
        //$page = $input['page'];

        $user = User::where('mobile_access_token', $token)->first();
        if (!$user)
            return view('public.error', ['errorCode' => 901]);

        if ($user->mobile_access_expires < Carbon::now())
            return view('public.error', ['errorCode' => 902]);

         // I tried with and without the second param (true)
        if (!Auth::loginUsingId($user->id, true))
            return view('public.error', ['errorCode' => 903]);

        // My code makes it to here just fine which means the token
        // was valid and I 
        // successfully logged in.
        // Now I want to send them to a page where the user
        // can browse and remain logged in. 

        // I tried the following line but it does not work
        // It just immediately redirects me to the login page
        return redirect(url('dashboard'));

        // The following line works but if user clicks a link
        // to go to any other page it redirects them to the login page 
        return view('console.dashboard.dashboard');

    } catch (\Exception $e) {
        return view('public.error', ['errorCode' => 900, 'errorMessage' => $e->getMessage()]);
    }
}

and in the web.php route file I have the following:

Route::group(['middleware' => 'auth'], function() {
    Route::get('dashboard', ['uses' => 'DashboardController@getDashboard']);
});

and of course for testing I am typing something like this in my browser:

example.com/api/mobile/requestaccess?token=abc

Everything is all in one domain I am just wanting to log the user in and have them stay logged in while browsing the site.

Any help will be greatly appreciated.

  • 写回答

2条回答 默认 最新

  • dongyong1400 2018-07-05 04:03
    关注

    The API route doesn't have \Illuminate\Session\Middleware\StartSession::class this middleware, meaning it doesn't have any access to session so you cannot maintain session state i.e. logged in state. The purpose of api route is to have a stateless transmission.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。