Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call to a member function set() on null

Tags:

laravel-5

I am trying to use oauth for google in laravel 5 but i am getting the error. Can any one help me to sort out this problem.

Followings are my files please check out

.env

GOOGLE_ID = 'mygoogleId' 
GOOGLE_SECRET = 'mysecretID'
GOOGLE_REDIRECT = http://localhost:8090/users

services.php

'google' => [
        'client_id' => env('GOOGLE_ID'),
        'client_secret' => env('GOOGLE_SECRET'),
        'redirect' => env('GOOGLE_REDIRECT'),
    ],

AuthController
public function redirectToProvider() {
        return Socialite::driver('google')->redirect();
    }

public function handleProviderCallback()    {
        $user = Socialite::driver('google')->user();
            console.log($user);
}

routes.php

Route::get('google', 'Auth\AuthController@redirectToProvider');
Route::get('google/callback', 'Auth\AuthController@handleProviderCallback');

//I have set the providers and aliases in app.php.

Here is the code where i am getting an error

//on set() method

public function redirect()
{
    $state = str::random(40);

    if ($this->usesState()) {
        $this->request->getSession()->set('state', $state);
    }
    return new RedirectResponse($this->getAuthUrl($state));
}

Thanks in advance..

like image 661
Akshay Deshmukh Avatar asked Dec 24 '15 06:12

Akshay Deshmukh


Video Answer


1 Answers

Hey If you are using laravel 5.2, this is worked for me.

Put your controllers in 'web' middleware. like,

Route::group(['middleware' => 'web'], function() {
   Route::get('google', 'Auth\AuthController@redirectToProvider');
   Route::get('google/callback', 'Auth\AuthController@handleProviderCallback');
});

and Make sure Kernel file has middleware classes registered.

/**
 * The application's route middleware groups.
 *
 * @var array
 */
protected $middlewareGroups = [
    'web' => [
        \Perkweb\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \Perkweb\Http\Middleware\VerifyCsrfToken::class,
    ],

    'api' => [
        'throttle:60,1',
    ],
];
like image 189
Cowboy Avatar answered Oct 18 '22 23:10

Cowboy