2018-07-05 03:54 阅读 31

如何以编程方式登录然后使用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:

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

    点赞 评论 复制链接分享
  • dongliao9233 dongliao9233 2018-07-05 04:35

    You can login user by using Auth::login() like this

    public function RequestAccess() {
        try {
            $input = request()->all();
            $token = $input['token'];
            $user = User::where('mobile_access_token', $token)->first();
            if (!$user){
                 return view('public.error', ['errorCode' => 901, 'errorMessage' => 'Could not find user']);
        }catch(\Exception $e){
            return view('public.error', ['errorCode' => 900, 'errorMessage' => $e->getMessage()]);

    Note: You have written that your route is in api and in controller you are rendering view, be sure this code will work for web guard that has StartSession middleware. If you are looking for api authentication then you can look this

    check Other Authentication Methods section in

    点赞 评论 复制链接分享