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:

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条回答 默认 最新

相关推荐 更多相似问题