简体   繁体   中英

Laravel Socialite Request Returns Null

I'm using Laravel Socialite 5.0.

<?php

use Illuminate\Http\Request;

public function login(AuthenticateUser $authenticateUser, Request $request){
    return $authenticateUser->execute($request->has('code'));
}

$request->has('code') always returns null. Why is that? Providing my AuthenticateUser class I see this as a redirect/request problem.

<?php 

namespace App;

use Laravel\Socialite\Contracts\Factory as Socialite;
use Illuminate\Contracts\Auth\Guard as Guard;
use App\Repositories\UserRepository as UserRepository;
use Log;

class AuthenticateUser {

    private $users;
    private $socialite;
    private $auth;

    public function __construct (UserRepository $users, Socialite $socialite, Guard $auth) {
        $this->users = $users;
        $this->socialite = $socialite;
        $this->auth = $auth;
    }

    public function execute ($hasCode) {
        if( ! $hasCode){
            Log::info('1');
            return $this->getAuthorizationFirst();
        }
        Log::info('2');
        $user = $this->socialite->driver('github')->user();
    }

    public function getAuthorizationFirst(){
        return $this->socialite->driver('github')->redirect();
    }

}

Adding the AuthController Class for reference:

class AuthController extends Controller {

    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */

    use AuthenticatesAndRegistersUsers;

    /**
     * Create a new authentication controller instance.
     *
     * @param  \Illuminate\Contracts\Auth\Guard  $auth
     * @param  \Illuminate\Contracts\Auth\Registrar  $registrar
     * @return void
     */
    public function __construct(Guard $auth, Registrar $registrar)
    {
        $this->auth = $auth;
        $this->registrar = $registrar;

        $this->middleware('guest', ['except' => 'getLogout']);
    }

    public function login(AuthenticateUser $authenticateUser, Request $request){
        Log::info('_'.$request->has('code').'_');
        return $authenticateUser->execute($request->has('code'));
    }

}

The problem was with the return url route! I was returning to a wrong route from the application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM